How to set up a web server for Magento 2.2

IMPORTANT NOTICE: This tutorial has now been replaced with a new version for Magento 2.4.
See the new tutorial here

Important update: This tutorial was originally created for Magento 2.1.5, but Magento 2.2.2 requires some minor changes to the instructions to make these instructions work. Please, pay extra attention to any instructions marked in red as they will differ slightly from the original videos.

As a reminder, these instructions are for a clean Ubuntu server. If you’re using a pre-existing web server or using WHM/cPanel/Plesk then your results may vary.

Before I start, I just want to let you know that this series isn’t going to be for everyone. You might want to stick around if you’re interested to know how to create a web server for Magento 2 – for the purposes of curiosity or development .

But if technology scares you, I recommend setting up Magento 2 using a one-click installation method. I highly recommend Nexcess.net if you are looking for a simple method to get a Production Web Server up and running for Magento 2.

This article is best used in conjunction with the provided videos because I have timestamped the commands that I use so that they correlate with the video. This should make is easier for you to follow along.

What this article will cover

Like most things in life, there is more than one way of doing them. For this article, I have decided to use Ubuntu as my chosen Operating System, running Apache , MySQL and PHP 7 . And then using Composer as my method to download Magento 2. These are the sections you will find as you scroll down:

How to create a Ubuntu Server

In order to create our web server for Magento 2, we need to use a server. One option is to use a Virtual Machine but for the purpose of this article, we will use service provided by DigitalOcean.com . Click this link will entitle you to $10 free credit to spend on your server .

How to start a Ubuntu 16.04 Server on DigitalOceon

Once you have registered with Digital Ocean, simply Create a Droplet. You only have to select:

  • The server type (Ubuntu)
  • The Size ($10/mo Package)
  • The Datacenter Region (The closest to your country)

And then you have hit ‘Create’. It takes 1-2 minutes to the server to be created, but once it has you will receive an Email with your SSH Root Login Credentials.

How to connect to your new server

Now that you have a server, you will need a way of connecting to it. In this article, I am working on a Windows machine so I will need to download Putty from Putty.org .

Once installed, you only need to enter the IP Address of your new server into the ‘Host Name (or IP address)’ Field and click ‘Open’.

How to create a new user – and grant it superuser privileges

In order to create our web server for Magento 2, we need to use a server. One option is to use a Virtual Machine but for the purpose of this article, we will use service provided by DigitalOcean.com. Click this link will entitle you to $10 free credit to spend on your server.

How to start a Ubuntu 16.04 Server on DigitalOceon
Once you have registered with Digital Ocean, simply Create a Droplet. You only have to select:

The server type (Ubuntu)
The Size ($10/mo Package)
The Datacenter Region (The closest to your country)
And then you have hit ‘Create’. It takes 1-2 minutes to the server to be created, but once it has you will receive an Email with your SSH Root Login Credentials.

How to connect to your new server

Now that you have a server, you will need a way of connecting to it. In this article, I am working on a Windows machine so I will need to download Putty from Putty.org .

Once installed, you only need to enter the IP Address of your new server into the ‘Host Name (or IP address)’ Field and click ‘Open’.

How to create a new user – and grant it superuser privileges

Once you have successfully logged into your server using Putty, enter the following commands.

Create yourself a user [04:23]

adduser craig

Give this user ‘superuser’ or root privileges by adding it to the ‘sudo’ group [05:06]

usermod -aG sudo craig

How to disable the root user (for security)

**Before disabling root access completely, log out as the root user and relog as the new user. Once logged in as the new user, check that your ‘superuser’ access works. You can check this by switching to the root user and back again. If you followed the steps carefully, then you will not see an error

Access config file to disable root user login** [06:18]

nano /etc/ssh/sshd_config

Change the below line, then save and exit [06:32]

#PermitRootLogin yes to PermitRootLogin no

Reload the SSH Service for changes to take effect [06:58]

sudo systemctl reload sshd

How to enable a basic firewall

First, you must allow SSH connections to your web server. This is the connection we are currently using. [07:58]

sudo ufw allow ssh

Then we turn on the firewall [08:26]

sudo ufw enable

How to create a LAMP stack on Ubuntu

How to install Apache and configure it for Magento 2

Download the package lists from pre-selected repositories and update them on our web server. [01:32]

sudo apt-get update

Install Apache [01:46]

sudo apt-get install apache2 -y

Open Apache settings file to allow .htaccess file usage [02:38]

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

Add the below to the file, then save and exit [03:00]

<Directory "/var/www/html">
    AllowOverride All
</Directory>

Open Apache settings file to set the Global ServerName [03:24]

sudo nano /etc/apache2/apache2.conf

Add this line at the end of the file, then save and exit [03:35]

ServerName <server_IP>

Check for any errors [04:02]

