Install Magento 2, Apache2, PHP 7.2 and SSL on Ubuntu

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, avr: 5.00)

Magento is an e-commerce platform built with PHP and it’s an open source technology which provides online merchants with a flexible shopping cart, content and functionality of their online store.

This tutorial demonstrates step by step how to manually install Magento 2, Apache2, PHP 7.2 and SSL on Ubuntu.


Before starting with this guide, you’ll need to generate Magento 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.

For this tutorial, we will use as testing domain and the Ubuntu version is 18.10

Step 1: Install Apache2 as Web Server

To install Apache2 stable version by default repository Ubuntu, use the following single command to update and install Apache2

sudo apt update && sudo apt install apache2

Once Apache2 already installed, to check and verify the Apache service status type the command below

sudo service apache2 status

If it is running and working properly you will see the output Apache2 active (running) like below

 apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-01-02 17:58:26 UTC; 1min 54s ago
 Main PID: 14259 (apache2)
    Tasks: 55 (limit: 665)
   Memory: 6.1M
   CGroup: /system.slice/apache2.service
           ├─14259 /usr/sbin/apache2 -k start
           ├─14261 /usr/sbin/apache2 -k start
           └─14262 /usr/sbin/apache2 -k start
Jan 02 17:58:26 axfon-lab systemd[1]: Starting The Apache HTTP Server...
Jan 02 17:58:26 axfon-lab systemd[1]: Started The Apache HTTP Server.

To exit Apache2 service status just press q on the keyboard

Step 2: Configuration the Firewall with UFW

UFW (Uncomplicated Firewall) is an Iptable interface to easily configure a firewall on your system. If it does not yet enable, it’s recommended to enable and setup the rule for Apache

For the first, we have to add rule for SSH, let’s se the command line below

sudo ufw allow OpenSSH

Add the rule receive HTTP to Apache2

sudo ufw allow 'Apache Full'

Then you will see on terminal

Rule added
Rule added (v6)'

Now enable ufw for Firewall.

sudo ufw enable

If prompted just pres Y to accept and continue and to check UFW status type the command below

sudo ufw status

You will see the output UFW active (running) like below

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)

Apaceh2 web server now is ready on Ubuntu, now you may go to your web browser and visit your domain or IP. If you have not configured domain name yet and don’t know your IP, use the following command find out

sudo ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v ''

If everything is OK you will be presented on your browser with the default Apache2 landing page as below

Step 3: Install PHP 7.2 FPM and Related Modules

When this tutorial made the PHP 7.3 is not yet available in Ubuntu default repositories, for installing it we have to add manually ondrej/php PPA by running the following command

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

After adding the PPA we need to update Ubuntu, run the following command

sudo apt-get update

Once the PPA repository has been added and updated, now we are going to install PHP 7.2

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

You will be presented on the terminal screen as below

 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 Sat 2019-02-02 22:19:41 UTC; 1h 32min ago
     Docs: man:php-fpm7.2(8)
 Main PID: 10696 (php-fpm7.2)
   Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 3 (limit: 4399)
   Memory: 15.1M
   CGroup: /system.slice/php7.2-fpm.service
           ├─10696 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
           ├─10715 php-fpm: pool www
           └─10717 php-fpm: pool www
Feb 02 22:19:41 axfon systemd[1]: Starting The PHP 7.2 FastCGI Process Manager...
Feb 02 22:19:41 axfon systemd[1]: Started The PHP 7.2 FastCGI Process Manager.

Step 4: Configure PHP file (Optional)

The php.ini file is a default configuration file that read on PHP starts up. If you want to change the PHP settings on Ubuntu, Follow the below steps to modify the PHP configuration file php.ini using the command line on Ubuntu.

sudo nano /etc/php/7.2/apache2/php.ini

To edit the configuration file your may find or you can use Cmd/Ctrl W to search and replace it as the following

max_input_time = 30
error_log = /var/log/php/error.log
file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Los_Angeles

Press Ctrl/Cmd + X and then press Y and ENTER to save changes

