In the world of web hosting, Nginx stands out as a powerful and efficient web server. Its lightweight nature and high performance make it a popular choice for hosting websites. In this step-by-step guide, we’ll walk you through the process of installing Nginx on Linux, configuring a virtual host, and securing your setup.
Step 1: Install Nginx
yum install epel-release -y
yum install nginx -y
systemctl start nginx && systemctl enable nginx
This sets the foundation for your web server. Nginx is now installed and set to start on boot.
Step 2: Allow HTTP/HTTPS on Firewall
firewall-cmd --permanent --zone=public --add-service=http --add-service=https
firewall-cmd --reload
Open up your firewall to allow web traffic through HTTP and HTTPS.
Step 3: Configure Virtual Hosts
Create Additional Configuration Files
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled
Declare additional configuration files in nginx.conf
:
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;
Change the default document root to:
root /var/www/html;
Step 4: Create Directory Structure and Set Permissions
mkdir -p /var/www/html/ayaaninfratech.com
chown -R nginx:nginx /var/www/html/ayaaninfratech.com
chmod -R 755 /var/www/html/ayaaninfratech.com
Prepare the directory structure for your website and set the necessary permissions.
Step 5: Create Demo Pages
vim /var/www/html/ayaaninfratech.com/index.html
Create a simple HTML page for your website.
<html>
<head>
<title>Welcome to ayaaninfratech.com!</title>
</head>
<body>
<h1>Thanks for visiting!</h1>
</body>
</html>
Step 6: Create the Server Block Configuration
vim /etc/nginx/sites-available/ayaaninfratech.com.conf
Insert the server block configuration for your website.
server {
listen 80;
server_name ayaaninfratech.com www.ayaaninfratech.com;
location / {
root /var/www/html/ayaaninfratech.com;
index index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Step 7: Enable the New Server Block
ln -sf /etc/nginx/sites-available/ayaaninfratech.com.conf /etc/nginx/sites-enabled/ayaaninfratech.com.conf
Enable the new server block for your website.
Step 8: SELinux Configuration
setsebool -P httpd_can_network_connect on
getenforce
chcon -Rt httpd_sys_content_t /var/www/html/ayaaninfratech.com
Adjust SELinux settings for proper web server functioning.
Conclusion
Congratulations! You’ve successfully set up Nginx on CentOS and configured a virtual host for your website. This robust web server will serve your content efficiently. Feel free to customize further and explore Nginx’s extensive features for an optimized web hosting experience. Happy hosting!