Linux Tactic

Hosting Multiple Domains on CentOS 8 with Nginx and Letsencrypt

Hosting websites on a CentOS 8 server has never been easier, thanks to Nginx. The popular web server can handle multiple domains on a single server with ease.

In this article, we’ll guide you through the steps to set up Nginx server blocks and host multiple domains on a CentOS 8 server.

Setting up Nginx server blocks on CentOS 8

Nginx is a lightweight and high-performance web server that can handle a large number of concurrent connections. It’s an excellent option for serving static files, reverse proxying, and load balancing.

To set up Nginx server blocks on CentOS 8, you’ll need to follow these steps:

Prerequisites:

– A CentOS 8 server

– Nginx installed

– Basic knowledge of Nginx configuration

Creating the Directory Structure:

– The first step is to create a document root directory for each website you want to host. – The document root is where the website files will be stored.

– Here’s an example of how to create a directory structure:

“`

sudo mkdir -p /var/www/example.com/html

“`

– This creates a directory called “example.com” under “/var/www” with a subdirectory called “html.”

Creating a Server Block:

– The next step is to create an Nginx server block configuration file for each website. – Each server block contains the configuration for a specific domain or subdomain.

– Here’s an example of a simple server block for “example.com”:

“`

server {

listen 80;

listen [::]:80;

root /var/www/example.com/html;

index index.html;

server_name example.com www.example.com;

location / {

try_files $uri $uri/ =404;

}

}

“`

– The “listen” directive specifies the IP address and port where Nginx should listen for incoming connections. – The “root” directive specifies the document root directory for the website.

– The “server_name” directive specifies the domain name(s) that this server block will handle. – The “location” directive specifies how Nginx should handle requests for this website.

– After creating the server block configuration file, you have to test it for syntax errors:

“`

sudo nginx -t

“`

– If there are no syntax errors, restart the Nginx service:

“`

sudo systemctl restart nginx

“`

Hosting multiple domains on a single CentOS server

Hosting multiple domains on a single server has many benefits, including low cost, easy maintenance, and improved security. With Nginx and the Letsencrypt SSL certificate, you can easily set up a secure environment for multiple websites.

Benefits of hosting multiple domains on a single server:

– Lower cost

– Easier maintenance

– Improved security

Letsencrypt SSL certificate:

– Letsencrypt is a free, automated, and open Certificate Authority (CA) that provides SSL certificates. – An SSL certificate is required for secure communication between a website and its visitors.

– Installing and configuring an SSL certificate for each website can be time-consuming and challenging. – With Letsencrypt, you can easily generate and renew SSL certificates for all your domains with just a few basic commands.

Here’s how to use Letsencrypt to set up SSL for a website:

Step 1: Install Certbot

“`

sudo dnf install certbot python3-dnf-plugin -y

“`

Step 2: Run Certbot to generate an SSL certificate for your domain. “`

sudo certbot certonly –webroot -w /var/www/example.com/html -d example.com -d www.example.com

“`

– This command generates a new SSL certificate using the webroot plugin.

– The “-w” option specifies the webroot for the website. – Replace “example.com” with your domain name(s).

Step 3: Configure Nginx to use the SSL certificate

“`

server {

listen 443 ssl http2;

root /var/www/example.com/html;

index index.html;

server_name example.com www.example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;

ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

location / {

try_files $uri $uri/ =404;

}

}

“`

– The “listen” directive specifies the IP address and port for HTTPS connections. – The “ssl_certificate” and “ssl_certificate_key” directives specify the location of the SSL certificate and key.

Conclusion:

In conclusion, setting up Nginx server blocks and hosting multiple domains on a CentOS 8 server is a simple process that can be completed with just a few basic commands. Nginx and Letsencrypt are powerful tools that offer many benefits for website owners.

With Nginx and Letsencrypt, you can easily create a secure and stable environment for all your websites. Hosting multiple domains on a CentOS server can be a daunting task, especially if you’re not familiar with Nginx server blocks.

However, with a little bit of knowledge, and some simple commands, you can set up your server to host multiple websites with ease. Nginx server blocks are a powerful tool that allow you to host multiple websites on a single server.

Server blocks are essentially different configurations for Nginx that allow it to serve different websites. Each server block contains the configuration for a specific domain or subdomain.

