Step-by-step Guide to Creating Apache Virtual Hosts on CentOS 7
Before hosting multiple domains on your server, creating virtual hosts on the web server is crucial. This allows for serving different content based on incoming requests. This guide provides a step-by-step approach to configuring Apache virtual hosts on CentOS 7.
1. Install Apache
Ensure you have root access to your VPS. An SSH connection is also necessary. Hostinger login details can usually be found in hPanel under the Servers tab.
1. Use the following command to install Apache on your CentOS 7 server:
2. Enable Apache as a CentOS service by executing this command:
3. Confirm Apache is running correctly by visiting your server’s IP address in a web browser. You should see a default Apache welcome page, similar to this:
2. Generate a Directory Tree
1. A directory tree ensures secure storage of website data. Begin by setting /var/www/ as your working directory. Run this command:
2. Each virtual host requires a unique document root. Create a directory for your domain:
Remember to replace yourdomain.com
with your actual domain name.
3. Ensure Apache can access the directory. Change the ownership using chown
and set appropriate permissions with chmod
:
3. Set Up a Demo Page
A demo page confirms the virtual host is configured correctly. Follow these steps:
1. Create an index.html
file within the domain’s public_html
directory using the nano editor:
2. Paste the following HTML content into the index.html
file:
3. Save the file by pressing CTRL + X
, then Y
.
4. Create the Virtual Host
1. Create a virtual host configuration file (.conf) in the Apache configuration directory:
2. Add the following configuration to the yourdomain.com.conf
file, modifying it with your actual domain name:
This configuration defines the virtual host for yourdomain.com
, specifying the port (80), document root, and error logs.
3. Restart Apache to apply the changes:
You have now created an Apache virtual host for your domain! Access the domain in your web browser. If you see the “It works!” message from the demo page, the configuration is successful.
Concluding Remarks
Creating Apache virtual hosts is straightforward when following a clear process. These are the steps:
- Apache installation.
- Creation of a directory tree for website data security.
- Creation of a demo page to verify virtual host functionality.
- Creation of a virtual host configuration file.
Follow these steps, and successfully create your virtual hosts!
Blog