Setting up a web server on CentOS 7 using Nginx is a straightforward process that can be completed in a few steps. Nginx is a powerful web server that can handle high levels of traffic while using minimal system resources. In this blog, we will guide you through the steps required to set up a web server on CentOS 7 using Nginx.
Step 1: Update the system
The first step is to ensure that the CentOS 7 system is up-to-date. To do this, open a terminal window and run the following command:
sudo yum update
Step 2: Install Nginx
To install Nginx, run the following command in the terminal:
sudo yum install nginx
Step 3: Start Nginx
Once Nginx is installed, you can start it by running the following command:
sudo systemctl start nginx
To ensure that Nginx starts automatically when the system is rebooted, run the following command:
sudo systemctl enable nginx
Step 4: Configure the Firewall
By default, the CentOS 7 firewall blocks incoming traffic. To allow traffic to access your web server, you will need to configure the firewall to allow HTTP and HTTPS traffic. To do this, run the following commands:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
After running these commands, reload the firewall with the following command:
sudo firewall-cmd --reload
Step 5: Create a new Nginx virtual host configuration file
Nginx uses virtual host configuration files to serve multiple websites from the same server. To create a new virtual host configuration file, run the following command:
sudo nano /etc/nginx/conf.d/example.com.conf
Replace example.com
with your domain name.
In the configuration file, add the following code:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Save and close the file.
Step 6: Create the document root directory
The document root directory is the location on the server where the website files are stored. To create the directory, run the following command:
sudo mkdir -p /var/www/example.com/html
Replace example.com
with your domain name.
Step 7: Add a test file
To test that the web server is working, create a test file in the document root directory. Run the following command:
sudo nano /var/www/example.com/html/index.html
Add the following code to the file:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Hello, world!</h1>
<p>If you can see this, it means that your web server is working correctly.</p>
</body>
</html>
Save and close the file.
Step 8: Restart Nginx
To apply the changes made to the Nginx configuration file, restart Nginx by running the following command:
sudo systemctl restart nginx
Step 9: Test the web server
To test the web server, open a web browser and enter your domain name in the address bar. If everything is set up correctly, you should see the test page you created earlier.
Congratulations! You have successfully set up a web server on CentOS 7 using Nginx.