Nginx – webserver

Nginx – webserver

What is NGINX?

NGINX (pronounced Engine ex) was released for production in 2004 and is rapidly becoming a popular alternative to the traditional Apache web server suite. It features an event-driven design, which can make better use of today’s computer hardware than Apache’s process-driven design. Because of this, NGINX is often seen as the “faster” alternative to Apache, being able to handle a higher load of concurrent connections while using less resources. There are many comparisons out there between Apache and NGINX; we’ll leave the debate up to the community. But here are a few pointers that outline the key reasons to choose Apache versus NGINX. At the end of the day, the choice of the web server platform is entirely dependent on what you’re doing with the server.

Here’s a good rule of thumb: If you want to run ONE site at lightning speed on an advanced configuration, NGINX is probably the server for you. If you want to run MANY sites with easy configuration and flexibility, Apache is still your bread and butter.

At the end of the day, both are a good fit for most sites. Apache is included with all major Linux distributions and requires much less configuration. However, most benchmarks have clocked NGINX at serving websites faster. You can also see some configurations that run both — it’s all up to you as the admin

Installing NGINX on CentOS 6

Every major Linux distribution comes packaged with Apache by default; it’s literally integrated into the OS by now (similar to how Windows comes packaged with IIS natively). However, since we’re setting up a dedicated space for NGINX, it’s possible that the existing Apache configuration can cause problems when NGINX is put in its place. What we’re going to do is turn Apache off, then configure Apache so that it does not start upon server reboot.

Turning Apache off on a server with live sites will bring those sites down; act accordingly.

1. Log into your server via SSH, then get to the root user by running:

sudo su –

Note: We’ll remain as the root user for the remainder of the article.

2. Shut Apache down. This will bring down any current websites that are hosted on the server.

service httpd stop

3. Now we need to remove Apache from the boot cycle, so that it doesn’t try to start up during server boot.

chkconfig httpd off

Apache is now fully shut down, and won’t be starting up again until we say so.

Note: If you have buyer’s remorse later on about NGINX, and want Apache to start on boot again, you can easily correct this previous command by running:

chkconfig httpd on

Now that Apache is riding off into the sunset, we can start to install NGINX.

4. Add the EPEL-Release yum repository, which will have NGINX for us:

yum install epel-release

5. Now that our repository is installed on the server, we can use yum to install NGINX:

yum -y install nginx

6. Start NGINX:

service nginx start

7. Then configure the server to start NGINX upon reboot:

chkconfig nginx on

You should now be able to see an NGINX test page by going to http://1.2.3.4, using your IP address for your server.

Configuring NGINX to serve for your domain

Alrighty, we’ve switched from the Apache schooner to the NGINX steamboat. Now it’s time to get it working for your domain.

Create a new user for the webspace

Before doing anything, we need to create a UNIX user for your webspace.

1. Type the following command to create your user:

useradd <username>

To demonstrate, I’ll add my user nginxsite:

useradd nginxsite

2. Give this user a password with the following:

passwd <username>

//for our example

passwd nginxsite

3. Set the password for this user.

Your characters won’t register in the terminal when you type. That’s fine; it’s just Linux protecting you by not logging the password entry. Follow safe password practices.

Your user should now be properly set up.

Create a new directory for the site DocumentRoot

Next, we need to create the directory that will act as the DocumentRoot for this website. It’s a good idea to follow a standard naming convention if you’r hosting multiple websites.

1. Follow the standard naming convention used by cPanel, and make the DocumentRoot based on the name public_html, like so:

mkdir -p /var/www/nginxsite.com/public_html

2. Create a test index.html in this directory so that we have something to look at when we test the configuration later:

vim /var/www/nginxsite.com/public_html/index.html

3. Use the HTML below the fold for this test index file:

<html>

<head>

<title>www.nginxsite.com</title>

</head>

<body>

<h1>Success! Nginx is properly serving on this domain!</h1>

</body>

</html>

4. Give ownership of that directory to the user in question:

chown -R nginxsite:nginxsite /var/www/nginxsite.com/public_html

5. Set permissions for this folder so that it can be viewed by the outside world:

chmod 755 /var/www/nginxsite.com/public_html

Our directory is now set up, and we have a test index.html file to use.

Configure NGINX to recognize new VirtualHosts (server blocks)

Now for the fun part. Configuring a VirtualHost for NGINX is very similar to Apache, though the layout of the configuration file is a bit different. Also, in NGINX, they are referred to as “server blocks,” and not the Apache VirtualHost label. It’s worth noting that when editing an Apache configuration file, we’re editing XML. With NGINX, we’re actually editing the C code.

1. Set up directories where the server blocks will live:

mkdir /etc/nginx/sites-available

mkdir /etc/nginx/sites-enabled

Note: In theory, instead of doing this by having a directory tree, you could simply edit the global configuration file. However, setting up a directory tree (which is what Debian-based Linux distros, such as Ubuntu, do), allows for an easier configuration down the line as more website are added.

2. Open the global NGINX configuration file in the text editor of your choice (we will use vim):

vim /etc/nginx/nginx.conf

3. Add these lines to the end of the http {} block, then save the file:

include /etc/nginx/sites-enabled/*.conf;

server_names_hash_bucket_size 64;

Great. Now NGINX can recognize the server block.

Configure the actual NGINX server blocks

1. Create a new file specifically for the server block for your site. (The line below will do this and open it in vim.)

vim /etc/nginx/sites-available/nginxsite.com.conf

2. Paste a new NGINX server block in here, which should look like this:

server {

  listen       80;

  server_name  nginxsite.com www.nginxsite.com;

  location / {

    root   /var/www/nginxsite.com/public_html;

    index  index.html index.htm;

    try_files $uri $uri/ =404;

    }    error_page   500 502 503 504  /50x.html;

  location = /50x.html {

    root   html;

  }

}

Let’s break down a few important parts of this process:

server_name. This is the domain you’ll be using for your site. Instead of localhost, we will use the public facing domain and www version of the domain you want to use, like so:

server_name  nginxsite.com www.nginxsite.com;

root. This should be set to the directory where the files live. In our example this can be changed to /var/www/nginxsite.com/public_html:

root /var/www/nginxsite.com/public_html;

try_files. This is something we need to add in the location block. What we are doing here is telling the server to display a 404 error when a given file is not found. So you’ll place this right under the index definition, before the closing } bracket:

try_files $uri $uri/ =404;

Create your server block using these parameters, then go ahead and save and close the file.

3. Create a symbolic link between sites-available and sites-enabled:

ln -s /etc/nginx/sites-available/nginxsite.com.conf /etc/nginx/sites-enabled/nginxsite.com.conf

4. Restart NGINX:

service nginx restart

You’re done! Provided your DNS and/or hosts file is pointed for your domain, you should now be able to go to the domain in a web browser and see the test HTML page we created earlier.