How to set up a web server for Magento 2

How to set up a web server for Magento 2 Reading Time: 6 minutes
Share on Pinterest

How to set up a web server for Magento 2

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

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

Create yourself a user [04:23]

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

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]

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

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

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]

Then we turn on the firewall [08:26]


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]

Install Apache [01:46]

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

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

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

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

Check for any errors [04:02]

Enable Apache rewrite [04:40]

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

Enable Apache through the firewall [05:28]

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]

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

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]

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

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

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

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]

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

Enable 2 phpMyAdmin required extensions [11:42]

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

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]

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]

How to set folder permissions

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


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.magent.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]

How to download Magento 2 via Composer

Move to the web directory [02:35]

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

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

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

Remember that Username = Public Key and Password = Private Key

Set pre-installation permissions

Update the directory permissions [05:42]

Update the file permissions [05:44]

How to set pre-installation permissions

Update ownership execution permissions [05:46]


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

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]

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]

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


Summary on how to create a web server for Magento 2

I hope that you found the video and article useful. Remember, that this article is not to be used without watching the videos. Let me know in the comments below how you get on.

Leave a comment below

 

Craig Nammontri

Craig Nammontri

I've spent a chunk of my career working in eCommerce for a FTSE 100 company where I've worked in Web Development/Design, SEO and assisted in running an eCommerce business. If I can help anyone out, then I'll try my best.
Craig Nammontri

Related Post

Share on Pinterest

Your email address will not be published. Required fields are marked *