Nginx on aws ec2

How To Install and Configure Nginx on Amazon ec2 RHEL and Ubuntu Instances

Nginx is a web server like apache. Performance wise, Nginx is considered to be better than apache. Processing request in Nginx is even based as opposed to the spawning new thread model in apache.

In this tutorial, i will explain how to install and configure Nginx on ec2 RHEL and ubuntu instances.

The process for installing Nginx on RHEL , Centos and Amazon Linux is the same.

Instance Setup

1. Launch an RHEL instance using the management console. while launching the instance , configure the security group to allow traffic from HTTP 80 port.

2. Connect the instance using putty.

If you have apache server installed and running already, you have to stop the apache service. If you don’t , it will create conflicts.

Nginx Setup On RHEL

1. In RHEL you cannot download and install Nginx directly. You have to setup the epel ( extra packages for enterprise Linux) repo to install Nginx .

2. Download the Epel repo rpm for RHEL 6 64 bit

3. Install the rpm using the following command

4. Check if the repository is enabled

5. Install Nginx using the following command.

6. Start the Nginx server

7. By default, RHEL allows traffic only to port 22. Even if you have port 80 opened in security groups you won’t be able to access the web server. So you have to accept port 80 connection in the server IP tables.

8. Open IP tables using the following command

9. Add the following line to the file

10. save the file and restart the iptables

10. Use the public IP or the DNS in your browser. You will see a Nginx welcome message.

Setting up Virtual Hosts: (server blocks)

1. Create the website folder and a public folder to put the static assets inside /var/www. Here am going to give the name as comtechies.com. The folder name can be any user defined name.

2. Create an index.html inside the public folder and put any basic HTML page.

3. Change the owner for your website files to have read-write permissions.

4. Allow read access to /var/www for everyone to access the website publicly.

5. To add a virtual host, edit the virtual.conf file using the following command

6. Make the configuration changes like the following and save the file.

Above, for server name I have put the public DNS of my amazon machine. Since I don’t have a custom domain , I am testing the virtual host setting using the public DNS.

7. Restart the Nginx server

8. If you use the public DNS , the server will throw the following error when you restart the Nginx server.

9. Since the DNS name is pretty long , you have to add the server name bucket size to 128 to the Nginx.conf file.
10. Open the nginx.conf file

11. Add the following to the HTTP part of the file.

12. If you restart the Nginx server, you won’t be getting the bucket size error.

13. Check the virtual host setting by visiting the public DNS or the IP. You will see the demo HTML page you put in the /var/www/comtechies.com/public folder.

14. If you want to add more websites, you just have to add one more virtual host configuration like we did in the virtual.conf file.

Load balancing using Nginx

You can use Nginx as a load balancer to balance the load between server fleets. Nginx proxies the incoming requests and sends it to the backend servers. To configure Nginx as a load balancer, you have to add two blocks of code to the nginx.conf file.

1. Open nginx.conf file

2. Add the upstream group under the HTTP section. The upstream group is the group of servers which comes under the load balancer. You can give any user defined name for the group. Here am going to give the name as “web_fleet”.

Note: this configuration should be present in the HTTP section of nginx.conf file.

3. Now you have to set the vhost configuration to receive traffic from a particular domain name and route it to the upstream servers. Add the following lines followed by the upstream block.

Here the server name I have given is the public DNS of the Nginx EC2 server. Here is where you provide your custom domain. For testing purpose, you can use the public DNS of your EC2 server.

4. Save the nginx.conf file and restart the Nginx server

5. Now, if you access your nginx server using the public DNS, the request will be routed to the backend server fleet present in the upstream block. You can test this by installing apache server in any server and give the servers IP address inside the upstream block.

There are many other parameters and setting associated with the load balancing configuration. You can check the official Nginx documentation for more clarification.

Ubuntu Setup

1. launch an ubuntu instance with port 80 open in the security group.

2. Connect the instance using putty

3. Issue the following command to become the root user.

4. Install Nginx from the apt repository

5. After installation starts the Nginx service using any one of the following commands.

6. Copy and paste the public IP or the DNS name of your Ubuntu server and hit enter. You will see a welcome message from the Nginx server.

Setting up server blocks (virtual hosts):

If you want to host more than one website on your server ,you should set up virtual hosts for each website.

1. Create a directory for your website inside /var/www folder.

The public folder will contain all the static pages of your website.
Yoursite.com should be the actual custom domain you point to the server IP.
2. Change the owner to ubuntu using the following command.

3. Change the read permission of the www folder, so that everyone can access the website.

4.Put a sample html page inside the public folder

Enter any sample HTML code

5.Copy the default virtual host file to yoursite.com

6. Open the yoursite.com file under sites-available folder and copy the following contents.

7. create a link between sites-available and sites-enabled yoursite.com file

8. restart the Nginx server

Load balancing configurations are the same as RHEL for Ubuntu.

Share the article and leave a comment for queries.

Related

ububtu server gui on aws ec2

Amazon EC2 Linux servers do not come with GUI, all the operations have to be done using ssh client like putty. So…

Read more

 Plesk:Plesk is a control panel solution for system administrators and webmasters. plesk is a product of…

Read more
Password Authentication For AWS ec2 Instances

By default, ec2 instances don't have password authentication. You have to use the private key to connect to the…

Read more
About

Cloud Computing Tutorial Blog