The Apache Web server is the most common way to provide Web content on the Internet, decomposing its functions and components into individual units that can be individually customized and configured. The basic unit describing a single site or domain is called a virtual host. Virtual hosting allows one server to use a matching system to host multiple domains or interfaces.
In this tutorial, we will introduce how to set up Apache Virtual Host on CentOS 7 server. In this process, you will learn how to provide different content to different visitors based on the requested domain. If there is a problem with the Apache configuration virtual host, you can also visit [Tencent Cloud Community to view related solutions] (https://cloud.tencent.com/developer/article/1113091?from=10680).
Before starting this guide, you need to complete a few steps.
sudo yum -y install httpd
Next, enable Apache as a CentOS service so that it will start automatically after rebooting:
sudo systemctl enable httpd.service
After completing these steps, log in as a non-root user account via SSH and continue this tutorial.
First, we need to create a directory structure to store site data in order to provide services to visitors.
Our document root directory (the top-level directory where Apache looks for content to provide) will be set to the /var/www
directory in the directory. We will create a directory for each virtual host we plan to make.
In each directory, we will create a public_html directory where the actual files are saved. This provides some flexibility for our hosting.
We can use the mkdir
command to create these directories (with a -p flag, allowing us to create a nested folder):
sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/example2.com/public_html
The directory structure where we now have files is owned by the root user. If we want our regular users to be able to modify the files in our website directory, we can change the ownership by chown:
sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example2.com/public_html
When you submit the command, the \$USER
variable will adopt your currently logged-in user. Now, our regular user now has permissions for the public_html
subdirectory. We should also make sure to allow read access to the general web directory and all files and folders inside so that the page can be served correctly:
sudo hmod -R 755/var/www
Your web server should now have the necessary permissions to provide content, and your users should be able to create content in the corresponding folders.
Now that we have the directory structure, let's create some content and our page will be very simple. We will create an index.html page for each site that identifies that specific domain.
Let's start with example.com
. We can open the index.html
file in the editor by typing the following:
nano /var/www/example.com/public_html/index.html
In this file, create a simple HTML document pointing to the site connected to the page. The file for our first domain will look like this:
< html><head><title>Welcome to Example.com!</title></head><body><h1>Success! The example.com virtual host is working!</h1></body></html>
Save and close the file when you are done.
We can copy the index.html
file to use as a template for the second website by typing:
cp /var/www/example.com/public_html/index.html /var/www/example2.com/public_html/index.html
Now let us open the file and modify the relevant information:
nano /var/www/example2.com/public_html/index.html
< html><head><title>Welcome to Example2.com!</title></head><body><h1>Success! The example2.com virtual host is working!</h1></body></html>
Save and close this file.
The virtual host file specifies the configuration of our various sites and instructs the Apache web server how to respond to various domain requests.
First, we need to set up the directory where the virtual host is stored, and tell the Apache virtual host to provide services to visitors. The sites-available
directory will hold all our virtual host files, and the sites-enabled
directory will hold the symbolic link of the virtual host we want to publish. We can enter the following two directories:
sudo mkdir /etc/httpd/sites-available
sudo mkdir /etc/httpd/sites-enabled
Next, we make Apache look for virtual hosts in the sites-enabled directory. We will edit Apache's main configuration file and add a line representing optional directories for other configuration files:
sudo nano /etc/httpd/conf/httpd.conf
Add this line to the end of the file:
IncludeOptional sites-enabled/*.conf
After you finish adding the line, save and close the file. We are now ready to create our first virtual host file.
First open the new file with root privileges in the editor:
sudo nano /etc/httpd/sites-available/example.com.conf
**Note: **All virtual host files must end with .conf.
First make a pair of tags and specify the content as a virtual host listening on port 80 (the default HTTP port):
< VirtualHost *:80></VirtualHost>
Next we will declare the name of the main server www.example.com
. We will also specify a server alias to point to example.com
so that requests to www.example.com
and example.com
provide the same content:
< VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
< /VirtualHost>
**Note: **In order for the
www
version of the domain to work properly, the DNS configuration of the domain will require an A record or CNAME to point thewww
request to the server's IP. Wildcard (*) records are also available.
Finally, we will point to the root directory of the publicly accessible web document. We will also tell Apache where to store error and request logs for this particular site:
< VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog /var/www/example.com/error.log
CustomLog /var/www/example.com/requests.log combined
< /VirtualHost>
After writing these items, you can save and close the file.
Now that we have created the first virtual host file, we can create the second virtual host file by copying the file and adjusting as needed.
First copy it with cp:
sudo cp /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-available/example2.com.conf
Open the new file with root privileges in a text editor:
sudo nano /etc/httpd/sites-available/example2.com.conf
You now need to modify all the information to reference your second domain. When you are done, your second virtual host file might look like this:
< VirtualHost *:80>
ServerName www.example2.com
DocumentRoot /var/www/example2.com/public_html
ServerAlias example2.com
ErrorLog /var/www/example2.com/error.log
CustomLog /var/www/example2.com/requests.log combined
< /VirtualHost>
After making these changes, you can save and close the file.
Now that we have created the virtual host files, we need to enable them so that Apache can provide services to visitors. To do this, we can create a symbolic link for each virtual host in the sites-enabled
directory:
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
sudo ln -s /etc/httpd/sites-available/example2.com.conf /etc/httpd/sites-enabled/example2.com.conf
When finished, restart Apache for these changes to take effect:
sudo apachectl restart
If you have been using a sample domain instead of an actual domain to test this process, you can still test the functionality of the virtual host by temporarily modifying the hosts file on the local computer. This will intercept any requests for your configured domain and direct them to your server. But only for testing purposes.
**Note: **Make sure you run these steps on your local computer. You will need administrative credentials to access the computer.
If you are using a Mac or Linux computer, type the following command to edit the local hosts
file with administrative rights:
sudo nano /etc/hosts
If you are using a Windows computer, you can find instructions on changing the host file here.
The details you need to add are the public IP address of the server, followed by the domain you want to use to access the service:
127.0.0.1 localhost
127.0.1.1 guest-desktop
server_ip_address example.com
server_ip_address example2.com
This will direct any requests for example.com
and example2.com
to our local computer and send them to our server at server_ip_address
.
Now that you have configured the virtual host, you can easily test your settings by entering the domain configured in the web browser:
http://example.com
You should see a page like this:
result interface
Similarly, if you visit other domains, you will also see the files created for them.
If all the sites you configured are running well, then you have successfully configured a new Apache virtual host on the CentOS server.
If you have adjusted the hosts file of your computer, you may need to delete lines that have been verified to be valid. Because this will prevent your hosts file from being filled with a lot of unnecessary entries.
This tutorial introduces how to set up an Apache virtual host on CentOS 7. If you are using a Debian system, you can refer to the tutorial of How to set up an Apache virtual host on Debian 8 in Tencent Cloud Community.
Reference: "How To Set Up Apache Virtual Hosts on CentOS 7"
Recommended Posts