How To Install Magento 2.3 on Ubuntu 18.04 LTS or Debian Buster in 10 minutes

The Magento developers provide great documentation covering installing Magento, however, they have created rather too much. They list several options at each step which are spread across many pages of dense information. I have gone through this information and extracted a simple Debian Buster or Ubuntu 18.04 server to a working Magento installation in under 10 minutes.

Prerequisites: 1. An Ubuntu 18.04 LTS or Debian Buster server with at least 1GB of RAM. 2. Basic knowledge of using the Linux command line. 3. The hostname of your store must resolve (point) to the IP address of your server before you start this guide if you want to install an SSL certificate. 4. An account with Magento. Login or sign up here.

Please note, Whenever I mention Magento in the following guide I am referring to Magento 2.3.

To begin these steps, you must SSH into your server as the root user.

Step 1 – Install all necessary packages.

Magento requires a web server, a database server, PHP and some PHP extensions.

We will use Apache2 and MariaDB as our web and database servers along with the default PHP in Ubuntu 16.04 and Debian Buster, PHP 7.3. The following commands will install everything required:

apt update
apt install apache2 mariadb-server php libapache2-mod-php php-bcmath php-dev php-gd php-intl php-json php-mbstring php-mysql php-opcache php-xml php-soap php-curl php-zip

This will install PHP 7.2 on Ubuntu and PHP 7.3 on Debian. Both versions are supported by Magento 2.3

Step 2 – Create a system user

The Magento developers recommend, for security reasons, that a non-root system user is created and added to the webserver group. This user should be used when running the magento binary that is used to manage the Magento installation.

We will create a user called magento and add them to the web server’s group ‘ www-data. with the following commands:

adduser magento
usermod -a -G www-data magento

Next, we need to create and configure the database that Magento will use.

Step 3 – Creating a database

In order to create a database, we need to first enter the MariaDB shell. This is simply done by entering the command mysql on the command line. When you do that you will be immediately taken to the MariaDB shell that will supply a new prompt that looks like this:

MariaDB [(none)]>

The following command will create a new database called magentodb:

CREATE DATABASE magentodb;

Next, we will create a new user called magento and assigned them full permissions to the new database with the following command:

GRANT ALL ON magentodb.* TO magento@localhost IDENTIFIED BY 'a_strong_password';

You must change a_strong_password to a secure password when you enter this command. Make a note of the database name, username and password as they will be required later. After you have done this log out of the MariaDB shell by entering exit;.

It is a good idea to validate the database we just created along with the username and password. This is done by logging into the MariaDB shell as the new user with the following command:

mysql -u magento -p magentodb

If you can log in using the password you set in the “GRANT ALL” command then the new database and user are working correctly.

The next job is to download, unpack and prepare Magento archive.

Step 4 – Downloading the Magento archive and setting permissions

The archive containing Magento is downloaded from this page:

https://magento.com/tech-resources/download

You should select version 2.3.x without sample data. We will use the .tar.gz version as this is the most widely supported compression format. The required version is shown highlighted here:

You must create an account to download the file. Once you have downloaded the file you will need to upload it to your server.

After you have uploaded the archive to your server you need to log back in as root. Create the directory into which you will unpack the Magento archive:

mkdir /var/www/magento2

Next, unpack the Magento archive to /var/www/magento2:

tar -vxf /path/to/Magento-archive.tar.gz --directory /var/www/magento2

You can now delete the Magneto archive if you want to.

Next, we will set recommended ownerships and permissions. The following commands must be run from the installation directory, i.e. /var/www/magento2, so the first command will move you to that directory:

cd /var/www/magento2
find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
chmod u+x bin/magento

Finally, we have to set the correct ownerships:

chown -R magento:www-data /var/www/magento2

We will finish installing Magento from an installation page accessed by a browser.

Step – 5 Configuring Apache

We need to configure Apache for HTTP so that we can use it to install the SSL certificate in the next step.The application that we will use in the next step will create the HTTPS configuration automatically.

First, change into the sites-available directory under Apache’s configuration directory:

cd /etc/apache2/sites-available/

Then use your preferred text editor to create and open a new VirtualHost file. Here we will use nano and the file will be called example.com.conf:

nano example.com.conf

We will use the following VirtualHost template:

<VirtualHost *:80>
        ServerName <STORE HOSTNAME>
        DocumentRoot /var/www/magento2
        ErrorLog ${APACHE_LOG_DIR}/magento_error.log
        CustomLog ${APACHE_LOG_DIR}/magento_access.log combined

        <Directory /var/www/magento2 >
                Options FollowSymLinks
                AllowOverride All
        </Directory>

</VirtualHost>

Copy and paste this into the VirtualHost file. The only change that you will need to make is to change <STORE HOSTNAME> to the name of your web store.

