To host a website on your own server, first, install a web server. Among those available, one of the most popular today is Apache, compatible with both Unix and Windows systems and featuring excellent integration with other software and good media support.
But what if you needed to host multiple websites on the same server, when having one public IP address available? In this case, you will need to configure the Virtual Hosts on Apache to associate different domains to the same IP address.
In this guide, you will be shown how to install the web server and how to configure the Virtual Host tool to manage a greater number of domains on the same web server. Each domain can have its own configuration, technologies and different versions of the applications.
Pre-Requisites
To follow the tutorial, first, access to an AlmaLinux 8 server with a non-root user but with sudo privileges.
On this server, the installation of a Firewall component will be required.
Apache Installation
Apache is available on the main AlmaLinux 8 repositories. Then, use the dnf package manager :
$ sudo dnf install httpd
After confirming your choice, the package manager will install Apache on AlmaLinux 8.
To create a secure connection with the server, Apache offers the HTTPS protocol, opening port 443.
$ sudo firewall-cmd --permanent --add-service=https
Then, reload the firewall to apply the changes.
$ sudo firewall-cmd --reload
Checking the status of your Web Server
Apache does not start automatically on AlmaLinux 8. So, the command will have to be run to start the process.
$ sudo systemctl start httpd
To confirm that Apache has started, use the command:
$ sudo systemctl status httpd
If the service is working correctly, the output will be similar to the following.
Another way to check if Apache is correctly functioning, is by connecting to its landing page. To do so, simply enter the server's IP address into your browser's address bar.
If the Cloud Server has been created on Aruba, it is important to know that a public IP is provided at the time of creation, included in the cost, which is specified in your Aruba Cloud control panel.
Otherwise, it is possible to scan the host addresses connected to your network and try to connect to them, to find out the IP address of your server, until you get the one of your Apache server.
$ hostname -I
Another faster way is to request your IP address from the icanhazip.com site, using the curl command to run the request.
$ curl -4 icanhazip.com
Once the IP address of the server has been obtained, specify it in the address bar, remembering to also specify the http protocol.
If this page, which contains a lot of information about Apache, is displayed, the test, using this alternative method, has worked out.
Apache process management
Now that the service is running, use a few commands to manage it.
The basic syntax is the one that includes the systemctl command, together with the specification of some additional options:
$ sudo systemctl opzione httpd
The available options will be:
- stop, stop the server
- start, start the server
- restart, restart the server
- reload, reload the new configuration without dropping the connections
- disable, disable automatic startup of Apache
- enable, enable automatic startup of Apache.
After this introductory part on the Apache service, let's move on to the description ofVirtual Host.
Virtual Hosts
Configuration
Virtual hosts allow you to encapsulate configuration details and to manage multiple domains with different versions of applications on the same server.
On AlmaLinux 8, Apache has a virtual host activated by default which hosts files in the path /var/www/html. However, managing multiple coexisting sites in the same directory may be inefficient. Therefore, it would be advisable to create a directory for each site.
It is also important to create the right subfolders to separate the html files from the log files.
Start by creating a subdirectory in the domain, called html :
$ sudo mkdir -p /var/www/nomedominio/html
Create a new subdirectory dedicated to log files:
$ sudo mkdir -p /var/www/nomedominio/log
Now, assign the html folder to the $USER environment variable :
$ sudo chown -R $USER:$USER /var/www/nomedominio/html
Now, run this command to make sure the web root has default permissions:
$ sudo chmod -R 755 /var/www
Now, create a sample page in HTML, using your favorite text editor ( nano, vi ).
$ sudo vi /var/www/nomedominio/html/index.html
Now add the following HTML code to the new text file created:
<html>
<head>
<title>Example</title>
</head>
<body>
<h1>This is an html page</h1>
</body>
</html>
In case of vi, use ESC, type :wq and press Enter.
to save the file.
Now that the site folders and an index page for your site have been created, start creating the virtual host files.
To create the virtual hosts, create a directory with the now available sites: to do so, name the directory sites-available.
The second directory to be created is sites-enabled, and this will be useful to tell Apache that a specific virtual host is ready to receive visitors.
$ sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
Now tell Apache:
$ sudo vi /etc/httpd/conf/httpd.conf
Press the G. Then, press i and put it in INSERT mode, and add this line to the end of the file. Note that the string is exactly at the end.
IncludeOptional sites-enabled/*.conf
When finished editing, close the file by saving it.
Now that the directories for your virtual host have been created, create the virtual host file.
To start, create a new file in the sites-available directory, using the command:
$ sudo vi /etc/httpd/sites-available/tuodominio.com.conf
Inside the file, insert the following strings:
<VirtualHost *:80>
ServerName www.tuodominio.com
ServerAlias tuodominio.com
DocumentRoot /var/www/tuodominio.com/html
ErrorLog /var/www/tuodominio.com/log/error.log
CustomLog /var/www/tuodominio.com/log/requests.log combined
</VirtualHost>
At this point, the configuration file of your virtual host will have been created, and some necessary information for the management of the site by Apache will be displayed.
Now, create a link to the sites-enabled directory, using the ln command :
$ sudo ln -s /etc/httpd/sites-available/tuodominio.com.conf /etc/httpd/sites-enabled/tuodominio.com.conf
The setup of the virtual host is now complete, but checking and adjusting SELinux permissions for the Virtual Hosts will still be needed.
Configuring SELinux permissions for Virtual Host
SELinux is a security module of the Linux kernel. AlmaLinux 8 has SELinux configured to work in synergy with the Apache configuration.
However, since configuration files have already been created, it is necessary to double check the configuration of the security module.
There are two methods of granting permissions to Apache: the first is a more universal method, while the second allows you to act on Apache-specific directories. Clearly, acting on specific directories helps have greater control over the privileges granted to Apache.
Setting universal privileges for Apache
Setting universal privileges will allow SELinux to treat all Apache-related processes in the same way.
To do so, simply change the boolean value, identified by the httpd_unified entry, into 1.
$ sudo setsebool -P httpd_unified 1
Analyzing the complete syntax of the command:
- The setsebool command is used to change boolean values in SELinux
- The -P flag will allow the changes to persist on subsequent reboots
- The httpd_unified variable contains the boolean value that will tell SELinux to manageall the Apache processes equally, while setting the value to 1.
Setting privileges on Apache directories
The permissions granted by SELinux can be individually set to some Apache folders, allowing for better management and control. However, it requires greater time and maintenance in case of variations.
Try granting permissions to the domain of your log folder to get started. To do so, use the command:
$ sudo ls -dlZ /var/www/tuodominio.com/log/
When checking, an output like the following should be shown:
Testing the Virtual Host
After the configuration steps, it's time to test your virtual host.
The first step is to restart Apache:
$ sudo systemctl restart httpd
Now, check if the Apache log files have been created in the appropriate directory of your domain. Use the ls command to get a list of the contents of that directory:
$ ls -lZ /var/www/tuodominio.com/log
An output like the following should be shown:
Now, it’s time to test the functioning of the server, by inserting that of your domain in the address bar.
If an html page is correctly viewed, then the test was successful!
Conclusions
At the end of this tutorial, you will have been able to install the Apache service and configure virtual hosts to manage multiple domains on the same server with AlmaLinux 8.