Check this Out!Visit our social network pages for recent updates!

Installing Nginx, MySQL and PHP (LEMP) on Ubuntu 17.04 / 17.10

This brief tutorial shows students and new users how to install Nginx, MySQL and PHP (LEMP) on Ubuntu 17.04 / 17.10 servers or VPS. LEMP is an acronym for Linux, Nginx (aka engine-x), MySQL and PHP.

LEMP, a stack of opensource applications that powered many of the PHP-based websites and web applications online today. LEMP and LAMP are similar except LAMP uses Apache2 webserver and LEMP uses Nginx.

LEMP is rapidly gaining popularity because it provides speed and scalability.

Continue with the steps below to get LEMP installed.

Step 1: Install / Update Ubuntu Server

LEMP won’t exist without a Linux system. For this post, we’re going to be using Ubuntu as our Linux machine.

This post assumes that you already have Ubuntu server installed and that you have administrative rights to install packages and make changes to the server.

After installing Ubuntu, run the commands below to update the server

sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove

You may have to reboot the systems after running the commands above..

Step 2: Install Nginx Webserver

Now that Ubuntu is updated, run the commands below to install Nginx webserver

sudo apt-get install nginx

After installing Nginx, the commands below can be used to stop, start, and enable Nginx webserver to always start up when the server boots.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

After installing Nginx, test it out by browsing to the server IP address or hostname. When you see the page below, then Nginx is installed and functioning.

nginx default home page test

Step 3: Install MySQL Server

After installing Nginx above, the commands below show you how to install MySQL Server.

sudo apt-get install mysql-server mysql-client

Shortly after continuing with the installation, you’ll be prompted to create and confirm MySQL root user password. Please create a strong password for security.

Ubuntu MySQL password prompt screen

After installing MySQL database server, the commands below show you how to stop, start and enable MySQL to always start up with the system boots.

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

Now that MySQL is installed, run the commands below to secure it.

sudo mysql_secure_installation

Use the guide below to answer the questions when asked.

Enter password for user root: TYPE CURRENT ROOT PASSWORD

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

Estimated strength of the password: 50 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

After that, MySQL should be ready to use. To test whether MySQL is functioning, run the commands below to logon.

sudo mysql -u root -p

Enter the password you created earlier in the post and you should see MySQL welcome message.

Step 4: Install PHP and Related Modules

Lastly, run the commands below to install PHP and related PHP modules.

sudo apt-get install php php-fpm php-mysql

The commands above will install PHP, but may also install Apache2 webserver. Run the commands below to disable Apache2 from starting.

sudo systemctl disable apache2.service

Remove Apache2 default index.html page.

sudo rm /var/www/html/index.html

To run PHP-based websites on Nginx you’ll want to edit its default site configuration file. Run the commands below to create a backup of the default file.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak

Then edit the default file and make the highlighted changes below

sudo nano /etc/nginx/sites-available/default

Then make the highlighted change.

server {
        listen  80 default_server;
        listen [::]:80 default_server;
        server_name  _;
        root /var/www/html;
        index index.php index.html;

        location / {
                try_files $uri $uri/ =404;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;

        location = /50x.html {
                root /var/www/html;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

Restart Nginx

sudo systemctl restart nginx.service

Now, create a test PHP file to test.

sudo nano /var/www/html/phpinfo.php

Add the content below in the file and save.

<?php
phpinfo();
?>

Now browse to the server IP or hostname followed by /phpinfo.php

You should see PHP file as below. This shows that PHP is functioning with Nginx.

PHP default test page for Apache2

Congratulations, you’ve installed LEMP.

Summary:

This post shows how to install Nginx, MySQL and PHP (LEMP) on Ubuntu 17.04 / 17.10. ┬áLEMP is an alternative to LAMP and it’s rapidly gaining popularity because its speed and scalability.

When the above steps are followed, you should be able to quickly run dynamic PHP-based websites and applications.

Enjoy!

You may also like the post below:

Backup MySQL Databases with AutoMySQLBackup on Ubuntu 17.04 / 17.10

Leave a Reply