Home >Operation and Maintenance >Linux Operation and Maintenance >Multi-version coexistence: A guide to building multiple web servers at the same time on CentOS

Multi-version coexistence: A guide to building multiple web servers at the same time on CentOS

PHPz
PHPzOriginal
2023-08-08 12:33:271502browse

Multi-version coexistence: A guide to building multiple web servers at the same time on CentOS

Multi-version coexistence: A guide to building multiple web servers at the same time on CentOS

In modern web development, it is often necessary to build multiple web servers on the same server Server to meet the needs of different projects or different versions. This article will guide you on how to build multiple versions of web servers simultaneously on the CentOS operating system.

First, we need to install and configure two mainstream web servers, Apache and Nginx. Here are the steps to install Apache and Nginx on CentOS:

  1. Install Apache

Install Apache using the following command:

sudo yum install httpd

After the installation is complete, start Apache and set it to start automatically at boot:

sudo systemctl start httpd
sudo systemctl enable httpd
  1. Install Nginx

Use the following command to install Nginx:

sudo yum install nginx

After the installation is completed, start the same Nginx and set it to start automatically at boot:

sudo systemctl start nginx
sudo systemctl enable nginx

At this point, we have completed the installation and configuration of Apache and Nginx. Next, we will configure multiple versions of the web server.

  1. Configuring multiple versions of Apache

First, we need to create different virtual hosts for each different version of the web server. In Apache, the virtual host configuration file is located in the /etc/httpd/conf.d directory. We can create a corresponding configuration file for each version. For example, we create two versions of virtual hosts, namely vhost1.conf and vhost2.conf.

Create vhost1.conf:

sudo vi /etc/httpd/conf.d/vhost1.conf

Add the following content to the vhost1.conf file:

<VirtualHost *:80>
    ServerAdmin admin@vhost1.com
    DocumentRoot /var/www/vhost1
    ServerName vhost1.com
    <Directory /var/www/vhost1>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Create vhost2.conf:

sudo vi /etc/httpd/conf.d/vhost2.conf

Add the following Add the content to the vhost2.conf file:

<VirtualHost *:80>
    ServerAdmin admin@vhost2.com
    DocumentRoot /var/www/vhost2
    ServerName vhost2.com
    <Directory /var/www/vhost2>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Save and exit the configuration file. Next, we need to create the corresponding website directory and set permissions:

sudo mkdir /var/www/vhost1
sudo chmod -R 755 /var/www/vhost1

sudo mkdir /var/www/vhost2
sudo chmod -R 755 /var/www/vhost2

Restart Apache to make the configuration take effect:

sudo systemctl restart httpd
  1. Configure multiple versions of Nginx

Similarly, in Nginx, we also need to create different configuration files for each version. The Nginx configuration file is located in the /etc/nginx/conf.d directory.

Create vhost1.conf:

sudo vi /etc/nginx/conf.d/vhost1.conf

Add the following content to the vhost1.conf file:

server {
    listen 80;
    server_name vhost1.com;
    root /var/www/vhost1;
    index index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
}

Create vhost2.conf:

sudo vi /etc/nginx/conf.d/vhost2.conf

Add the following Add the content to the vhost2.conf file:

server {
    listen 80;
    server_name vhost2.com;
    root /var/www/vhost2;
    index index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
}

Save and exit the configuration file. Next, we need to create the corresponding website directory and set permissions:

sudo mkdir /var/www/vhost1
sudo chmod -R 755 /var/www/vhost1

sudo mkdir /var/www/vhost2
sudo chmod -R 755 /var/www/vhost2

Restart Nginx to make the configuration take effect:

sudo systemctl restart nginx

At this point, we have successfully configured the multi-version web server of Apache and Nginx. You can add more virtual hosts and profiles as needed.

Summary:

This article introduces how to install and configure Apache and Nginx on the CentOS operating system, and realize the simultaneous establishment of multiple versions of web servers by creating virtual hosts and configuration files. Using a multi-version web server, you can easily meet the needs of different projects or different versions. I wish you success in web development!

The above is the detailed content of Multi-version coexistence: A guide to building multiple web servers at the same time on CentOS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn