VPS Notes

Beginner Tutorial – Installing NGINX, PHP, MySQL (LEMP Stack) for Dynamic Websites

The LAMP Stack (Linux + Apache + MySQL + PHP) is popular setup for hosting dynamic websites. I prefer to use LEMP (Linux + Nginx + MySQL + PHP) because I find it easier to install and more friendly to system resources.

Install NGINX

Refer to my previous post to prepare a Linux server and install NGINX. Below is a review:
Prepare the Linux server:
$ apt-get -y update
$ apt-get -q -y upgrade
$ apt-get -q -y remove --purge portmap apache2* bind9 samba* nscd sendmail* whoopsie*
$ apt-get -q -y autoremove
$ apt-get -y install locate nano htop less zip gzip unzip less git-core

Install NGINX:

$ apt-get -y install nginx
$ sed -i "/worker_processes/cworker_processes `nproc`;"  /etc/nginx/nginx.conf
$ rm /etc/nginx/sites-available/default

Install PHP

Install PHP and some common modules:

$ apt-get install -q -y --force-yes php5 php5-fpm php-pear php5-common php5-mcrypt php5-mysql php5-cli php5-gd php5-cgi php5-curl

Let's make sure than PHP-FPM (The Process Manager of PHP) is listening at the default port 9000. This is usually configured like this, but we need to be sure.

$ sed -i "/listen *=/clisten = 127.0.0.1:9000"  /etc/php5/fpm/pool.d/www.conf

Install MySQL

This is easiest. Just issue the command and provide the password of database user "root":

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

Create a Simple PHP Website

This step is as simple as the previous post about creating a static website.
Create the folder that will contain the web files:
$ mkdir -p /var/www/vpsnotes.com

Create a php page
$ echo "<?php phpinfo(); ?>" > /var/www/vpsnotes.com/index.php

Configure NGINX for our PHP based website. Create the config file: /etc/nginx/sites-enabled/vpsnotes.conf (or any name you like, all files in sites-enabled folder will be scanned)
$ nano /etc/nginx/sites-enabled/vpsnotes.conf

And use the following contents:

server {
    listen 80;
    server_name vpsnotes.com www.vpsnotes.com;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    root /var/www/vpsnotes.com;

index index.php index.htm index.html; location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } }

Restart NGINX and PHP
$ service nginx restart
$ service php5-fpm restart

Now you can load the PHP site on your browser. Here is the output in my computer:
v03

TLDR

From scratch, here is shorter version using a script.
Download the script:
$ wget https://raw2.github.com/vpsnotes/setup/master/setup.sh

Clean UP and install NGINX, PHP, and MySQL
$ bash setup.sh init
$ bash setup.sh install_nginx
$ bash setup.sh install_php
$ bash setup.sh install_mysql

Install a php website
$ bash setup.sh add_php_website vpsnotes.com

Restart NGINX and PHP
$ service nginx restart
$ service php5-fpm restart

Remarks

Now that you have a LEMP stack, you can install popular PHP programs such as Wordpress.
Tags: mysql, nginx, php, web server