This post is all about basic LEMP(Linux, Nginx, MySql and PHP) from the ground up. It is the traditional way to spin up the server, ssh in and manually install and configure everything from ground up.

By the end of this post, you should be able to have a full functioning LEMP server. Let’s do this!

Getting a server from Digital Ocean

First thing first, you need a server. I have been using DigitalOcean as my hosting provider for 2-3 years now and I love their UI/UX and how simple it is to spin up the server. Here is my Digital Ocean. You will receive $10 in credit and I will get $25 credit in return. This is a win win situation! Once you sign up and add your credit card info, you are now ready to create your very first Droplet.

At the time of this post, I’m starting of as the minimal server so I pick my server spec as below:

Size: $5 per month

Region: Newyork

Image: Ubuntu 14.04×64

Setup and configure the “L”

You should receive an email from Digital Ocean with your server credentials. Now heading over to your terminal and ssh in.

ssh [email protected]_IP_ADDRESS

Let’s get the latest patched software to protect the server.

apt-get update

Setup and configure the “E”

apt-get install nginx

Or if you want to make sure to get the latest version of Nginx

echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/nginx-stable.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C
sudo apt-get update
sudo apt-get install nginx

By default, Nginx will start after the install but if it doesn’t, you can start nginx yourself.

service nginx start

Now that you have Nginx up and running by browsing to your browser(http://SERVER_IP_ADDRESS), you probably want to know how to stop and/or restart eh?

service nginx stop
service nginx restart
service nginx reload

Install and configure the “M”

To install MySql, type these commands. During the installation, MySql will ask you to set a root password so do it!

apt-get install mysql-server php5-mysql
sudo mysql_install_db
/usr/bin/mysql_secure_installation

Install and configure the “P”

Last but not least, the “P”!

apt-get install php5-fpm

Open up php.ini

vi /etc/php5/fpm/php.ini

Need to change cgi.fix_pathinfo=1 to cgi.fix_pathinfo=0 in php.ini.

This is for the security reason. If this number is set to 0, the interpreter will process the exact file path.

Restart php-fpm

service php5-fpm restart

Update nginx config

vi /etc/nginx/sites-available/default

Add these line of code in your default site.

server {
        listen   80;


        root /var/www/domains/default;
        index index.php index.html index.htm;

        server_name example.com;

        location / {
                try_files $uri $uri/ /index.html;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /var/www/domains/default;
        }

        # pass the PHP scripts to FastCGI server listening on the php-fpm socket
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }

}

Create your first working directory

mkdir -p /var/www/domains/ && touch /var/www/domains/default
vi /var/www/domains/default

Update default with the code below

phpinfo(); 

Done!