Apache Virtual Hosts Configuration Centos Stream 9
Apache Virtual Hosts allows you to host multiple websites on a single server. By using virtual hosts, you can configure different domain names to point to different websites or web applications, all on the same physical machine. This is especially useful for web hosting companies or developers who need to manage multiple websites without needing additional servers.
There are two (2) main types of virtual hosts in Apache: Name-Based and IP-Based. Name-based virtual hosts use the domain name to determine which website to serve, while IP-based virtual hosts use the server's IP address.
***Our example is on Name-based Virtual Hosts.
Creating a directory framework:
The document root is the directory where website files for a domain are stored and served to clients. The document root can be stored in any location you want.
The example directory framework is shown here.
/var/www/
├── farukguler.com
│ └── public_html
├── digital-defender.eu
│ └── public_html
├── linuxacademy.edu.gov
│ └── public_html
├── windows-server.dev
│ └── public_html
You must create a separate directory for each domain name that will be kept on the server.
Now let's create the root directory ve web files for the domain: farukguler.com
echo '192.168.5.111 www.farukguler.com' >> /etc/hosts
echo '192.168.5.111 www.example.com'>> /etc/hosts
sudo mkdir -p /var/www/farukguler.com/public_html
sudo mkdir -p /var/www/example.com/public_html
sudo nano /var/www/farukguler.com/public_html/index.html
sudo nano /var/www/example.com/public_html/index.html
#locate: /var/www/farukguler.com/public_html/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</style>
</head>
<body>
<h1>farukguler.com</h1>
<p>Web sitemiz şu anda bakımdadır.</p>
<img src="https://www.ns-newelding.com/wp-content/uploads/2019/10/maintenance-of-industrial-plant-1080x630.jpg" width="700" height="400" alt="bakim">
</body>
</html>
If you want to change the ownership of the Apache HTTP Server's document root directory to the "apache" user, you can use the chown command. With this process, the "apache" user gains permission to read and write the document root directory and the files within it.
#Give file ownership permissions to user "apache":
sudo chown -R apache: /var/www/farukguler.com
#Give everyone "Read" and "execute" access in your directory:
sudo chmod -R 755 /var/www/farukguler.com
#Check if there are any errors.
httpd -t
#Apache Service config file:
cd /etc/httpd/conf
nano httpd.conf
#farukguler.com config file:
cd /etc/httpd/conf.d/
touch farukguler.com.conf
nano farukguler.com.conf
***A virtual host file is a configuration file that determines how the Apache HTTP Server behaves for a particular website or domain. These files allow the server to manage multiple websites or applications on the same server. Create this file.
#locate: /etc/httpd/conf.d/farukguler.conf
<VirtualHost *:80>
ServerName farukguler.com
ServerAlias www.farukguler.com
ServerAdmin [email protected]
DocumentRoot /var/www/farukguler.com/public_html
<Directory /var/www/farukguler.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog /var/log/httpd/farukguler.com-error.log
CustomLog /var/log/httpd/farukguler.com-access.log combined
</VirtualHost>
- ServerName: The name of the domain in which the virtual host configuration will be used.
- ServerAlias: All other domains, such as the subdomain in which the virtual host configuration will be used, are www.
- DocumentRoot: The directory where Apache serves domain files.
- Options: This directive controls server properties on a directory basis.
- -Indexes: Prevents index listings.
- FollowSymLinks: Tells the web server to follow symbolic links.
- AllowOverride: Specifies which directives declared in the file can override .htaccess configuration directives.
- ErrorLog, CustomLog: Specifies the location of the log files.
- .etc
sudo apachectl configtest
>>Syntax OK
sudo systemctl restart httpd
Check the syntax of Apache configuration files and restart the Apache service
Firewalld Permissions (Allow: 80,43)
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-ports
Summary:
I explained how to configure Apache virtual host in CentOS 9. You can repeat these steps for all your other domain names.
- Additionally, it will be beneficial for you to secure your website with an SSL/TLS certificate.
- You need to configure dns records for all your domain names.
Wait! There is another way. /Faruk GULER