In this tutorial, we will show you how to set up an Nginx Virtual Host. For those of you who didn’t know, Virtual hosts such as Nginx are used for running two or more domains or websites using just one server which you can learn more about in this hosting fundamentals course. Here’s a brief tutorial that shows you how to create a virtual host or server block on the Nginx web server.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step setup of Nginx vhost on your Linux server.
Prerequisites
- A server running one of the following operating systems: RHEL-based such as CentOS Stream, AlmaLinux, or Rocky Linux.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Setup an Nginx Virtual Host
Step 1. Before proceeding, update your Fedora operating system to make sure all existing packages are up to date. Use this command to update the server packages:
sudo dnf upgrade sudo dnf update
Step 2. Installing Nginx
By default, Nginx is available on the default base repository. Then we can install Nginx with the following command:
sudo dnf install nginx
Verify the installation using this command:
nginx -v
Output:
nginx version: nginx/1.20.2
Now After installation is complete we need to start the Nginx server to start operating. We do that with the following command below:
sudo systemctl start nginx sudo systemctl enable nginx
Step 3. Setup Virtual Host (Server Blocks).
The first thing we need to do is create a directory for each virtual host to store the different website content:
cd /var/www sudo mkdir -p idroot.us/{public_html,logs,stats} sudo mkdir -p idroot.org/{public_html,logs,stats}
idroot.us
.nano /etc/nginx/conf.d/idroot.us.conf
Add the following file:
server { listen 80; server_name idroot.us www.idroot.us; access_log /var/www/idroot.us/logs/access.log ; error_log /var/www/idroot.org/logs/error.log ; location / { root /var/www/idroot.us/public_html; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/idroot.org/public_html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; root /var/www/idroot.us/public_html; fastcgi_param SCRIPT_FILENAME /var/www/idroot.us/public_html$fastcgi_script_name; include fastcgi_params; } location ~ /.ht { deny all; } }
Save and close the file. After that, we create Nginx Virtual Host (Server Blocks) for the domain idroot.org
.
nano /etc/nginx/conf.d/idroot.us.conf
Add the following file:
server { listen 80; server_name idroot.org www.idroot.org; access_log /var/www/idroot.org/logs/access.log ; error_log /var/www/idroot.org/logs/error.log ; location / { root /var/www/idroot.org/public_html; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/idroot.org/public_html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; root /var/www/idroot.org/public_html; fastcgi_param SCRIPT_FILENAME /var/www/idroot.org/public_html$fastcgi_script_name; include fastcgi_params; } location ~ /.ht { deny all; } }
Once you’re done editing your virtual host file, be sure to save the file. And finally, if no problems were found, restart the Nginx service to apply changes:
nginx -t sudo systemctl restart nginx
Note: Please make sure that all the domain names are propagated and are properly directed to your server’s IP address, if not you will not able able to check if your new configuration works or not.
Congratulations! You have successfully installed server blocks Nginx. Thanks for using this tutorial for installing Virtual hosts Nginx on the Linux system. For additional help or useful information, we recommend you check the official Nginx website.