You are on page 1of 8

LEMP Server on Ubuntu 16.

04 (Xenial Xerus)
Before You Begin

Complete the Getting Started guide, specifically setting your hostname.


If you are new to Linux systems administration, you may want to consider the introduction to Linux concepts guide and the
Linux administration basics guide.
Update your system:
1 sudo apt-get update && sudo apt-get upgrade

This guide is written for a non-root user. Commands that require elevated privileges are prefixed with
not familiar with the sudo command, see the Linux Users and Groups Guide.

sudo .

If youre

Nginx
Install the Nginx Web Server
To ensure compatability of installation and with future updates, install nginx from the Ubuntu package repository using apt :
1 sudo apt-get install nginx

Configure Nginx Virtual Hosting


Older versions of nginx specified site directories and other information in the main nginx.conf file, but newer versions, such as the
ones included with Ubuntu 16.04, are more compartmentalized. As you read through this section, make note of each files contents
and location so that you are familiar with the structure and know where to go if you need to customize one particular aspect of your
web server.

Nginx uses server directives to specify name-based virtual hosts. Nginx calls these server blocks. All server blocks are contained
within server directives in site files, located in /etc/nginx/sites-available . When activated, these are included in the main nginx
configuration by default.
1. Nginx includes a sample configuration that you may use as a template. To create a new file with a basic server block for
configuration, enter the following command, replacing example.com with your domain:

tail /etc/nginx/sites-available/default -n 13 | cut -c 2- | sudo tee /etc/nginx/sites-available/example.com 1>


/dev/null

2. The command above reads the example server block contained in the last 13 lines of the default site file, cuts out the #
comment symbols, and outputs the result to a new site file.
3. Alternatively, you may manually copy the last section from /etc/nginx/sites-available/default into a new file, /etc/nginx/sitesavailable/example.com . You will have to manually remove the # in front of the relevant lines.
4. You should now have the following server block in the nginx virtual host configuration:
/etc/nginx/sites-available/example.com
1 server {
2
listen 80;
3
listen [::]:80;
4
5
server_name example.com;
6
7
root
/var/www/example.com;
8
index index.html;
9
10
location / {
11
try_files $uri $uri/ =404;
12
}
13 }

Replace example.com with your domain name. If your index page uses PHP, add index.php to the index line:
/etc/nginx/sites-available/example.com
1

index index.html index.php;

5. The nginx example configuration uses /var/www/ as a document root, but Ubuntu uses /var/www/html as a standard.
Additionally, Linode guides encourage the standard practice of using a subdirectory called public_html to exclude web files that
shouldnt be publicly accesible. Update the root directive to match these conventions:
/etc/nginx/sites-available/example.com
1

root

/var/www/html/example.com/public_html;

6. Create the root directory referenced in this configuration, replacing example.com with your domain name:
1 sudo mkdir -p /var/www/html/example.com/public_html

7. Enable the site, disable the default host, and restart the web server:
1 sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled
2 sudo rm /etc/nginx/sites-enabled/default
3 sudo systemctl restart nginx

8. To deactivate a site, simply delete the symbolic link:


1 sudo rm /etc/nginx/sites-enabled/example.com

2 sudo systemctl restart nginx

9. The source file is saved, and the site can be re-enabled at any time by recreating the symbolic link.
If you are using nginx to host more than one site, create multiple virtual host files using the method above.
You may also want to edit the http block in /etc/nginx/nginx.conf , which applies across all sites and allows the following options,
among others:
Hiding HTTP header information using server_tokens
Configuring SSL/TLS settings
Customizing log file paths

Deploy PHP with FastCGI


In order to deploy PHP applications, implement the following PHP-FastCGI solution to allow nginx to properly handle and serve pages
that contain PHP code.
1. Install the required dependencies:
1 sudo apt-get install php7.0-cli php7.0-cgi php7.0-fpm

2. Modify your virtual host configuration to include the location directive as shown below:
/etc/nginx/sites-available/example.com
1 server {
2
listen 80;
3
listen [::]:80;
4
5
server_name example.com;
6

7
8
9
10
11
12
13
14
15
16
17
18
19 }

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

3. Restart the php7.0-fpm and nginx services:


1 sudo systemctl restart php7.0-fpm nginx

Congratulations! You can now deploy PHP scripts with your web server.

Install the MySQL Database Server


The MySQL database engine is one of the leading open-source relational database engines and is a popular database solution for
web-based applications.
1. Install the MySQL server packages and required PHP support for MySQL:
1 sudo apt-get install mysql-server php7.0-mysql

2. During the installation process you will be prompted to set a password for the MySQL root user via an ncurses menu. Choose a
strong password and keep it in a safe place for future reference.
3. Run the mysql_secure_installation script, created to help secure fresh MySQL server installations.
1 sudo mysql_secure_installation

4. Weve already set a strong root password in Step 1, and theres no need to replace it. All other actions should be performed in
most cases.
5. Log in to the MySQL command line interface (CLU) as the root user. When prompted, provide the password set in step 1:
1 mysql -u root -p

6. Create a database and user with permissions for it. Replace web and webuser with appropriate names, and password with a
strong password:
1 CREATE DATABASE web;
2 CREATE USER 'webuser' IDENTIFIED BY 'password';
3 GRANT ALL PRIVILEGES ON web.* TO 'webuser';
4 quit

7. You can now provide the credentials for the web database and the webuser user to your application, which will now be able to
use the database for its purposes. To ensure that PHP will be able to access the MySQL connector your just installed, restart
the PHP service by issue the following command:
1 sudo systemctl restart php7.0-fpm

Optional: Test and Troubleshoot the LEMP Stack


In this section, youll create a test page that shows whether nginx can render PHP and connect to the MySQL database. This can be
helpful in locating the source of an error if one of the elements of your LEMP stack is not communicating with the others.
1. Paste the following code into a new file, phptest.php , in the public_html directory. Modify webuser and password to match the
information entered in the Install the MySQL Database Server section above:
/var/www/html/example.com/public_html/phptest.php
1 <html>
2 <head>
3
<title>PHP Test</title>
4 </head>
5
<body>
6
<?php echo '<p>Hello World</p>';
7
8
// In the variables section below, replace user and password with your own MySQL credentials as created on your
9 server
10
$servername = "localhost";
11
$username = "webuser";
12
$password = "password";
13
14
// Create MySQL connection
15
$conn = mysqli_connect($servername, $username, $password);
16
17
// Check connection - if it fails, output will include the error message
18
if (!$conn) {
19
exit('<p>Connection failed: <p>' . mysqli_connect_error());
20
}
21
echo '<p>Connected successfully</p>';
22
?>
23 </body>

</html>

2. Navigate to example.com/phptest.php from your local machine. If the components of your LEMP stack are working correctly, the
browser will display a Connected successfully message. If not, the output will be an error message.
3. Once youve verified that the stack is working, remove the test file:
1 sudo rm -rf /var/www/html/example.com/public_html/phptest.php

Troubleshooting

If the site does not load at all, check nginxs status and restart if required:
1 systemctl status nginx
2 sudo systemctl restart nginx

If the site loads, but the page it returns is the default nginx page, return to the Configure Nginx Virtual Hosting section above
and check that the root directive matches your example.com/public_html folder.
If the page it returns displays Index of / or has a similar folder tree structure, create a test index.html file or a test file as shown
above.
Congratulations! You now have a fully functional and fully featured LEMP stack for website and application deployment.