STEP 1: Setup Server
When you install Magento 2 you will need newer versions of php and mysql/mariadb. So using the latest version of Debian 9 is very helpful, as it includes latest versions of many software packages. After you install I usually run the following commands so the base system is updated.
apt-get update; apt-get upgrade
Next I like get quickly setup and have control panel to manage my server. My control panel is Webmin with Virtualmin. This will get everything installed like mail servers, bind, spam and virus detection. Virtualmin also has easy install of SSL certificates and managing server limits.
wget http://software.virtualmin.com/gpl/scripts/install.sh; chmod +x install.sh; sh ./install.sh
Some people may feel that this installs too much software on their server. In that case you can avoid Virtualmin and just have Webmin installed or avoid a control panel all together.
Now that you have run the Virtualmin script, you need to stop Apache which it uses by default and install nginx.
systemctl stop apache2.service;apt-get install nginx; apt-get install webmin-virtualmin-nginx webmin-virtualmin-nginx-ssl
Next we need to install all the php modules that are required by Magento 2:
apt-get install -y php7.0 libapache2-mod-php7.0 php7.0 php7.0-common php7.0-gd php7.0-mysql php7.0-mcrypt php7.0-curl php7.0-intl php7.0-xsl php7.0-mbstring php7.0-zip php7.0-bcmath php7.0-iconv php7.0-soap
Now you are ready to setup your Magento 2 website. Make sure you have pointed your domain name to your server IP. When you log into Webmin. You will get an error message from the browser as the browser thinks the site is insecure. Just click the Advanced button/link and add an exception or proceed. You can access the Webmin control panel by:
https://domain.com:10000
Once you are logged in with your root username and password,run through the system setup. Now you need to disable Apache and activate Nginx. In the Virtualmin tab, choose System Settings -> Features and Plugins.
Now uncheck:
Apache website, SSL website, DAV Login, Mailman, Protected web directories
Now check:
Nginx website and Nginx SSL website
Now click Create Virtual Server. Once you have setup your domain name as a new server, you will need to click on Server Configuration. Click on Website Options and change PHP script execution mode to FPM. Right above you will see Manage SSL Certificate. If you click on that option, you can choose Let’s Encrypt tab and Request Certificate.
Now go back to the command line and install sudo and assign the new user, which will be the name of your domain in most cases. If in question, check what Virtualmin created as a username for the domain,
apt-get install sudo zsh; usermod -aG sudo domain
STEP 2: Install Magento 2
Now that you have your domain setup in Virtualmin, SSH into that account.
I like to improve my shell by using ZSH with OH-MY-ZSH. This is optional but a great improvement. Just run the following commands:
sudo apt-get install sudo zsh; sh -c “$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)”
Next install composer:
sudo apt-get install composer
To install Magento 2 with composer, you will need to setup an Acess key with your account. Go to:
https://marketplace.magento.com/customer/account/
The public key will be the username and the private key will be the password. Run this composer command:
sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition .
Now you need to configure Nginx for Magento 2. Go back to Webmin and choose Servers. Then click Nginx Webserver and finally Edit Configuration Files. From the drop down choose your server from the list, which should be the last item in the list. Domain.com.conf
Replace the configuration with this one. Make sure you replace domain with your domain name:
upstream fastcgi_backend { # socket # server unix:/var/run/php5-fpm.sock; server unix:/var/run/php/php7.0-fpm.sock; # use tcp connection # server 127.0.0.1:9000; } server { listen 80; server_name www.domain.com; return 301 https://www.domain.com$request_uri; } server { listen 80 reuseport; server_name domain.com; listen 45.76.228.48:443 default ssl; ssl_certificate /home/domain/ssl.combined; ssl_certificate_key /home/domain/ssl.key; set $MAGE_ROOT /home/domain/public_html; set $MAGE_MODE developer; #set $MAGE_MODE default; #set $MAGE_MODE production; include /home/domain/public_html/nginx.conf.sample; fastcgi_read_timeout 3000; }
Click Save and it will test the script. If the test goes fine click Return to virtual hosts list and click Apply Nginx Configuration.
Now go to your domain name and run the Magento 2 install.
FINAL NOTES
I suggest you setup SSH keys and lock down your server to prevent people hacking your site. You can shut off root access at this point and just sudo into root if you need to manage the server.
It is also wise to shut down Webmin when you are using it to manage the server. From the command line, type:
service webmin stop