Setup Secure Web Server Using Ubuntu 16.04, NGINX, PHP7, MySQL | Install LEMP In 15 Minutes

2
Setup Secure Web Server Using Ubuntu 16.04, Nginx, PHP7, MySQL | Install LEMP In 15 Minutes-techinfoBiT

More than a year ago, I had published a post about setting up a secure web server using Ubuntu 14.04, NGINX, PHP and MySQL, I am glad that the post helped many webmasters and they found the tutorial very easy and quickest way to configure web server manually.

It has been a long while since Ubuntu has released the new LTS the Ubuntu 16.04, which supports the PHP 7, maybe this is the right time to upgrade the web server environment if you haven’t already upgraded yet. If you are already using the Ubuntu 14.04 on your VPS (Virtual Private Server) then you can upgrade it very easily with a single command but if you are not having too many websites hosted on the server then I would recommend doing the fresh installation. I have followed both ways on multiple servers and noticed that the servers with clean Ubuntu 16.04 installation are working more flawlessly.

How To Upgrade Ubuntu 14.04 to Ubuntu 16.04?

This is the easiest way to upgrade the existing Ubuntu 14.04 server to Ubuntu 16.04 and I strongly recommend to take a complete backup (database+files) before proceeding any further.

Follow the following steps to upgrade from Ubuntu 14.04 to Ubuntu 16.04:-

Remember, you are going to mess with your important website data so do not forget to take the backups of all your database and files hosted on that server. Taking backups can save lots of your time and frustration if something went wrong during the upgrade.

  • After taking the backups, open SSH terminal/Putty. Most probably you will see the message to upgrade your system.
  • Execute the command.
do-release-upgrade
  • The upgrade process will start after executing the above command.
  • During the upgrade process, the system will ask you for confirmation multiple times, eg. confirmation to overwrite files and remove old system files etc. read the instructions very carefully and execute the desired command from the options.

Once system upgrade is finished, check and verify that everything is working fine after the system upgrade. In case you are facing any issues or getting any PHP or NGINX related error messages than you should remove the PHP and NGINX installation completely and install it again by following the process mentioned in this article below.

Execute the command below to completely remove the existing  PHP installation.

sudo apt-get remove --auto-remove php7.0
sudo apt-get purge --auto-remove php7.0

Execute the command below to completely remove the existing  NGINX installation.

sudo apt-get purge nginx nginx-common
sudo apt-get autoremove

If you are setting up the Server for the first time then don’t forget the get the free referral credits by following my referral links.

Click Here to get 10 USD free referral credits from DigitalOcean
Click Here to get 20 USD free referral credits from Linode

Click here to follow my old post where I have mentioned in detail about creating the VPS account and accessing the private server using SSH via Terminal or Putty, ignore the server setup process. In that post, I have explained the steps to create the VPS account with DigitalOcean but I would like to recommend the Linode over DigitalOcean because you will get better server resource at the same price.

Linode has started selling the 5 dollars per month server similar to DigitalOcean but with Linode you will get 1GB Memory whereas DigitalOcean is giving only 512 MB RAM, rest of the features are same.

How to Create VPS on Linode?

  • Click here to sign up with my referral link and get 20 USD referral credit on your first signup.
  • Enter your email address, desired username, and password to create your account with Linode
  • Open your email and verify your account by clicking on the verification link you might have received from Linode.
  • Complete the signup process after successful email verification, it will ask you to add your credit card and you may also need to add minimum 5 dollars to verify the credit card, the amount will be used during the monthly invoice payment.
  • After adding the desired amount of credit, Go To Linodes and click on Add a Linode
  • Select your choice of server configuration, options starting from a server with 1GB RAM for 5 USD per month.
  • Select the server location, Personally, I preferred the Frankfurt data centre
  • Click on Add Linode.

Prerequisite To Setup The Server:

  1. You should have approved account with any VPS or Server service providers eg. Linode, DigitalOcean.
  2. Create a server block from host’s dashboard.
  3. Download server configuration file from here. These server configuration files will be required in later steps.
  4. Keep some sets of strong passwords (use strongpasswordgenerator.com)
  5. Download and install FileZilla

Now, lets setup secure web server using Ubuntu 16.04, NGINX, PHP 7 and MySQL, this LEMP installation will not take more than 15 minutes. Just keep following the steps one by one until the end.

To access the server you can use your preferred SSH client like system’s inbuilt terminal for Linux/Mac or Putty/SuperPutty on Windows. Click here to read the detailed about accessing the server via Putty on Windows.

