Apache Virtual Hosts allow you to host multiple websites on a single server. This guide will walk you through setting up virtual hosts on CentOS 6.
1. Install Apache
If Apache is not already installed, you can install it using:
sudo yum install httpd
2. Create Your Website Directory
Decide where you want to store your website files. Typically, you might create a directory in /var/www/. For example:
sudo mkdir -p /var/www/example.com/public_html
Set the correct permissions for the directory:
sudo chown -R $USER:$USER /var/www/example.com/public_html
3. Create a Virtual Host File
Create a new virtual host configuration file in the /etc/httpd/conf.d/ directory. For example:
sudo nano /etc/httpd/conf.d/example.com.conf
Add the following configuration to the file, adjusting the values as needed:
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
Save and close the file (press Ctrl+X, then Y, and Enter).
4. Restart Apache
Apply the changes by restarting Apache:
sudo service httpd restart
5. Update Your Hosts File (Optional)
If you are testing on a local machine and want to access your site using example.com, update your local hosts file:
sudo nano /etc/hosts
Add the following line, replacing 127.0.0.1 with your server’s IP address if needed:
127.0.0.1 example.com
Save and close the file.
6. Verify Your Setup
Open a web browser and navigate to http://example.com. You should see your website’s default page or content.
7. Create Additional Virtual Hosts
Repeat steps 2 through 4 for any additional sites you want to host. Just create new directories and virtual host files as needed.
8. Secure Your Sites
Consider securing your virtual hosts with SSL/TLS. You can use tools like Certbot to obtain and install SSL certificates for your domains.