How to Setup LEMP Stack (Linux, Nginx, MySQL, PHP 7) on Ubuntu 16.04
This guide will teach you how to set-up a Ubuntu 16.04 LTS server running Nginx, MySQL and PHP 7. This configuration is commonly referred to as a LEMP stack.
A LEMP stack is commonly used for hosting your own website, such as a WordPress or Drupal blog.
I recommend that after setting up your LEMP stack you read my guide on how to secure your Linux server.
Step 1: Update packages
Update the list of packages and install the latest versions of the packages that are already installed.
apt-get update && apt-get upgrade -y
Step 2: Install Nginx, MySQL and PHP 7
apt-get install nginx mysql-server php-fpm php-mysql
During this step you’ll be prompted to set a password for the MySQL root user.
Step 3: Perform MySQL installation security
This program allows you to improve the security of your MySQL server by disabling remote root logins, removing the anonymous user account and dropping the test database.
mysql_secure_installation
Step 4: Create default Nginx config file
Edit the default configuration file for Nginx
nano /etc/nginx/sites-available/default
Replace the configuration file with the following server block
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ /\.ht {
deny all;
}
}
Step 5: Configure Nginx to use PHP 7
Edit the default Nginx configuration file by adding the following location block inside of the server block
nano /etc/nginx/sites-available/default
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
Step 6: Customize log file path
This step is optional. I personally like to use the following directory for the log files from Nginx.
Create the log file directory to be used with Nginx
mkdir /var/www/logs
Add access and error log directives to the Nginx config file
nano /etc/nginx/sites-available/default
Add the following into the server block
access_log /var/www/logs/access.log;
error_log /var/www/logs/error.log;
Step 7: Reload Nginx
Test the Nginx configuration file for errors
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
If there’s no errors, reload the configuration file.
service nginx reload
If there’s an error, make sure your /etc/nginx/sites-available/default file looks like the following
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
access_log /var/www/logs/access.log;
error_log /var/www/logs/error.log;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
Step 8: Confirm Nginx is running properly
You can now navigate to your server’s IP address in your web browser. If your Nginx server is running properly, you’ll see the following welcome to Nginx message.
To view your server’s IP address type this command
ip addr show eth0
Step 9: Confirm PHP is working
Create a PHP script file named info.php
nano /var/www/html/info.php
Add the following PHP code to it:
<?php
phpinfo();
?>
If PHP is working properly, you’ll see a page that looks like this
Congratulations! You have succesfully installed a LEMP stack. Did this guide help you? Do you have any questions? Let us know in the comments below!
Quick reference commands
Verify Nginx config is valid
nginx -t
Reload nginx
service nginx reload
Restart nginx
service nginx restart