How to set up a web server for Magento 2.2

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

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:


Newsletter Subscription

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]

Important Update: Added php7.0-soap to command

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

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]

Important Update: Added generated to command

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

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

Comments

  1. Matthias

    Hi Craig,

    fantastic videos and explanation. That really speeds things up!

    right now I am getting the error:

    Forbidden
    You don’t have permission to access / on this server.
    Apache Server at xxx.xxx.xx Port 443

    Do I have some issues with directory permissions or is it a apache config problem?

    Thanks and nice Weekend 🙂

    1. Craig Nammontri Post author

      Hi Matthias, thanks for your positive comment.

      This guide didn’t cover 443/SSL so I’m assuming you’ve made your own tweaks for your purposes. If you’re receiving the Permission Error when testing your LAMP stack then it’s definitely a mis-step during the setup, so I’d go back over video #3.

      I can’t think of a reason why Magento would cause this issue during this setup, because unless you engaged SSL during the Web Setup Wizard then any attempt to visit a page via port 443 would redirect you to 80 instead.

      Please, let us know how you get on.

  2. Alexander Sandberg

    I have a question regarding secutity, what is the recommended things to do to minimize the risk of a data breach? Aka what do I as IT need to make sure I know before doing this as a live server? Any recommendations? I’m thinking digitalocean obviously have security on the network side but I still need to handle the vm. I’m asking because I’m a a bit nervous to be handeling userdata.

    1. Craig Nammontri Post author

      Hi Alexander. It’s refreshing to see someone discuss security, as it’s so important in a production (live) environment to protect both you and your customers. I won’t give you a comprehensive how-to or anything, but I’m just going to real off some points from the top of my head regarding lite-usage of WEB SERVERS:

      • Firewalls: Restrict SSH/SFTP/FTP Access to a select few IP Addresses. If you don’t have a Static IP address with your ISP, then get one
      • Usernames: Disable all default usernames that maybe auto-generated (e.g. “admin”, “root”) and replace them with something obscure
      • Usernames: Don’t use obvious usernames like “John” or “Jdoe”
      • Passwords: This one feels obvious, but don’t use dictionary words
      • Passwords: Enforce using SSH Keys to replace your passwords
      • Users: Don’t use your ROOT user to access and work on your web files. Create server users specific for a purpose with restricted write/read access to specific working directories. This is a great one, because if this user is compromised, it helps to limit damage
      • Access: Only log into the server from computers you own

      In most cases, the firewall will save you from brute-force attacks. But also knowing I have regular backups helps me sleep at night. For more advice on Magento (Frontend) Security, you should checkout this video I did last year: 25 Ultimate Security Checks

    1. Craig Nammontri Post author

      Well spotted, thank you. I didn’t realise they added that feature to 2.2. If anyone else is reading this, make sure you’re logged in as the ‘Magento file system owner’ before executing that command.

  3. soulsako

    magento@ubuntu-s-1vcpu-2gb-lon1-01:/var/www/html$ find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \;
    find: ‘var’: No such file or directory
    find: ‘generated’: No such file or directory
    find: ‘vendor’: No such file or directory
    find: ‘pub/static’: No such file or directory
    find: ‘pub/media’: No such file or directory
    find: ‘app/etc’: No such file or directory
    Hi people, can anybody please help me set pre-installation permissions. i am getting this error.

    1. Craig Nammontri Post author

      Hi. The “find” command is run post-installation. If you’re getting “No such file or directory” then this would suggest Magento has not been installed in the “/var/www/html” directory. I would highly recommend checking the steps again.

      1. soulsako

        magento@ubuntu-s-1vcpu-2gb-lon1-01:/var/www/html$ ls -alh
        total 12K
        drwxr-xr-x 3 magento www-data 4.0K Mar 8 04:46 .
        drwxr-xr-x 3 root root 4.0K Mar 8 03:20 ..
        drwxr-xr-x 13 magento www-data 4.0K Mar 8 04:51 project-community-edition

        I have looked into the directory and there is project-community-edition there.

        1. Craig Nammontri Post author

          The folder “project-community-edition” should not exist. The files you need are probably in that folder. Move the contents of that folder into “/var/www/html”.

          1. soulsako

            The previous problem is fixed but i am getting this new error whilst installing magneto using the setup wizard
            SQLSTATE[HY000] [1045] Access denied for user ‘magento’@’localhost’ (using password: YES)
            please help.

          2. Craig Nammontri Post author

            Go back over episode 6. You either noted the username/password incorrectly when creating your user or did not click “Create database with same name and grant all privileges” before pressing “Go”.

    2. Derickt

      @soulsako I know this is 3 months ago (so im posting for others that might run into this), but have a look at your installation and check for any errors, also the command syntax for composer might have changed as you can specify an installation directory, if you don’t it defaults to ‘project-community-edition’
      composer create-project –repository-url=https://repo.magento.com/ magento/project-community-edition

      1. Derickt

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

  4. Ivan Tran

    This is a great tutorials. I successfully installed magento 2.2.2 on the ubuntu server.
    Now, I do fresh install magento 2.2.3 for another server but I got 404 error in admin page and front end home page missing styling and image. Do you know how to fix that? (I already tried clean cached and do this command $ php bin/magento setup:static-content:deploy and modify apache2 config
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    allow from all

    but still the same. Do you have any ideas?
    Thanks

    1. Craig Nammontri Post author

      Hi Ivan. I’m glad you’ve found these videos of use. The problem you describe matches the exact symptoms of apache rewrite not being configured correctly. Have you tried what Ollie H did in his comment below this one?

      Regards,
      Craig

  5. Ollie H

    Hi Craig,

    You are an absolute champ, thanks you from the bottom of my heart for sharing your hard work and working setup, God knows I burnt out my fuse until I came across your article.

    One thing to note, I got everything smooth until the successful installation, when visiting the site it didn’t have and css, and admin page threw a 404 error at me, I fixed it with these commands:

    #a2dismod rewrite
    #sudo service apache2 restart
    #a2enmod rewrite
    #sudo service apache2 restart

    Strange since it was already enabled!

    Albeit, you are a true gentleman, God bless you,
    Ollie.

    1. Craig Nammontri Post author

      Hi Ollie,

      You’re more than welcome. I can’t take all the credit as it’s the community that helps me stay on top of the changes as Magento gets updates.

      I’m glad you were able to solve your issue, and thank you for posting your problem/solution on here for others to learn from in case they face the same issues.

      Good luck with your Magento project, and I hope to see you around here again (Or on the YouTube Channel) in the future 🙂

  6. Fabio

    The best tutorial on the internet!

    You Sir, are a hero!

    1. Craig Nammontri Post author

      You’re welcome, Fabio 🙂 Team effort though because the community lets me know if the instructions need changing when new updates come out. Good luck with your project!

  7. Daniel

    Hi this is super.
    only thing i cant fix is this permission check i have installed magento 2.2.2

    File Permission Check
    1 file permission not met. Hide detail

    The best way to resolve this is to allow write permissions for files in the following Magento directories and subdirectories. The exact fix depends on your server, your host, and other system variables.
    For help, see our File Permission Help or call your hosting provider.

    “/var/www/html/app/etc” – Writable.
    “/var/www/html/var” – Writable.
    “/var/www/html/pub/media” – Writable.
    “/var/www/html/pub/static” – Writable.
    “/var/www/html/generated” – Not writable, change the permissions. Show details
    * – In some cases, you might have two PHP configuration files: one for the PHP command line and for the web server. If so, make the change in both php.ini files. For details, see the php.ini reference.

    1. Craig Nammontri Post author

      Thanks for sharing that. Depending on how you set up your users, your cron user must be the same as the file owner. In this guide, the cron is run as www-data and the file/folder owners are also www-data.

      This guide uses the two users setup. Check step 5 in the Official Magento DevDocs to ensure folders and permissions are correct.

  8. Remco

    Thank you for this tutorial. I managed to setup a test server.
    Some minor issues though. Some plugins for PHP were missing (Curl, intl and zip). I was able to install them with “sudo apt install php-missing_plugin”
    And opening the webbrowser on my server ip lead me to a white web page.
    I needed to open “my-server-ip/setup” to start the setup wizard.
    Again thanks for the tutorial.

    1. Remco

      small add-on:
      the map “generated” needed to change to the correct permissions as well.

    2. Craig Nammontri Post author

      Thank you for the information. I’ll be going through these steps again soon as a lot seems to have changed since I wrote this for v2.1.5. I really appreciate you sharing this with myself and the community.

  9. Spike

    great article.
    I ‘ve engage an cosmetic issue during composer installation says missing extension of PHP-soap
    by adding sudo apt-get install php7.0-soap and reinstall the issue was resolved,

  10. Ömür Yanıkoğlu

    Helo,

    Your training really helped me. I have never encountered an error. There’s just one thing. The current version is 2.2.0 but the theme I want to use is compatible with 2.1.9. For this reason, I needed to use the following code:

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

    Thank you so much.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.