How to Install Magento 2 on Ubuntu 18.04

Updated on

8 min read

Install Magento 2 on Ubuntu 18.04 with Composer

Magento is leading enterprise-class e-commerce platform built on open-source technology combining powerful features with flexibility and user-friendly interface.

With features like Engaging Shopping Experiences, Flexible Modular Architecture and Enterprise-grade Scalability and Performance Magento is a platform of choice for most online merchants.

In this tutorial, we will show you how to install Magento 2.3 on an Ubuntu 18.04 machine. We’ll be using Nginx as a web server, the latest PHP 7.2 and MySQL/MariaDB as a database server.

Prerequisites

Ensure that you have met the following prerequisites before continuing with this tutorial:

  • Have a domain name pointing to your public server IP. We will use example.com.
  • Nginx is installed on your Ubuntu server by following these instructions .
  • An SSL certificate installed for your domain to encrypt user’s information. You can install a free Let’s Encrypt SSL certificate by following these instructions .

To be able to access to the Magento 2 code repository you’ll need to generate authentication keys. If you don’t have a Magento Marketplace account, you can create one here . Once you create the account, please check these instructions on how to generate a new set of authentication keys.

Update the system packages to the latest versions and install the unzip utility :

sudo apt update && sudo apt upgradesudo apt install unzip

Creating MySQL database

If you have MySQL or MariaDB installed on your server you can skip this step, if not you can install the MySQL 5.7 server package from the Ubuntu’s default repositories by typing:

sudo apt install mysql-server mysql-client
For fresh MySQL installations, it is recommended to run the mysql_secure_installation command to improve the security of your MySQL server.

Login to the MySQL shell using the following command:

sudo mysql

From within the MySQL shell, run the following SQL statement to create a new database named magento:

CREATE DATABASE magento;

Next, create a MySQL user account named magento and grant the necessary permissions to the user by running the following command:

GRANT ALL ON magento.* TO 'magento'@'localhost' IDENTIFIED BY 'change-with-strong-password';
Make sure you change change-with-strong-password with a strong password.

Once done, exit the MySQL console by typing:

EXIT;

Creating System User

Create a new user and group, which will be Magento file system owner , for simplicity we will name the user magento:

sudo useradd -m -U -r -d /opt/magento magento

Add the www-data user to the magento group and change the /opt/magento directory permissions so that the Nginx can access the Magento installation:

sudo usermod -a -G magento www-datasudo chmod 750 /opt/magento

Installing and Configuring PHP

PHP 7.2 which is the default PHP version in Ubuntu 18.04 is fully supported and recommended for Magento 2.3. Since we will be using Nginx as a web server we’ll also install the PHP-FPM package.

Run the following command to install PHP and all required PHP modules:

sudo apt install php7.2-common php7.2-cli php7.2-fpm php7.2-opcache 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

PHP-FPM service will automatically start after the installation process is complete, you can verify it by printing the service status:

sudo systemctl status php7.2-fpm

The output should indicate that the fpm service is active and running.

● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2018-12-12 15:47:16 UTC; 5s ago
     Docs: man:php-fpm7.2(8)
 Main PID: 16814 (php-fpm7.2)
   Status: "Ready to handle connections"
    Tasks: 3 (limit: 505)
   CGroup: /system.slice/php7.2-fpm.service

Set the required and recommended PHP options by editing the php.ini file with sed ::

sudo sed -i "s/memory_limit = .*/memory_limit = 1024M/" /etc/php/7.2/fpm/php.inisudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 256M/" /etc/php/7.2/fpm/php.inisudo sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.2/fpm/php.inisudo sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.2/fpm/php.inisudo sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.2/fpm/php.inisudo sed -i "s/;opcache.save_comments.*/opcache.save_comments = 1/" /etc/php/7.2/fpm/php.ini

Next we need to create a FPM pool for the magento user.

Open your text editor and create the following file:

sudo nano /etc/php/7.2/fpm/pool.d/magento.conf
/etc/php/7.2/fpm/pool.d/magento.conf
[magento]
user = magento
group = www-data
listen.owner = magento
listen.group = www-data
listen = /var/run/php/php7.2-fpm-magento.sock
pm = ondemand
pm.max_children =  50
pm.process_idle_timeout = 10s
pm.max_requests = 500
chdir = /

Restart the PHP-FPM service for changes to take effect:

systemctl restart php7.2-fpm

Verify whether the PHP socket was successfully created by running the following ls command :

ls -al /var/run/php/php7.2-fpm-magento.sock

The output should look something like this:

srw-rw---- 1 magento www-data 0 Dec 12 16:07 /var/run/php/php7.2-fpm-magento.sock=

Installing Composer

Composer is a dependency manager for PHP and we will be using it to download the Magento core and install all necessary Magento components.

To install composer globally, download the Composer installer with curl and move the file to the /usr/local/bin directory:

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Verify the installation by printing the composer version:

composer --version

The output should look something like this:

Composer version 1.8.0 2018-12-03 10:31:16

