Page 1 of 1

httpd VirtualHost configuration

Posted: 2023 May 03, 22:42
by Mihai
It is assumed that we have a website that runs on an httpd server, and we want to add a 2nd website to the same httpd server.
The httpd configuration before adding the VirtualHost:

Code: Select all

ServerName www. 1stwebiste. com:80
Alias 1stwebsite "/var/www/html/1stwebsite/"
ServerAdmin admin @ 1stwebsite.com
DocumentRoot "/var/www/html/1stwebsite"
<Directory /1stwebsite/>
    Options Indexes MultiViews FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
</Directory>
UseCanonicalName Off
So far everything is ok and works, but not as a virtual server, but as a master server.
To add a virtual server, we have to change the current website (master server) to a virtual server.
Amend as follows

Code: Select all

[root @ centos ~]$ vim /etc/http/conf/httpd.conf
# Global Config #
NameVirtualHost *:80
#-----------------------------------------------------------
#-----=== !!!  MAIN WEB SERVER www. 1stwebsite .com !!!! ===-----
<VirtualHost *:80>
    ServerName www. 1stwebsite .com:80
    ServerAdmin admin @ 1stwebsite.com
    DocumentRoot "/var/www/html/1stwebsite/"
    <Directory /var/www/html/1stwebsite/>
            Options Indexes MultiViews FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
    </Directory>
</VirtualHost>
#-----====== !!! END Config www. 1stwebsite .com !!!! =======----
Restart httpd.service

Code: Select all

[root @ centos ~]$ service httpd restart
or

Code: Select all

[root @ centos ~]$ /etc/init.d/httpd restart
We check the functionality of the website, if nothing has changed at the web-browsing level.
If everything is fine, and the server works perfectly, the next virtualhost can be added. The next virtualhost is forum.1stwebsite.com
Add the following configuration

Code: Select all

[root @ centos ~]$ vim /etc/http/conf/httpd.conf
#-----=== !!!  www.forum. 1stwebsite .com !!!! ===-----
<VirtualHost *:80>
    ServerName www.forum.1stwebsite.com:80
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/forum.1stwebsite.com/"
    <Directory /var/www/html/forum.1stwebsite.com/>
            Options Indexes MultiViews FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
    </Directory>
</VirtualHost>
#-----====== !!! END Config www. forum.1stwebsite .com !!!! =======----
Restart httpd.service

Code: Select all

[root @ centos ~]$ service httpd restart
or

Code: Select all

[root @ centos ~]$ /etc/init.d/httpd restart