Step 5: Install MariaDB or MySQL

There two options using MariaDB or MySQL Database, please choose one that you prefer with. To install MariaDB or MySQL on Ubuntu server let’s following steps:

Installing MariaDB (Option 1)

MariaDB is a fork of MySQL, the database structure and indexes of MariaDB are the same as MySQL. This allows you to switch from MySQL to MariaDB without having to alter your applications since the data and data structures will not need to change.

To install MariaDB packages from the MariaDB repository use the following command

sudo apt-get install mariadb-server mariadb-client

Press Y and ENTER when prompted on installing process
MariaDB service will start automatically, the commands below can be used to stop, start and enable MariaDB service.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

To check and verify MariaDB is working, use the command below

sudo systemctl status mariadb

The output MariaDB active (running) like below

 mariadb.service - MariaDB database server
   Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-02-02 10:37:38 UTC; 11s ago
 Main PID: 31143 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 27 (limit: 665)
   Memory: 70.4M
   CGroup: /system.slice/mariadb.service
           └─31143 /usr/sbin/mysqld

Press q on the keyboard to exit service status

To check MariaDB server version use the command below

mysql -V

If you already installed MariaDB server above, you should ignore below Installation MySQL and jump to step 6

Installing MySQL (Option 2)

MySQL is an open-source relational database management system (RDBMS). Just like all other relational databases, MySQL uses tables, constraints, triggers, roles, stored procedures and views as the core components

To install MySQL packages from the MySQL repository use the following command

sudo apt update && sudo apt install mysql-server

Press Y and ENTER when prompted on installing process

MySQL service will start automatically, the commands below can be used to stop, and start MySQL service.

sudo service mysql stop
sudo service mysql start

To check and verify MySQL is working, use the command below

sudo service mysql status

The output MySQL active (running) like below

 mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2019-02-02 10:37:38 UTC; 11s ago
 Main PID: 30545 (mysqld)
    Tasks: 27 (limit: 1153)
   CGroup: /system.slice/mysql.service
           └─30545 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/

Press q on the keyboard to exit service status

To check MySQL server version use the command below

mysql -V

Step 6: Configuration MySQL Security

To improve MySQL security it’s recommended to enable mysql_secure_installation. These packed to create a root password and disallow remotely root access. To install packages use the following ways

sudo mysql_secure_installation

On installation process will prompt and to be asked the validate password plugin, it can be used for password strength checking

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]:  Y
  • Reload privilege tables now? [Y/n]:  Y

If you didn’t create a root password, you have to generate a strong password

For testing MySQL server and run the following command.

sudo mysqladmin -p -u root version

Enter the MySQL root password you created before and see the following output

mysqladmin  Ver 9.1 Distrib 10.1.29-MariaDB, for debian-linux-gnu on x86_64
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Server version          10.1.29-MariaDB-6ubuntu2
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/run/mysqld/mysqld.sock
Uptime:                 24 min 27 sec
Threads: 1  Questions: 489  Slow queries: 0  Opens: 173  Flush tables: 1  Open tables: 30  Qu
eries per second avg: 0.333

Configured MySQL server on Ubuntu now is successful

Step 7: Create Database for Magento

At this step, Magento required Database to store data collection, to create a database run the commands below to login to the database server. When prompted for a password, type the root password you created above.

sudo mysql -u root -p

Then create a database name ex: “magento” for user “MAGENTOUSER” and for password “NEWPASSWORD”. Please attention you may change with anything else that you want. Let’s create a database called magento


Creating a database user called MAGENTOUSER with NEWPASSWORD


Then grant the user full access to the database.


Finally, save your changes and exit.


Step 8: Install 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 | sudo php -- --install-dir=/usr/local/bin --filename=composer

For the next we need to Unzip for extracting the package, to install the unzip utility

sudo apt install unzip

Step 9: Install Magento 2

Start the installation by downloading Magento files to the default directory /var/www/html and create a new folder lab-axfon (Change it with your own name), just follow the command below for the easy way

