How to Set up Nginx Server Blocks on Ubuntu and CentOS

How to Set up Nginx Server Blocks on Ubuntu and CentOS

In this tutorial, we are going to provide you with step by step instructions on how to set up nginx server blocks on Ubuntu 16.04 and  CentOS 7. Nginx server blocks are often used to run multiple websites on a single IP address.

Note that this tutorial is for Nginx only. If you use Apache, you’ll need to set up virtual hosts

1. Install Nginx on Ubuntu

Make sure your server OS packages are fully up-to-date:

apt-get update 
apt-get upgrade

Stop and remove Apache, then install nginx using the following commands:

service apache2 stop
apt-get remove apache2
apt-get autoremove
apt-get install nginx

Remove the default nginx configuration file:

rm /etc/nginx/sites-enabled/default

Enable nginx service to start on boot and start it:

systemctl enable nginx
systemctl start nginx.service

2. Install Nginx on CentOS

Make sure that all OS packages are up to date by running the following commands:

yum clean all
yum update

Stop and remove Apache if it is installed and running on your virtual server:

systemctl stop httpd
systemctl disable httpd
yum remove httpd

Install nginx:

yum install nginx

Also, you may want to install PHP and MariaDB, so run the following command:

yum install nginx mariadb mariadb-server php php-fpm php-cli php-curl php-gd php-mbstring php-mysql php-xml

Edit the /etc/php-fpm.d/www.conf configuration file and change:

user = apache
group = apache

to:

user = www-data
group = www-data

Run the following command:

chown www-data: -R /var/lib/php/session

Start the PHP-FPM, Nginx and MariaDB services and enable them to start on boot:

systemctl start php-fpm
systemctl start nginx
systemctl start mariadb
systemctl enable php-fpm
systemctl enable nginx
systemctl enable mariadb

3. Configure nginx and set up nginx server blocks

Let’s configure the core directives in the main nginx configuration file named ‘nginx.conf’. It should be located in the /etc/nginx/ directory on your server.

vi /etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

user – a user which will own and run the nginx server.

worker_processes – in general, it is best to set the nginx worker_processes config to the number of CPUs. If for example you are using our SSD 4 VPS plan, set worker_processes to 4.

To check the number of CPUs on your server, you may use the following command:

#cat /proc/cpuinfo | grep processor | wc -l

4

pid – the location where nginx will write its master process ID, or PID.

If you use CentOS, create a new Nginx configuration file for your website:

vi /etc/nginx/conf.d/domain1.com.conf

Add the following content:

server {
    server_name www.domain1.com domain1.com;
    listen 80;
    root /var/www/html/domain1.com;
    access_log /var/log/nginx/domain1.com-access.log;
    error_log /var/log/nginx/domain1.com-error.log;
    index index.php;
 
    location / {
        try_files  $uri $uri/ /index.php?$args;
    }

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
    }

    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }

}

If you need to host more than one website on the same server (using the same IP address), you can create a new server block. It is best to create a new configuration file for each website.

For example, create a second nginx configuration file named /etc/nginx/conf.d/domain2.com.conf and add the same content as /etc/nginx/conf.d/domain1.com.conf , but make sure to replace domain1.com with the second domain name including the document root of your website, location of log files etc.:

server {
    server_name www.domain2.com domain2.com;
    listen 80;
    root /var/www/html/domain2.com;
    access_log /var/log/nginx/domain2.com-access.log;
    error_log /var/log/nginx/domain2.com-error.log;
    index index.php;
 
    location / {
        try_files  $uri $uri/ /index.php?$args;
    }

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
    }

    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }

}

If you use Ubuntu OS, create the ‘domain1.com.conf’ nginx configuration file to the /etc/nginx/sites-available directory.

vi /etc/nginx/sites-enabled/domain1.com.conf

(add the same content as the content listed above, i.e. /etc/nginx/conf.d/domain1.com.conf on a CentOS server).

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

Then, create a symbolic link from this file to the sites-enabled directory:

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

Repeat the same procedure and create new configuration files for all other websites hosted on the server.

4. Test the Nginx configuration

nginx -t

It the test is successful, restart the Nginx service:

systemctl restart nginx

Upload your website files to the /var/www/html/domain1.com , /var/www/html/domain2.com /var/www/html/domainN.com directories.

The web server user (www-data) needs to have access to /var/www/html/domain1.com , /var/www/html/domain2.com , /var/www/html/domainN.com directories. It can easily be accomplished by executing the following command:

chown -R www-data:www-data /var/www/html/

Of course you don’t have to How to Set up Nginx Server Blocks on Ubuntu and CentOS,  if you use one of our Nginx VPS Hosting services, in which case you can simply ask our expert Linux admins to set up nginx server blocks for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post, on how to set up Nginx Server Blocks on Ubuntu and CentOS,  please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

1 thought on “How to Set up Nginx Server Blocks on Ubuntu and CentOS”

Leave a Comment