Setting up hosting environment on Amazon EC2 with NodeJS, Nginx and php-fpm

Posted on Sep 24th, 2015

From last 4 years, I was using Godaddy for hosting my site and my projects but lately figured out there cPanel is not very secure. So I switched to Amazon EC2 Linux hosting and its awesome. It fast, secure and gives you full control to your ecosystem.

My requirement was not to host one single website but to host multiple websites with one instance.

Once you have setup your instance, you will have to ssh to your account and install necessary libraries to setup your hosting environment.

$ ssh id_rsa.pem [email protected]

id_rsa.pem is the permission file that you created while setting up your instance.

After connecting, it will ask you to add the new host in to known_hosts. Say yes. You will have to generate a public key after this and add this key to EC2's authorized_keys.

$ ssh-keygen -t rsa -b 4096 -C "[email protected]"

Save the file in your local system at ~/.ssh or any safe folder and name it as

cat ~/.ssh/ | ssh -i [email protected]"cat >> .ssh/authorized_keys"

The above command will automatically add the key to EC2's authorized keys. Now you should be able to connect without .pem key

$ [email protected]

Next, we will update the system and install few libraries which are prerequistes for nodejs.

$ sudo yum update
$ sudo yum install gcc-c++ make
$ sudo yum install openssl-devel

Installing NodeJS

$ wget
$ tar -xvf node-v4.0.0.tar.gz
$ cd node-v4.0.0
$ ./configure && make && sudo make install

At this point, node should be working fine. You can type npm  and see if its installed.

Installing Nginx and PHP-FPM

$ sudo yum install -y nginx php-fpm

After installing Nginx, you will find its configuration file here:


And all your hosting files will stay in the below location:


Now everytime you host a new website, its not a good practice to touch nginx.conf and add your server information. Instead we are going to create a folder  /var/www/hosts and keep all our custom configuration files here. So if you have a new website, you just create a new conf file for that and nginx will automatically detect that. To do this, we need to tell nginx.conf file to scan this new directory. So open up this file:

$ sudo vim /etc/nginx/nginx.conf

And just after

include /etc/nginx/conf.d/*.conf;

add the below line 

include /var/www/hosts/*.conf;

Now create the hosts folder.

$ mkdir /var/www/hosts

Create a configuration file for one of your website in this folder.

Lets name it as

server {
    listen       80;

    root   /var/www/html/;
    #autoindex on;
    index index.php index.html;

    #access_log  logs/host.access.log  main;

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;

    location ~ \.php$ {

        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;


Restart the server:

$ sudo service nginx restart

All set, lets create the folder inside /var/www/html

$ mkdir /var/www/html/
$ cd /var/www/html/
$ vim index.php

//Add some content inside index.php


Save the file. Because we dont have as our real domain, we will have to edit our local machine's hosts file and add an entry.

In your local machine, edit your hosts file:

Windows: C:/Windows/System32/Drivers/etc/hosts

Mac: /etc/hosts

And add the below entry: //that ip is your amazon's elastic ip

Now visit and you should be able to see phpinfo information.

Algorithm - QuickSort in JavascriptMysql sorting on multiple columns with IF-ELSE

100% Complete