In our previous section, we discussed how to create server blocks for each of your websites. Creating the Document Root Directory:

To create the document root directory for your website, you’ll need access to the server that will host your website.

The document root directory is where your website’s files will be stored. To create the document root directory, you can use the command:

“`

sudo mkdir -p /var/www/example.com/html

“`

This command creates the directory that will be used to store the files for your website.

The “-p” option ensures that any intermediate directories that do not exist are created. Creating the Nginx Server Block:

When you have created the directory to store your website’s files, you’ll need to create an Nginx server block configuration file for that website.

The server block will contain the configuration for your website, including the domain name(s) that the server block will handle. Inside the server block, you’ll need to configure the root directory for the website, specify the index file to use, listen for incoming connections on certain ports, and configure a location block to specify how Nginx should handle requests for the website.

Here’s an example of a simple Nginx server block for a hypothetical website:

“`

server {

listen 80;

listen [::]:80;

root /var/www/example.com/html;

index index.html;

server_name example.com www.example.com;

location / {

try_files $uri $uri/ =404;

}

}

“`

This block simply listens on ports 80 and [::]:80, sets the root directory as /var/www/example.com/html, specifies index.html as the default index file, and handles requests to the domain example.com or www.example.com. After creating this server block, test it for syntax errors using the command:

“`

sudo nginx -t

“`

And if there are no syntax errors, restart the nginx service using:

“`

sudo systemctl restart nginx

“`

Benefits of Hosting Multiple Domains on a Single Server:

Hosting multiple domains on a single server has several advantages, including:

1. Reduced costs:

If you host multiple websites on a single server, you’ll save money on server hardware, maintenance, and licensing costs.

2. Easier maintenance:

Maintaining one server with multiple websites is much easier than maintaining multiple servers, as you’ll only need to deal with one set of software updates, security patches, and configuration changes.

3. Improved security:

When you host multiple websites on a single server, you can improve security by using a single firewall, Intrusion Detection System (IDS), and other security measures that apply to all your websites.

Letsencrypt SSL Certificate:

An SSL certificate is essential for securing your website and protecting your visitors’ data. In the past, SSL certificates were expensive and required a lot of configuration.

However, with Letsencrypt, you can easily generate and renew SSL certificates for all your domains with just a few basic commands. Letsencrypt is a free and automated Certificate Authority that provides SSL certificates for your website.

Here’s how to use Letsencrypt with Nginx:

Step 1: Install Certbot

Certbot is a client that automates the process of obtaining and renewing SSL certificates. “`

sudo dnf install certbot python3-dnf-plugin -y

“`

Step 2: Run Certbot to generate an SSL certificate for your domain. “`

sudo certbot certonly –webroot -w /var/www/example.com/html -d example.com -d www.example.com

“`

This command generates a new SSL certificate using the webroot plugin.

The “-w” option specifies the webroot for the website. You should replace “example.com” with your own domain name(s).

Step 3: Configure Nginx to use the SSL certificate

To configure Nginx to use the SSL certificate, you’ll need to modify the server block for your website. Here’s an example of a server block that includes SSL configuration:

“`

server {

listen 443 ssl;

listen [::]:443 ssl;

server_name example.com www.example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;

ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

root /var/www/example.com/html;

index index.html;

location / {

try_files $uri $uri/ =404;

}

}

“`

The “listen” directive specifies the IP address and port for HTTPS connections.

The “ssl_certificate” and “ssl_certificate_key” directives specify the location of the SSL certificate and key. The rest of the configuration is the same as in the non-SSL configuration.

Conclusion:

There you have it! By following these steps, you should be able to set up Nginx server blocks and host multiple domains on a CentOS 8 server. Hosting multiple domains on a single server is a great way to save money and reduce maintenance costs, and with Letsencrypt, you can easily secure your websites with SSL certificates.

In conclusion, hosting multiple domains on a CentOS 8 server is a cost-effective way to create and manage websites for small to medium enterprises. By using Nginx server blocks, you can easily create configurations for each website and manage them from a single platform.

The Letsencrypt SSL certificate is an easy and free way to secure your websites and protect your visitors’ data. With this guide, you have learned how to create Nginx server blocks, configure them to serve websites, and secure them with Letsencrypt SSL certificates.

By following these steps, you now have the knowledge to host multiple domains on your CentOS 8 server with ease.

Popular Posts