Configure 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.
Let’s create a new directory for the site DocumentRoot.
It is a good idea to follow a standard naming convention if you are hosting multiple websites.
We’ll follow the standard used by cPanel,mk and make our DocumentRoot based on the name public_html, like so:
mkdir -p /var/www/yourdomain.com/public_html
Let’s create a test index.html in this directory so that we have something to look at when we test the configuration later:
nano /var/www/yourdomain.com/public_html/index.html
All we need is a simple line of text to show that the connection is working:
Hello world!
Close and save the index.html file.
We need to set permissions for this folder so that it can be viewed by the outside world:
chmod 755 /var/www/yourdomain.com/public_html
Our directory is now set up, and we have a test index.html waiting to be viewed.
Step 4: Configure NGINX to recognize server blocks
We can host multiple websites on a single Apache server by using Virtual Hosts. This effectively acts as routing instructions that points a domain to the appropriate directory on the server.
With NGINX, this sort of routing is handled by “server blocks” instead of Virtual Host entries. They’re similar, but the configuration file is a bit different.
First, we need to set up our directories where the server blocks will live:
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled
NOTE: Yes, we could just edit the NGINX global configuration file (nginx.conf) instead of creating a directory structure. However, by setting up a directory tree (which is what Debian-based Linux distros like Ubuntu will do), it allows for an easier configuration down the line if more websites are added.
Now we need to tell NGINX to use look at those directories for the server blocks. Open the global NGINX configuration file:
nano /etc/nginx/nginx.conf
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.
Create a new file specifically for the server block for the yourdomain.com site:
nano /etc/nginx/sites-available/yourdomain.com.conf
We are going to paste a new NGINX server block in this file:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com; location / {
root /var/www/yourdomain.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 the server block.
server_name: This is the domain you will 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 yourdomain.com www.yourdomain.com;
root: This is the root directory for the site files.
root /var/www/yourdomain.com/public_html;
try_files: What we are doing here is telling the server to display a 404 error when a given file is not found.
try_files $uri $uri/ =404;
Create your server block using these parameters, then go ahead and save and close the file.
We need to create a symbolic link between sites-available and sites-enabled:
ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled/yourdomain.com.conf
And finally, 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 your domain in a web browser and see the test HTML page we created earlier.
Congratulations — you now have NGINX up and running on CentOS 7.
You have successfully disabled Apache on your system, and substituted it with the sleek and sexy NGINX web server. You’re now ready to start tinkering and deploying the development stack of your choice.
As always, there will likely be more advanced configuration you want to do with NGINX to optimize the web server for your site. We highly suggest reviewing the NGINX documentation for any additional configuration you may want to do on the web server.
Otherwise, happy developing!
Thanks for reading, please feel free to comment with this post.