composer create-project --repository-url= magento/project-community-edition /var/www/html/lab-axfon

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 (
      Username: cf5e0a09388557182414e2d2ad663ee5
      Password: 0f57c43cba4276dbf37c245b48ccb3a3

After filling the Username and Password then Enter, it may take a few minutes to process and if it is successful at the end of the output should look like the following:

Writing lock file
Generating autoload files

Now go to the folder lab-axfon to Install Magento

cd /var/www/html/lab-axfon/

In this guide, we are going to install Magento using the command line. On the base URL use https for SSL certificate installation on the next step and change other value with your own

php bin/magento setup:install --base-url= \
        --base-url-secure= \
        --admin-firstname=YOUR_FIRST_NAME \
        --admin-lastname=YOUR_LAST_NAME \
        --admin-email=YOUR_EMAIL \
        --admin-user=USERNAME \
        --admin-password=YOUR_PASSWORD \
        --db-name=magento \
        --db-host=localhost \
        --db-user=MAGENTOUSER \
        --currency=USD \
        --timezone=America/Chicago \
        --use-rewrites=1 \

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

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

After running the commands above, a new project directory will be created. Run the commands below to set the correct permissions for that directory

sudo chown -R www-data:www-data /var/www/html/lab-axfon/
sudo chmod -R 755 /var/www/html/lab-axfon/

Step 10: Configuration Virtual Hosts on Apache2

If you wish to host multiple or one sites/domain, the way should to do is setting up virtual hosts. There are several ways to set up virtual hosts on Apache. The recommended method is the following steps outline will make things a lot easier for you.

The Virtual Host files are located in /etc/apache2/sites-available/  to allow web server respond to various domain requests.

To create a configuration file, copy default file to new files for our domain (replace it with your domain name)

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/

Open these files to edit and configure using nano editor with the following command

sudo nano /etc/apache2/sites-available/

Enter the document root path (change with your own) and log directories as shown below, and add Directory  block before <VirtualHost>:

<Directory /var/www/>
        Require all granted
        Options Indexes FollowSymLinks
        AllowOverride All
<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/lab-axfon/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

Press Ctrl/Cmd + X and then press Y and ENTER to save changes and close nano

Step 11: Enable and Restart Apache2

After configuring the Virtual Host, Enable and rewrite modules using the following commands

sudo a2ensite
sudo a2enmod rewrite

Changes made in the Apche2 configuration file will not be applied until the command to Restart and Before testing on the Browser we have to verify and make sure no error.
To restart Apache2 use the following the command line

sudo service apache2 restart

Step 12: Install SSL Certificate

Let’s Encrypt is a free automated and open certificate authority for SSL(Secure Sockets Layer) just as secure as currently paid certificates. On this step we will configure an SSL certificate for Apache2  on Ubuntu.

Let’s begin by updating the package lists and installing software-properties-common. Commands separated by && will run in succession.

sudo apt-get update && sudo apt-get install software-properties-common

Now add the repositories universe and certbot.

sudo add-apt-repository universe && sudo add-apt-repository ppa:certbot/certbot

Press ENTER if prompted.

sudo add-apt-repository universe && sudo add-apt-repository ppa:certbot/certbot

Update the package lists again and installing certbot for Apache. This is the Let’s Encrypt client.

sudo apt-get update && sudo apt-get install certbot python-certbot-apache

Press Y and ENTER when prompted to continue.

We will now obtain a cert for our test domain Certbot has an Apache plugin, which automates the certificate installation.

sudo certbot --apache

Enter your email address and Press A and ENTER to agree to the Terms of Service.

On Step please choose whether or not to redirect HTTP traffic to HTTPS. Press 2 and ENTER to redirect all traffic to HTTPS

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

Step 13: Testing Magento 2 Browser

You can now view this page in your web browser by visiting your server’s domain name or public IP address http://your_domain_or_IP/

If this tutorial could help you, please rate above Star button rating and share to help others find it! Feel free to leave a comment below.

Recommended For You

About the Author: Adzan

Internet enthusiast like to share about newest information technology such as programming language, software developer and etc...