How to Install Magento on Ubuntu 18.04

How to Install Magento on Ubuntu 18.04

In this guide, we will show you how to install Magento 2 on an Ubuntu 18.04 VPS using MySQL, Nginx, and PHP-FPM 7.2.

Magento is a modern and feature-rich open-source eCommerce web application. Magento is fully customizable to meet any user’s requirements, allowing them to create and launch a fully functional eCommerce store in minutes. Magento is written in PHP and uses MySQL/MariaDB as a database backend.

This guide should work on other Linux VPS systems as well, but it was tested and written for an Ubuntu 18.04 VPS.

Prerequisites:

  • An Ubuntu 18.04 VPS
  • Access to the root user, or access to a user account with sudo privileges

Before we begin, you’ll need to log into your server and update your system’s software packages.

ssh root@Server_IP -p Port_Number

Remember to replace Server_IP and Port_Number with your server’s respective IP address and SSH port number.

Run the following commands to update your system and install all necessary packages:

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install curl nano git

Step 1: Install MySQL and Create a Database

Run the following command to install the latest MySQL 5.7 server from the official Ubuntu repositories:

sudo apt-get install mysql-server

Once the installation is complete, issue the following command to improve the security of your MySQL server installation (we recommend answering with ‘Y’ to every prompt):

mysql_secure_installation

Next, we need to create a MySQL database and user for the new Magento installation.

Login to the MySQL console:

sudo mysql

Run the following commands to create a new database, user, and to grant privileges for the database to the user:

mysql> CREATE DATABASE magento;
mysql> GRANT ALL PRIVILEGES ON magento.* TO 'magento'@'localhost' IDENTIFIED BY 'my_strong_password';
mysql> FLUSH PRIVILEGES;
mysql> \q

Remember to replace my_strong_password with an actual strong password.

Step 2: Install PHP 7.2, Composer and Required PHP Modules

Since version 2.3 Magento 2 is fully compatible with PHP 7.2.

To install the latest stable version of PHP 7.2 and all necessary modules, run:

sudo apt-get install php7.2-fpm php7.2-common php7.2-gd php7.2-mysql php7.2-curl php7.2-intl php7.2-xsl php7.2-mbstring php7.2-zip php7.2-bcmath php7.2-soap php7.2-opcache

Set the recommended PHP settings:

sudo sed -i "s/memory_limit = .*/memory_limit = 768M/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 128M/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.2/fpm/php.ini

Composer is a PHP dependency manager that allows you to install PHP packages. The composer will pull in all of the required libraries and dependencies you need for this project.

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Step 3: Install Magento

At the time of writing this tutorial, the latest stable version is Magento 2.3.0.

Clone the Magento repository to the /var/www/myMagentoSite.com directory using the following command:

sudo git clone https://github.com/magento/magento2.git /var/www/myMagentoSite.com

Navigate to the Magento directory:

cd /var/www/myMagentoSite.com

Run Composer to install all Magento dependencies:

sudo composer install

The command can take some time to complete. Once completed, you’ll see the following output:

Generating autoload files

To continue with the installation, you can either use the installation wizard or the command line. In this guide we will install Magento using the setup:install command.

Run the following command to start the installation. Do not forget to set the correct MySQL database information.

sudo bin/magento setup:install \
--base-url=http://myMagentoSite.com/ \
--db-host=localhost \
--db-name=magento \
--db-user=magento \
--db-password=my_strong_password \
--admin-firstname=First  \
--admin-lastname=Last \
--admin-email=user@myMagentoSite.com \
--admin-user=admin \
--admin-password=my_strong_password123 \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1

If the installation is successful, you will see something like the message below:

[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_1kd5tz
Nothing to import.

Remember the Magento admin URI – you’ll need it to access your Magento installation later.

Set the correct permissions:

sudo chown -R www-data: /var/www/myMagentoSite.com

Run the following command to create the Magento crontab:

sudo -u www-data php bin/magento cron:install
Crontab has been generated and saved

Step 4: Install and configure Nginx

Install Nginx from the official Ubuntu repositories using the following command:

sudo apt-get install nginx

Create a new Nginx server block called myMagentoSite.com (You should change this to your domain name)::

sudo nano /etc/nginx/sites-available/myMagentoSite.com

Then paste the following content into the file:

upstream fastcgi_backend {
  server   unix:/run/php/php7.2-fpm.sock;
}

server {
    server_name myMagentoSite.com www.myMagentoSite.com; listen 80; set $MAGE_ROOT /var/www/myMagentoSite.com; set $MAGE_MODE developer; # or production access_log /var/log/nginx/myMagentoSite.com-access.log; error_log /var/log/nginx/myMagentoSite.com-error.log; include /var/www/myMagentoSite.com/nginx.conf.sample; }

Activate the server block by creating a symbolic link :

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
sudo ln -s /etc/nginx/sites-available/myMagentoSite.com /etc/nginx/sites-enabled/myMagentoSite.com

Delete the default Nginx configuration:

sudo rm -f /etc/nginx/sites-enabled/default

Test the Nginx configuration:

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the nginx server for changes to take effect:

sudo service nginx restart

You should be now able to log in to your Magento back-end by going to http://myMagentoSite.com/admin_1kd5tz using the information you set when running the bin/magento setup:install command.

That’s it. You have successfully installed Magento 2 with MySQL and Nginx on your Ubuntu 18.04 VPS. For more information about how to manage your Magento installation, please refer to the official Magento documentation. We also published a guide on How to Install Magento 2.4 with LEMP Stack on Ubuntu 20.04.


Of course, you don’t have to do any of this if you use one of our Magento VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post, please share it with your friends on the social networks using the buttons below, or simply leave a comment down in the comments section. Thanks.

Leave a Comment