The Apache web server is the most widely used platform for deploying HTTP-based services. Its popularity is due to its support for dynamically loadable modules, compatibility with various file formats, and integration with other software tools.
To install the Apache HTTP server following this guide, you will need:
sudo
privileges or root
firewalld
The Apache package is available in the official CentOS repository, so you can install it using dnf
.
First, update the package list:
sudo dnf update -y
Run the following command to install Apache:
sudo dnf install httpd -y
The package manager will install the Apache web server and all necessary dependencies on CentOS.
To operate the web server, you’ll need to configure the firewall to allow HTTP and HTTPS traffic:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
After running these commands, restart the firewall to apply the new rules:
sudo firewall-cmd --reload
The Apache installation is now complete, and you can start the web server and check its functionality.
Once installed, Apache isn’t running yet, so you need to enable and start it using these commands:
sudo systemctl enable httpd
sudo systemctl start httpd
To verify if the Apache service has started, use this command:
sudo systemctl status httpd
If the web server is running correctly, you should see a message showing the status as active (running)
:
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
Active: active (running) since Thu 2024-11-07 07:34:27 GMT; 6s ago
Another way to check is to open the server’s IP address in a browser:
http://your_server_ip
You can find your server’s IP on the server's Dashboard or in an email received after setting up the server.
Now, you can try some systemctl commands for interacting with the Apache service.
For example, to stop the HTTP server, use:
sudo systemctl stop httpd
To start it again, use:
sudo systemctl start httpd
For a complete restart, such as when applying configuration changes:
sudo systemctl restart httpd
To reload Apache without interrupting active connections, use:
sudo systemctl reload httpd
We enabled Apache to start automatically when the server boots. If you prefer to disable this option, run:
sudo systemctl disable httpd
These commands allow you to manage the Apache process easily.
The default Apache HTTP server configuration allows for hosting only one site. However, you can set up virtual hosts to host multiple sites with separate resources.
Virtual hosts in Apache work similarly to those in Nginx. They allow you to separate configurations and host multiple domains on a single virtual or physical server. In this guide, we’ll use a placeholder site called example.com. When configuring, replace it with your actual domain.
Create the html
directory for example.com:
sudo mkdir -p /var/www/example.com/html
Create a directory for log files:
sudo mkdir -p /var/www/example.com/log
Set permissions for the html
directory. Assign ownership to the $USER
environment variable.
sudo chown -R $USER:$USER /var/www/example.com/html
Verify standard permissions for the root directory:
sudo chmod -R 755 /var/www
Create an index.html
file. You can use any code editor to create this file. For example, with vi
:
sudo vi /var/www/example.com/html/index.html
Add simple content to the file:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com virtual host is working!</h1>
</body>
</html>
After saving index.html
, you’re nearly ready to set up the configuration files for each domain. These files will tell Apache how to handle requests for each virtual host.
Create directories for virtual host configurations. The configuration files for individual domains are stored in a sites-available
directory, while the sites-enabled
directory will contain symbolic links to sites that are ready to receive traffic:
sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
Now, you need to instruct the HTTP server to find virtual hosts in the sites-enabled
directory. To do this, modify the main Apache configuration file by running the following command:
sudo vi /etc/httpd/conf/httpd.conf
Then, move the cursor to the very end of the file and add the following lines:
# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
IncludeOptional sites-enabled/*.conf
Now, it’s time to create the virtual host configuration file:
sudo vi /etc/httpd/sites-available/example.com.conf
In this file, add the following configuration:
<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>
Make sure to replace example.com with your actual domain name. This configuration tells the web server where to find the site’s root directory and where to store the error and access logs.
After saving and closing the file, you need to activate the virtual host by creating a symbolic link for the domain in the sites-enabled
directory:
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
At this point, the configuration is complete, and the host is ready to function. However, before restarting the web server, it’s a good idea to check if the SELinux module is correctly handling requests.
The SELinux (Security-Enhanced Linux) module enhances the operating system's security. CentOS comes with a preconfigured SELinux package that works with Apache. However, since we've made changes, starting the web server services might result in an error. To resolve this, you need to adjust SELinux policies for Apache. There are two ways to adjust these policies: a universal approach and a folder-specific approach.
This method allows the SELinux security module to use any Apache processes via the httpd_unified boolean variable. It’s convenient but doesn’t allow separate policies for individual directories and files. To enable the universal policy, run:
sudo setsebool -P httpd_unified 1
The setsebool command is used to modify boolean values, and the -P flag ensures that the change is persistent across reboots. In this case, the httpd_unified boolean is activated with the value 1.
This approach requires more steps but allows for more granular control over permissions for each directory or file. You’ll need to specify the context type for each new folder manually. For example, to check the parameters of the /var/www/example.com/log
directory, run:
sudo ls -dlZ /var/www/example.com/log/
You’ll see something like this:
drwxr-xr-x. 2 root root unconfined_u:object_r:httpd_sys_content_t:s0 6 Nov 07 09:01 /var/www/example.com/log/
You can see that the context used is httpd_sys_content_t
, meaning Apache can only read files placed in this folder. To change the context to httpd_log_t
so that the web server can write to log files, run:
sudo semanage fcontext -a -t httpd_log_t "/var/www/example.com/log(/.*)?"
This command will set the correct context for the log directory and its contents, allowing Apache to write log entries.
Apply the changes using the following command:
sudo restorecon -R -v /var/www/example.com/log
The -R
flag allows the command to run recursively, updating existing files, and the -v
flag will display the changes being made. You should see an output like this:
Relabeled /var/www/example.com/log from unconfined_u:object_r:httpd_sys_content_t:s0 to unconfined_u:object_r:httpd_log_t:s0
If you want to verify that the context type has been updated, check the current status again:
sudo ls -dlZ /var/www/example.com/log/
The output should look like this:
drwxr-xr-x. 2 root root unconfined_u:object_r:httpd_log_t:s0 6 Nov 07 09:01 /var/www/example.com/log/
After adjusting the SELinux permissions, the Apache server should now be able to write data to the /var/www/example.com/log
directory. Let’s restart the Apache service:
sudo systemctl restart httpd
Next, list the contents of the /var/www/example.com/log
directory to verify that the system has created the log files:
ls -lZ /var/www/example.com/log
You should see output similar to this:
-rw-r--r--. 1 root root system_u:object_r:httpd_log_t:s0 0 Nov 07 09:06 error.log
-rw-r--r--. 1 root root system_u:object_r:httpd_log_t:s0 0 Nov 07 09:06 requests.log
The first line confirms the existence of the error.log
file, and the second confirms the presence of the requests.log
file.
Now, you can check the functionality of the domain through a browser. You should see a message like:
Success! The example.com virtual host is working
This confirms that the virtual host has been successfully set up and is serving content. Repeat steps 5 and 6 for each new site, replacing the domain with the appropriate one.
In this tutorial, we've walked through installing and configuring Apache on CentOS 9, including setting up virtual hosts for multiple domains. We covered installation with dnf, configuring firewall rules, enabling Apache to start on boot, and managing its service using systemctl. We also explored SELinux configuration for proper permissions, ensuring Apache can read and write log files. With these steps, you'll have a functional web server ready to host sites and deploy content.