;

How to Setup NGINX, PHP-FPM, MySQL (LEMP) on Ubuntu 16

Try it in our public cloud & Get $5 Credit
CLAIM NOW

How to Setup NGINX with PHP-FPM and MySQL on Ubuntu 16

PHP, Apache and MySQL are some of the most popular technologies for building commodity web applications. Even so, Apache is a large web server that consumes lots of resources, and those looking for budget hosting might want to replace it with the lighter Nginx.

Getting Started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) with a clean installation of Ubuntu 16.04.

Ubuntu 16.04 LTS is a long-term support release, meaning Canonical will provide updates for 5 years rather than for only 9 months.

Notice: Every command should be run as root user

Tutorial

Let’s get started. When finished, you’ll have a server platform capable of efficiently running any number of PHP and MySQL web apps.

Let’s begin by applying all the latest updates. Not only will this improve security, but it will also avoid potential package download failures in later steps.

apt-get update && apt-get upgrade -y
reboot

Now we’ll install the MySQL database, where the web application will keep all of its data.

apt-get -y install mysql-server mysql-client

You’ll be asked to set a root password. Make this password a strong one and remember it for later. Anyone with this password can access and modify all of your database content.

A few additional steps are needed to further secure the database server. Let’s take care of those now.

mysql_secure_installation

It is now time to install the Nginx web server. Let’s install the package.

apt-get install nginx -y

The Nginx daemon must now be started so it will serve files.

systemctl start nginx.service
systemctl enable nginx.service

While Apache runs PHP scripts directly, Nginx must connect to an external FPM daemon to interpret scripts. That daemon must be installed before it can be used. Let’s install the necessary packages.

apt-get -y install php7.0-fpm php7.0-mysql

Now that the daemon is installed and running, Nginx virtual hosts must be configured to know how to access it, and which paths and files should be run through the FPM interpreter.

nano /etc/nginx/sites-available/default
[...]
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name_,
[...]
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# # With php7.0-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
[...]

Save the virtual host configuration, and restart Nginx to take advantage of the changes.

systemctl restart nginx.service
systemctl enable nginx.service

The php.ini file is where PHP and its modules are configured. It is necessary to change one line in order to successfully run scripts in the FPM daemon.

nano /etc/php/7.0/fpm/php.ini

Find this block in the configuration file:

; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
; cgi.fix_pathinfo=1

Change the last line to this:

; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0

Save and exit. As with Nginx, it is now necessary to restart and enable the FPM daemon so the configuration changes take effect.

systemctl restart php7.0-fpm.service
systemctl enable php7.0-fpm.service

Finally, we’ll create a simple phpinfo page that provides diagnostic information on the installation. This page will later be replaced with a real application.
Enter the document root of the Nginx virtual host.

cd /var/www/html/

Create the file index.php

nano index.php

Now paste the following code:

<?php
phpinfo();
?>

Finally, visit the page you just created by visiting http://your_main_ip. You should see lots of diagnostic details about your PHP installation.

Conclusion

While this tutorial only installs a diagnostic page, you can now replace it with either a site you’ve developed yourself, or any number of third-party applications downloaded off the web. PHP is an easy platform to work with, and these instructions can easily apply to dozens or hundreds of PHP tools. If this guide was helpful to you, kindly share it with others who may also be interested.