How To Setup Virtual Host On CentOS 7
In this discourse, we delve into the setup of Virtual Host on CentOS 7, a practice integral to Apache server administration. Virtual Host, a prevalent concept in Apache architecture, proves invaluable when tasked with hosting multiple websites on a singular server. By adopting Virtual Host configuration, you streamline operations, consolidating multiple websites onto a single server infrastructure. This not only optimizes resource utilization but also eliminates the need for managing and launching individual servers for each website, thereby enhancing efficiency and economizing time.
Understood. Before proceeding with setting up Virtual Hosts on CentOS 7, it’s essential to install the Apache web server. Follow the instructions outlined in the post titled “How To Setup the HTTP Web Server on CentOS 7” to complete the installation of Apache. Once Apache is successfully installed, you can proceed with configuring Virtual Hosts on your CentOS 7 server.
To organize your projects effectively and facilitate multiple site hosting, you’ll need to create a directory structure. Follow these steps:
1. Single Website Hosting
If you’re hosting a single website, you can simply place your project code in the default directory for Apache, which is /var/www/html
.
2. Multiple Site Hosting
For hosting multiple sites, you should create a root directory for each domain. Here’s how to create the root directory for a domain like example.com
:
sudo mkdir -p /var/www/example.com/html
This command creates a directory structure where:
/var/www
is the root directory for web content.
example.com
is the domain name.
html
is the directory where the website files will be stored.
You can repeat this process for each domain you intend to host on your Apache server. This organized directory structure simplifies management and ensures that each website’s files are segregated properly.
To create an index.html
file inside the document root directory of your domain for testing purposes, follow these steps:
1. Open your preferred text editor.
2. Create a new file named index.html
.
3. Add the following HTML content to the file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to example.com</title>
</head>
<body>
<h1>Welcome to example.com</h1>
<p>This is a placeholder page for example.com. Replace this content with your actual website content.</p>
</body>
</html>
Save the file.
This index.html
file serves as the default landing page for your domain example.com
. You can replace the placeholder content with your actual website content when ready.
To avoid permission issues, it’s important to change the ownership of the domain document root directory to the Apache user. You can achieve this by executing the following command:
sudo chown -R apache:apache /var/www/example.com/html
To ensure that files in /var/www
and its subfolders can be read correctly, you can use the following command to set the appropriate permissions:
sudo chmod -R 755 /var/www
This command changes the ownership of the directory /var/www/example.com/html
and all its contents to the Apache user and group (apache:apache)
. By doing this, the Apache web server will have the necessary permissions to read and serve the files within this directory.
Indeed, organizing Virtual Host configurations into separate files offers improved maintainability. Each Virtual Host can have its own configuration file, simplifying management and making it easier to understand and troubleshoot individual configurations. This approach also enhances scalability, allowing you to add or remove Virtual Hosts without affecting other configurations.
Let’s proceed with creating separate configuration files for each Virtual Host Directive.
To organize your virtual host configurations, you need to create the sites-available
and sites-enabled
directories. Follow these steps:
sudo mkdir /etc/httpd/sites-available
sudo mkdir /etc/httpd/sites-enabled
These commands create the necessary directories for managing your virtual hosts. The sites-available
directory will contain configuration files for all available virtual hosts, while the sites-enabled
directory will hold symbolic links to the virtual hosts that are currently enabled and ready to serve visitors.
To instruct Apache to include virtual hosts configurations from the sites-enabled
directory, you need to edit Apache's main configuration file. Follow these steps:
sudo vi /etc/httpd/conf/httpd.conf
In the configuration file, locate the line containing IncludeOptional
, or if it's not present, you can add it. Then add the following line below it:
IncludeOptional sites-enabled/*.conf
This line tells Apache to include all configuration files with a .conf
extension from the sites-enabled
directory. Once you've added this line, save the changes and exit the editor.
Create a new virtual host configuration file for your domain in the sites-available
directory, follow these steps:
sudo vi /etc/httpd/sites-available/example.com.conf
Replace example.com.conf
with the appropriate filename for your domain. This command will open a new file named example.com.conf
in the sites-available
directory using the vi text editor.
In this file, you’ll define the configuration for your virtual host. Make sure to configure it according to your specific needs, including specifying the domain name, document root, and any other directives required for your website.
Here’s the configuration block you can add to your virtual host file, replacing example.com
with your actual domain name:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/example.com/html
ErrorLog /var/www/example.com/log/error.log
CustomLog /var/www/example.com/log/requests.log combined
</VirtualHost>
Ensure to replace example.com
with your actual domain name throughout the configuration block.
By specifying the DocumentRoot
, Apache knows where to find the web documents for this particular site. This directory will hold all the files that should be publicly accessible via the web server.
Save and close the file when you are finished.
To enable the virtual host for your domain, you need to create a symbolic link from the configuration file in the sites-available
directory to the sites-enabled
directory. Follow these steps:
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
Replace example.co.conf
with the filename of your virtual host configuration file.
3. Configure SELinux
To configure SELinux
to allow Apache to operate normally, you can use the following command to set the appropriate SELinux
context for the directories used by your virtual host:
sudo setsebool -P httpd_unified 1
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?"
sudo semanage fcontext -a -t httpd_log_t "/var/www/example.com/log(/.*)?"
sudo restorecon -Rv /var/www/example.com
Replace example.com
with your actual domain name in the commands above.
These commands set the SELinux
context for the directories containing your website's content and logs to allow Apache to read and write to them.
4. Testing the Virtual Host
Once you have updated the SELinux
context for the directories used by Apache, Apache will be able to write to the specified directories, such as /var/www/example.com/log
. You can now safely restart the Apache service to apply the changes.
Here’s how you can restart the Apache service:
sudo systemctl restart httpd
Now that your virtual host is set up and SELinux
permissions are updated, Apache will serve your domain name. You can test this by navigating to http://example.com
in a web browser.
Ensure that you replace example.com
with the actual domain name you configured for your virtual host.
Conclusion
In conclusion, we have successfully created an Apache virtual host configuration, enabling us to serve a website for a specific domain. By following the steps outlined above, you can repeat the process and create additional virtual hosts for all your domains. This approach allows you to efficiently manage multiple websites on a single server, enhancing flexibility and scalability in your web hosting setup. Should you have further domains to host, simply replicate the steps described to expand your web hosting capabilities.