Then, enable the new site with the following command:

a2ensite example.com

You must change this example to match the name of the VirtualHost file you created. Simply use the file name without the “.conf” to enable it e.g. my-domain.com.conf becomes:

a2ensite my-domain.com

Next, run the following to enable mod_rewrite Apache module which is required by Magento:

a2enmod rewrite

It is always a good idea to check any new Apache configuration with its built-in tool to ensure that there are no errors. This is because if you have made a mistake in the configuration Apache will not restart taking any other sites on your server offline. The following command checks Apache’s configuration files for syntax errors:

apachectl configtest

If there are no problems Syntax OK will be printed to the screen. If there is an issue the output will tell you the file and line that has the error. When you can pass the configuration test run the following command to reload Apache:

systemctl reload apache2

Step 6 - Install an SSL certificate

In this step we will install the SSL (TLS) certificate for your store so that you will be able to use HTTPS links. We will use a free Let’s Encrypt certificate that will work in all browsers and the CertBot application to install the certificate and keep it updated.

First download all the required packages:

apt-get install certbot python-certbot-apache 

Next, run the certbot command that will download the certificate and create the Apache configuration to use the certificate:

certbot --apache

You will then be prompted to enter an email address for the certificate. After you have entered that you must agree to the T&C’s and decide if you want to share your email address with the Electronic Frontier Foundation. This last step is optional.

The final prompt will ask you if you want to allow HTTP and HTTP access to your store or if you want HTTPS only. If you opt for the latter HTTP requests will get redirected to the HTTPS site. HTTPS only is the more secure and is the recommended, modern option.

Reload Apache again to load all the new configuration:

systemctl reload apache2

When you installed Certbot a systemd timer was created that will automatically attempt to renew any certificates on your server twice per day. When a certificate gets close to expiry it will get renewed automatically.

Your web store is now protected with an SSL certificate which will get automatically renewed before it expires.

Step – 7 Installing Magento

We will install Magento by accessing the installation page in a web browser. You need to visit the web site address that you set in the VirtualHost file and you will be automatically redirected to the installation page.

Start the installation by clicking on:

On the next page click:

This will initiate a test that checks that all the required PHP extensions are on the server and that the correct ownership and permissions were set. Everything should pass as we have installed and configured everything as required by Magento.

Click:

to proceed to the next page when the tests have completed.

The next page is where we configure Magento to use the database we created. This is a screenshot of the fields filled in with the details we used when we created the database above:

You will need to enter the password you set when we created the database. Click Next when you have filled in the fields.

The next page is where you can change the store name and administration access URL if you need to. Make sure to change the http:// to https:// here. If you are not sure about these leave them as you can change them after Magento is installed.

On the next page, you can set the time zone, currency and language of your store.

The next page is where you must create a user to access the Magento administration panel. Do not use admin as the username as this is always the first username that hackers will try to guess.

Click Next when you have filled in those fields and taken note of the information you set.

On the last page click:

This will initiate the installation process. It will take a couple of minutes to complete.

When the installation has finished you will see a Success page listing all the details of your Magento installation. You should copy and paste all this information into a file for future reference.

The final task for this installation is to create Magento cron jobs.

Step 8 – Creating the Magento cron jobs

Magento requires several periodic, backend tasks to keep the installation working correctly. Linux has a specific program called cron which is designed to run tasks or jobs at intervals. Magento provides a tool to create the correct cron entries to keep everything up-to-date.

Log back into your server’s command line and reset the owenships again after the installation:

chown -R magento:www-data /var/www/magento2

Then change to the magento that you created in Step 2:

su - magento

Then move to /var/www/magento2:

cd /var/www/magento2

When you are there run the following command:

bin/magento cron:install

You can check what was written to the crontab with the following command:

crontab -l

You should see something like the following:

#~ MAGENTO START a6b8f559a4432711bb5c07bca3c5734473e32ef2453c2c0db27c7f50fc023a63
* * * * * /usr/bin/php7.3 /var/www/magento2/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /var/www/magento2/var/log/magento.cron.log
* * * * * /usr/bin/php7.3 /var/www/magento2/update/cron.php >> /var/www/magento2/var/log/update.cron.log
* * * * * /usr/bin/php7.3 /var/www/magento2/bin/magento setup:cron:run >> /var/www/magento2/var/log/setup.cron.log
#~ MAGENTO END a6b8f559a4432711bb5c07bca3c5734473e32ef2453c2c0db27c7f50fc023a63

Magento is now fully installed and working.

Conclusion

We have now completed a Magento 2.3 installation following the recommendations of the Magento developers. A good idea at this point is to take a backup of your server or VM before you customize your Magento installation. This will give you an excellent starting point to return to if you mess up your installation or if you simply want to experiment with Magento and want a handy starting point before settling on a production version.