The Mac and Ubuntu users don’t need the Putty, they can make use of the system’s inbuilt terminal, see the following steps:-

  • Type ssh root@IP_address   and hit enter.
  • Type Yes and hit enter to continue with creating the connection.
  • Enter the server’s default temporary root password you might have received from your host.
  • The system may ask you to change the password on the first login so keep a strong password ready, enter the default/temporary password then the new password twice to change the default password.
  • You will be able to access your server as soon as you change the password.

Enable Default Firewall, UFW
Execute the following command to enable and add the firewall UFW to start automatically during system boot-up.

ufw allow ssh
ufw allow http
ufw logging off
ufw enable

Install and Configure Database On Ubuntu Server

The following process will install the MySQL on your VPS, during the installation the system will ask you to enter a password for the root user of MySQL database, so keep a very strong password ready before proceeding.

  • Execute the following command, to update the system.
apt-get update
  • Now, execute the following command to install MySQL
apt-get install mysql-server
  • Type and enter Y to confirm the installation.
  • Enter a very strong password for the root database user.
  • After putting the root database user’s password twice, the installation process will take just a few seconds to finish the installation. Leave a comment if getting any errors!.

Install and Configure PHP 7 on Ubuntu 16.04

  • Type the following command and press enter to execute.
sudo apt-get install php-fpm php-mysql
  • Type and enter Y to confirm the installation.
  • This command will install the latest version of PHP including the MySQL Module.
  • Execute the following command, necessary to run a WordPress website on the server.
sudo apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc

Now you need to edit few files, you can do it in the terminal but doing it with  FileZilla will make the job more easy and convenient since you have already downloaded the configuration files in step 3.

However, you can also do it in terminal using nano /file/path >> select existing codes and replace with code from the file you have downloaded. Open your FileZilla and connect to the server then follow the steps mentioned below.

  • Go to the /etc/php/7.0/fpm and replace the previous php.ini file with the php.ini file which you downloaded in step 3.
  • Now go to /etc/php/7.0/fpm/pool.d and replace the default www.conf file with downloaded www.conf file in step 3.

Install & Configure Lates Official NGINX on Ubuntu 16.04

NGINX is included in the Ubuntu software repository but the version you get from Ubuntu software repository may not be the latest version, so let’s download and configure it from the Official NGINX repository.

Add the official NGINX repository in the Ubuntu’s source list.

  • Open source list by executing the following command
sudo nano /etc/apt/sources.list
  • Use the CTRL+W then CTRL+V to jump to the end of the source list

Add the following two line at the end of the code.

deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx

Setup Secure Web Server Using Ubuntu 16.04, Nginx, PHP7, MySQL | Install LEMP In 15 Minutes-techinfoBiT

  • Now, press CTRL+O to save the file and CTRL+X to exit.
  • Execute the following command to import NGINX public key.
wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

You are good to go with the installation of NGINX from the official repository, run the below commands.

apt-get update
apt-get install nginx
  • Type and enter Y or yes to confirm the installation.
  • After the successful installation of NGINX, open FileZilla and navigate to /etc/nginx/conf.d/ directory.
  • Upload “default.conf” and “drop” file you had downloaded in step 3 to the /etc/nginx/conf.d/ directory. Overwrite the file if asks !.
  • Create www directory and give it proper permission and ownership.
  • Run these commands one by one.
mkdir -p /var/www/
chown nginx:nginx /var/www/
  • Now you need to restart NGINX server and PHP, to do so run this two command one by one.
service nginx restart
sudo systemctl restart php7.0-fpm

If you have executed all the steps as I have mentioned in the tutorial then your server should be ready and you can start hosting the websites on it. In case you are getting any error or you have any query about the article, feel free to leave a comment below in the comment section.

Install phpMyAdmin On Ubuntu 16.04

All database related jobs can be done using terminal but you can install the phpMyAdmin to do it more easily.

  • Run the command below to install the phpMyAdmin on your server.
sudo apt-get update
sudo apt-get install phpmyadmin
  • Type and enter Y to confirm the installation.
  • Once the phpMyAdmin installation finished, execute the following commands.
ln -s /usr/share/phpmyadmin /var/www/
sudo systemctl restart php7.0-fpm
service nginx restart
  • Access the phpMyAdmin using the URL – IP_Address/phpmyadmin

Note: While hosting the website, do not edit the default server block inside default.conf. The default.conf file should have a dummy server block with a domain as domainanme.com pointing to /etc/var/www/ directory to execute any URL starting with IP address.

If you are facing any issues anywhere during the processor you have any queries then just leave a comment below. If you don’t want to mess with your server on your own and you want to get it done by the expert then just drop an email to [email protected].

LEAVE A REPLY

Please enter your comment!
Please enter your name here


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