Install Nginx Ubuntu for a Dynamic Webpage

install nginx ubuntuWhen you install nginx and the rest of the LEMP stack on Ubuntu, your system will become more dynamic and be able to offer more to site visitors. The acronym LEMP describes the operating system Linux with the web server nginx.

Specifically, LEMP is a software stack used to serve dynamic web applications and web pages. It includes nginx, Linux, PHP and MySQL. The dynamic processing is handled by PHP and the back end data is stored in MySQL.

This guide demonstrates how to install nginx and the rest of the LEMP stack on an Ubuntu server 14.04, which you read more about in this Ubuntu article. 

Requirements

Before completing this tutorial, you will need a non-root, regular user account with sudo privileges on your server. On the initial server setup of Ubuntu 14.04, you can learn how to set up this account. Once it is available, use your username to sign into the server. Here is an Ubuntu course that shows you exactly how to do this.

Installing the Nginx Web Server

We are going to employ the efficient, modern web server nginx to display pages on the web to your website visitors. For this procedure, all the software will be coming directly from the default Ubuntu package repositories. This means that to complete the installation, you can use the apt package management suite. Begin by updating the package local index:

Ubuntu1

Nginx is configured to being running once you install it in the 14.04 version. Access your web browser’s IP public address or server domain name to test if the server is up and running. If you don’t know the server’s IP public address or the domain name of your server, type this into your terminal to find it:

Ubuntu2

Or you can try typing:

Ubuntu3

Once you see the ‘Welcome to Nginx’ page, the nginx web server is successfully working and installed.

Manage Site Data by Installing MySQL

Now that your web server is up and running, you need to install the database management system MySQL to manage and store data for your site just as this MySQL course shows you. You can do this by typing:

Ubuntu4

You will be asked to supply an administrative or root password for using within the system of MySQL. Now that you have installed the database software, you will need to complete its configuration. You will need to begin telling MySQL to generate the directory’s structure needed to store information and database. You can do this when you type:

Ubuntu5

Modifying insecure defaults by running a simple script for security is next;

Ubuntu  6

You will need to enter the root password MySQL which you had chosen earlier. This time, it asks you if you want the password changed. If you are happy with your root password for MySQL, type N. Next, you will get a prompt to remove some databases and test users. Through these prompts, just hit ‘ENTER’ to remove the default unsafe settings.

MySQL is then ready to go once the script has been run.

PHP Processor Configuration

Create a more secure setup now that you have the components of PHP installed, which you can also learn how to do with this PHP course. Using root privileges, open the main php5-fpm configuration file:

Ubuntu7

In this file you are looking for the cgi.fix_pathinfo parameter. Using a semi-colon, this will be commented out and by default, set to 1.

This setting is very insecure since it tells PHP to try executing the closest file it can find if the file PHP does not exactly match. Basically, this allows users to craft requests in PHP in ways that would let them write scripts that are otherwise not allowed. To change this condition, set it to ‘0’ and uncomment the line:

Ubuntu8

When you are finished, save and close the file. Now, type this line to restart your PHP processor:

Ubuntu  9

This implements the changes you made.

Configure Nginx to Use PHP

At this point you have installed all the required components. The only other change you need to make with the configuration is for dynamic content. You can do this by telling nginx t use the PHP processor. This is done on the level of the server block. Type this to open the nginx server default file configuration:

Ubuntu10

At the moment the nginx default serve file looks like this with the comments removed:

Ubuntu11

For your website, you will need to make some file changes:

  • Modify the directive’s server_name to point to your public IP address or server domain name.
  • To allow PHP file indexes to be served when you request a directory, add the option index.php as the index directive’s first value.
  • The actual file configuration includes some line that have been commented out defining routines of error processing. These will need to be uncommented to include these functionalities.
  • For the actual processing of PHP, uncomment portions of another section. To make sure nginx does not pass out a bad request to the processor PHP, add the directive try_files.

In the text below, the changes you need to make are in red:

Ubuntu  11

Ubuntu12

You can save and close the file once these changes have been made. To save the necessary changes, restart nginx.

Ubuntu13

Test Configuration

Now that your LEMP stack is set up completely, test to ensure that nginx can give .php files to your processor. Create a test PHP file in your root document to do this. Open a new info.php file in your text editor within your root document:

Ubuntu14

This is a valid code for PHP that returns server information. You can type this into the new file:

Ubuntu15

After you are finished, save and close the file.

At this point, you can visit web browser pages when you visit your server’s public IP address or domain name followed by /info. php:

Ubuntu16

You will now see a page with server information that has been generated by PHP. Once this page appears, you have successfully set up nginx with PHP processing.

On your Ubuntu server 14.04, you should now have a LEMP stack configured, giving you a flexible foundation for serving content on the web to your site visitors, which you can learn more about by taking this Ubuntu training course.