sudo apache2ctl configtest

Enable Apache rewrite [04:40]

sudo a2enmod rewrite

Restart Apache for any changes to take effect [04:52]

sudo systemctl restart apache2

Enable Apache through the firewall [05:28]

sudo ufw allow in "Apache Full"

How to install MySQL and secure the installation

MySQL is a database management system that allows the web server to store information that can be accessed by Magento. [06:22]

sudo apt-get install mysql-server -y

Run a MySQL security script that addresses some vulnerabilities [07:08]

sudo mysql_secure_installation

How to install PHP and any extensions needed for Magento 2

PHP allows the web server to process code, run scripts and display dynamic content that Magento 2 uses. [08:36]

Important Update: Added php7.0-soap and php7.0-bcmath to command

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql php7.0-soap php7.0-bcmath -y

Tell the webserver to prefer PHP files (move index.php to front of the list) [08:50]

sudo nano /etc/apache2/mods-enabled/dir.conf

Install PHP Modules for use with Magento 2 [09:52]

sudo apt-get install php-common php-cli php-curl php-intl php-zip -y

Restart apache for changes to take effect [10:08]

sudo systemctl restart apache2

How to install phpMyAdmin and secure the installation

phpMyAdmin allows us to manage MySQL Databases using a Graphical User Interface, rather than having to use a Command Line Interface.

Install phpMyAdmin [Warning: Press space to select ‘apache2’ before hitting Enter to continue] [10:56]

sudo apt-get install phpmyadmin php-mbstring php-gettext -y

Make sure that you select apache2 using your SPACE BAR before hitting ENTER to continue

Enable 2 phpMyAdmin required extensions [11:42]

sudo phpenmod mcrypt
sudo phpenmod mbstring

Restart Apache for any changes to take effect [12:12]

sudo systemctl restart apache2

phpMyAdmin Checks

Visit the phpMyAdmin portal you’ve just created via a web browser to ensure the page loads HTTP://<your_server>/phpmyadmin

How to set file system permissions

How to create a new web user

The reason for creating a new web user is for added security. The main user that we created (e.g. ‘craig’) has the ability to run superuser commands if required, where the web user would never need such privileges. Also, having a dedicated web user on the server means not having to keep updating folder/file permissions whenever you add or move files.

Add the user (I’ve called my user ‘magento’) [01:38]

sudo adduser magento

How to add the user to the web servers primary group
Make the web server group the primary group for the new user [02:30]

sudo usermod -g www-data magento

How to set folder permissions

Update the root file ownership to coincide with our new web user [03:12]

sudo chown magento:www-data /var/www/html/

How to install Composer and download Magento 2

Where to obtain our Magento Access Keys

Before you do anything, be sure to visit https://marketplace.magento.com and create an account. Then you can generate a set of access keys that will allow you to download from the Magento reporistory.

How to install Composer

To install composer, complete the 2 commands [01:56]

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

How to download Magento 2 via Composer

Move to the web directory [02:35]

cd /var/www/html

Switch to the ‘magento’ user [02:58]

su magento

Be sure to empty the web directory before running the below command (I forgot to do this in the video). Use rm index.html to clear out the Ubuntu Default Page.

Run the command to download Magento 2 from the marketplace repository [03:26]

Warning: Don’t miss the full-stop on the end of the command

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

Remember that Username = Public Key and Password = Private Key

Set pre-installation permissions

Update the directory permissions [05:42]

Important Update: Added generated to command

find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} ;

Update the file permissions [05:44]

find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} \;

How to set pre-installation permissions

Update ownership execution permissions [05:46]

chmod u+x bin/magento

How to create a database ready for installing Magento 2

How to create a user and database for Magento 2

Luckily are no commands to reference in this video.

##How to install Magento 2 via the Setup Wizard

How to install Magento using the Setup Wizard

This part of the video doesn’t refer to any commands. However, be sure to access the Wizard via http://123.123.123.123/setup (123.123.123.123 would be your own IP address)

How to setup the post-installation Folder Permissions

The last folder we have to modify here (run whilst still logged in as ‘magento’ user) [06:12]

chmod 555 /var/www/html/app/etc

How to setup Cron Jobs

If you get a prompt asking which editor to use when running the below command, select option 2 (nano). That’s the text editor we’ve been using throughout these videos.

Open our crontab in order to add commands that can be run to automate Magento tasks [06:40]

crontab -e

At the bottom of the file, simply add these 3 lines [07:00]

* * * * * /usr/bin/php /var/www/html/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/html/var/log/magento.cron.log
* * * * * /usr/bin/php /var/www/html/update/cron.php >> /var/www/html/var/log/update.cron.log
* * * * * /usr/bin/php /var/www/html/bin/magento setup:cron:run >> /var/www/html/var/log/setup.cron.log