Installing Magento

There are several ways to install Magento 2. Avoid installing Magento from the Github repository because that version is intended for development and not for production installations.

At the time of writing this article, the latest stable version of Magento is version 2.3.0. In this tutorial, we will install Magento from their repositories using composer.

Switch over to the user magento by typing:

sudo su - magento

Start the installation by downloading magento files to the /opt/magento/public_html directory:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition /opt/magento/public_html

You’ll be prompted to enter the access keys, copy the keys from your Magento marketplace account and store them in the auth.json file, so later when updating your installation you don’t have to add the same keys again.

    Authentication required (repo.magento.com):
      Username: e758ec1745d190320ca246e4e832e12c
      Password: 
Do you want to store credentials for repo.magento.com in /opt/magento/.config/composer/auth.json ? [Yn] Y

The command above will fetch all required PHP packages. The process may take a few minutes and if it is successful the end of the output should look like the following:

Writing lock file
Generating autoload files

Once the project is created we can start the Magento installation. We can install Magento either from the command line or using the web Setup Wizard. In this tutorial, we’ll install Magento using the command line.

We will use the following options to install the Magento store:

  • Base and Base secure URLs are set to https://example.com, change it with your domain.
  • Magento administrator:
    • John Doe as first and last name.
    • john@example.com as email.
    • john as username and j0hnP4ssvv0rD as password.
  • Database name magento, username magento, password change-with-strong-password and the database server is on the same host as the web server.
  • en_US, US English as a default language.
  • USD dollars as default currency.
  • America/Chicago as a time zone.
You can find all the installation options here .

Change to the Magento ~/public_html directory:

cd ~/public_html

Run the following command to start the installation:

php bin/magento setup:install --base-url=https://example.com/ \
        --base-url-secure=https://example.com/ \
        --admin-firstname="John" \
        --admin-lastname="Doe" \
        --admin-email="john@example.com" \
        --admin-user="john" \
        --admin-password="j0hnP4ssvv0rD" \
        --db-name="magento" \
        --db-host="localhost" \
        --db-user="magento" \
        --currency=USD \
        --timezone=America/Chicago \
        --use-rewrites=1 \
        --db-password="change-with-strong-password"
Don’t forget to change the password (j0hnP4ssvv0rD) to something more secure.

The process may take a few minutes and once completed you will be presented with a message that contains the URI to the Magento admin dashboard.

[Progress: 773 / 773]
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_13nv5k
Nothing to import.

Creating Magento crontab

Magento uses cron jobs to schedule tasks like re-indexing, notifications, sitemaps, emails and more.

To create the Magento crontab run the following command as magento user:

php ~/public_html/bin/magento cron:install
Crontab has been generated and saved

Verify that the crontab is installed by typing:

crontab -l
#~ MAGENTO START adc062915d7b30804a2b340095af072d
* * * * * /usr/bin/php7.2 /opt/magento/public_html/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /opt/magento/public_html/var/log/magento.cron.log
* * * * * /usr/bin/php7.2 /opt/magento/public_html/update/cron.php >> /opt/magento/public_html/var/log/update.cron.log
* * * * * /usr/bin/php7.2 /opt/magento/public_html/bin/magento setup:cron:run >> /opt/magento/public_html/var/log/setup.cron.log
#~ MAGENTO END adc062915d7b30804a2b340095af072d

Configuring Nginx

By now, you should already have Nginx with SSL certificate installed on your Ubuntu server, if not check the prerequisites for this tutorial.

We are going to include the default Nginx configuration shipped with Magento.

Switch over to your sudo user, open your text editor and create the following file:

sudo nano /etc/nginx/sites-available/example.com
/etc/nginx/sites-available/example.com
upstream fastcgi_backend {
  server   unix:/var/run/php/php7.2-fpm-magento.sock;
}

server {
    listen 80;
    server_name example.com www.example.com;

    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    set $MAGE_ROOT /opt/magento/public_html;
    set $MAGE_MODE developer; # or production

    access_log /var/log/nginx/example.com-access.log;
    error_log /var/log/nginx/example.com-error.log;

    include /opt/magento/public_html/nginx.conf.sample;
}
Don’t forget to replace example.com with your Magento domain and set the correct path to the SSL certificate files. The snippets used in this configuration are created in this guide .

Before restarting the Nginx service make a test to be sure that there are no syntax errors:

sudo nginx -t

If there are no errors the output should look like this:

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

Finally, restart the Nginx service by typing:

sudo systemctl restart nginx

Verifying the Installation

Open your browser, type your domain and assuming the installation is successful, a screen similar to the following will appear:

ubuntu magento

You can now go to the Magento Admin URI, log in as the admin user and start customizing your new Magento installation.

Conclusion

Congratulations, you have successfully installed Magento 2.3 on your Ubuntu 18.04 server. You can now start customizing your store.

Magento 2.3 Developer Documentation is a good starting place to learn more about how to manage your Magento installation.

If you have questions, feel free to leave a comment below.