Howtos / Articles

Most Popular

  • N/A

Installing NGINX, PHP, and MySQL on Ubuntu 12.04 LTS

This “how to” guide will step you through the process of setting up Nginx, PHP, and MySQL on Ubuntu 12.04 LTS.

Nginx (pronounced Engine-X) is a fast & lightweight HTTP and HTTPS web server (it can also act as a reverse proxy, and perform load balancing).

Its small memory footprint requirements make it great for systems with small amounts of memory, such as low end cloud servers. Nginx is great for serving static files to users, and is cable of handling more than 10,000 simultaneous connections, but it lacks the embedded module support for PHP as Apache does.

Nginx can’t process PHP files directly, so Nginx essentially offloads processing of .php files to the PHP FPM package, which in turn passes the interpreted script information back to Nginx, to return back to the end user.

On previous long term support editions of Ubuntu, there were no pre-compiled PHP-FPM packages that you could install out of the box (without adding additional repositories). Thankfully on Ubuntu 12.04 LTS, the PHP-FPM binaries are available in the default Ubuntu repositories.

The how to article below describes installing the usual components for the LAMP stack (Linux, Apache, MySQL, and PHP), however the Apache part is exchanged for Nginx.

Prerequisites

Make sure the apt sources are up to date before installing:

$ sudo apt-get update

Install MySQL

Install MySQL using the following command:

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

You will be asked to enter a “root” password for the MySQL server. Generate a strong password, type it in, and keep note of it for future reference.

Run through the “mysql_secure_installation” script to lock down the MySQL installation.

$ mysql_secure_installation

You will need the password you set when you installed the MySQL server in the previous step. Enter this in when prompted.

You will generally want to answer the prompts with “y”, apart from the first, asking if you want to change the root MySQL password. Assuming you set a strong password in the previous step, you can select “n” for this question.

Install Nginx

Nginx is available in the standard Ubuntu 12.04 repositories, so installing it is simply a matter of running the following command:

$ sudo apt-get install -y nginx

The configuration of Nginx will be done at a later stage.

Install PHP FPM

To use PHP with Nginx, you require the PHP FPM package. Thankfully this is included in the Ubuntu 12.04 repositories.

Install PHP and any other modules that you may require:

$ sudo apt-get -y install php5-fpm php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick \
php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode \
php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-common

Note: You can modify the above command to add/remove individual PHP packages if needed. The php5-fpm package is however required when used with Nginx.

Configure PHP to listen using UNIX sockets

Edit the PHP FPM config file for the www user (which will be used by Nginx).

$ sudo nano /etc/php5/fpm/pool.d/www.conf

Locate the following section:

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9000

Change the “listen” line to be:

listen = /var/run/php5-fpm.sock

Eg. This section should now read:

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /var/run/php5-fpm.sock

Configure Nginx to Pass PHP Files to PHP-FPM

Edit the Nginx configuration file so that it knows what to do with .PHP files:

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

There will be a section with the heading starting with “# pass the PHP scripts to FastCGI server” as seen below:

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
        #       fastcgi_index index.php;
        #       include fastcgi_params;
        #}

Un-comment the lines as shown below, and also add in the “try_files” line as noted in the config example below:

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:

                # add the following line in for added security.
                try_files $uri =403;

                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

Save the file and exit the editor.

Restart PHP FPM

Restart PHP FPM using the following command, to make sure the changes take effect:

$ sudo /etc/init.d/php5-fpm restart

Restart Nginx

Restart the Nginx web server using the following command:

$ sudo service nginx restart

Testing

To test to make sure you have set it all up correctly, create a .php file containing the following PHP code and save it in your website folder and call it test.php (/usr/share/nginx/www/test.php in my example.):

<?PHP
   phpinfo();
?>

Load up your site in a browser, and add the file /test.php to the url, and make sure it comes up with the usual PHP information page, and not just the source code for the .php file you created.

You should now have a working Nginx, PHP, and MySQL stack. Read the Nginx documentation for more detailed configuration options.

The Nginx documentation can be found on the Nginx website at http://nginx.org/



Share this howto article!
  • Facebook
  • Twitter
  • Reddit
  • email

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>