Skip to main content

Configuring Nginx as Webserver

Hello,


Thanks for visiting RvKmR.blogspot.in


In this post I am going explain about how to host your websites with nginx. Nginx is web server or we can use it as reverse proxy server. In this post we are discussing Nginx as web server.

Let's say I have two web sites that I wish to host with nginx. example.com and test.com are those two websites to be hosted.

Environment : Vagrant virtual machine(precise32)
___________________________________________________________________________________
1. Installing nginx and required libraries to test:

We can install it from vagrant provision script or manually from machine itself.
I am installing from manually from machine.

vagrant@precise32:~$ sudo apt-get install nginx wget links curl

2. Create website directories in document root and index page for those websites:

sudo mkdir -p /usr/share/nginx/www/example.com/html
sudo mkdir -p /usr/share/nginx/www/test.com/html 

I have created simple html page in above document root of respective domain as below.
for example.com

<html>
<h1>This is example.com</h1>
</html>

for test.com

<html>
<h1>This is test.com</h1>
</html>

3. Change ownership of those document root and files to executable(755)

sudo chmod -R 755 /usr/share/nginx/ 


4. Create server block for example.com and test.com:

Copy default block for both example.com and test.com 

vagrant@precise32:~$ sudo cp /etc/nginx/sites-available/default  /etc/nginx/sites-available/example.com
vagrant@precise32:~$ sudo cp /etc/nginx/sites-available/default  /etc/nginx/sites-available/test.com

5. Create symbolic links of those files in /etc/nginx/sites-enabled/

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/   
sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/  

6. Set appropriate values of directives listen, root(document root), server_name  in both of those templates.

listen : This directive specifies on which port server will receive requests from client. default is 80.
root : This directives specifies where the respective website is available.
server_name : This directive is used to set alias to domain.

There are various other directives available, but for testing these are sufficient. All of those directives must end with semicolon(;) because this indicate end of that directives and helps to trace where exactly error occurred.

After this our server block for example.com will looks like as :

Server {
            listen 80 default_server;
            listen [::]:80 default_server ipv6only=on;

            root /usr/share/nginx/www/example.com/html;
            index index.html index.htm;

           server_name example.com www.example.com;

           location /{
                          try_files $uri $uri/=404;
           }
}

7. Repeat above steps for test.com also and server block for test.com looks like as :

Server {
            listen 80;
            listen [::]:80 ipv6only=on;

            root /usr/share/nginx/www/test.com/html;
            index index.html index.htm;

           server_name example.com www.example.com;

           location /{
                          try_files $uri $uri/=404;
           }
}

Updating hosts file for domain example.com and test.com as:

vagrant@precise32:~$ cat /etc/hosts
127.0.0.1    localhost
127.0.1.1    precise32
127.0.0.1    example.com
127.0.0.1    test.com

Make sure firewall is stopped or port 80 is open.
Now you can test those domain from browser or command line tool curl, links etc. 
vagrant@precise32:~$ curl example.com
<html>
<h1>this is example domain </h1>
</html>
vagrant@precise32:~$ curl test.com
<html>
<h1>this is test domain</h1>
</html>
vagrant@precise32:~$

Comments

Popular posts from this blog

Wheel Users in Linux

Hello, Thanks for visiting RvKmR.blogspot.in In this blog post, I am explaining about Wheel users in Linux. Red Hat release that I am working on is as below.  [ravi@localhost vagrant]$ cat /etc/redhat-release CentOS release 6.9 (Final) An Linux system can have many users like System users, normal users, and admin (root) user. Its common sens that we have is not to share root user credentials with other user, but here is case that we wish to run administrative command by normal user. In that case we need to aware about wheel users group. Let's first understand what is wheel user in Linux and why we needed it. Wheel is one of Linux user group that allow members of that group to run administrative commands those need root access of system, Wheel user facilitate to do that with sudo (superuser do ) privileges. Lets do it on terminal : Add an user and set password [root@localhost vagrant]# useradd ravi [root@localhost vagrant]# passwd rav

IT Infrastructure Monitoring

Hello, Thanks for visiting RvKmR.blogspot.in IT Infrastructure monitoring its not just words, it means a lot in the information technology world. I expect an IT professional knows what it means. In a simple word, I can say Compute, Application, and network, etc. OK, let's start why we need IT infrastructure monitoring. Monitoring IT infrastructure allows us to take preemptive action to be undertaken towards a potential problem before they affect your business. OR at least reduce time to restore your infrastructure as soon as an incident occurs. Further monitoring of IT infrastructure is divided into two categories Agent-based and agentless. An agent is a software or script installed on machines that to be monitored so that it will send information to monitoring systems. Having an agent inside the machine uses system resources, which in turn raises maintenance cost. Whereas the agentless tool is little or no impact on monitored machines as an agent is not installed on

Understanding Linux Cluster

Hello Thanks for visiting RvKmR's blogs. So, What means of clusters and why we need them. Cluster is set of servers or say computers working together to achieve single goal or a task. Now you may have idea why we need cluster.  Types of Cluster: High Availability Cluster / HA Cluster  HA Cluster again grouped in two types. Active-Active Cluster Active-Passive Cluster In Active-Active Cluster Service is running on all nodes in cluster. That means all nodes serving independently. In Active-Passive Cluster Service is running on one of node from cluster and other nodes in passive state. If primary node fails then only other node in cluster will start service.   Storage Cluster In this cluster type, all members provides a single cluster file system that can be accessed by different servers. These file system can be used to read-write data simultaneously. EX: GFS2  OK, Then what is High Availability. This type of cluster provides high availability of serv