Apache HTTP server is the most popular web server in the world. It is a free, open-source and cross-platform HTTP server providing powerful features which can be extended by a wide variety of modules. The following instructions describe how to install and manage the Apache web server on your CentOS 7 machine.
Install Apache
Apache is available in the default CentOS repositories and the installation is pretty straight forward. On CentOS and RHEL the Apache package and the service is called httpd
. To install the package run the following command:
sudo yum install httpd
Once the installation is completed, enable and start the Apache service:
sudo systemctl enable httpd
sudo systemctl start httpd
Adjust the Firewall
If your server is protected by a firewall you need to open HTTP and HTTPS ports, 80
and 443
. Use the following commands to open the necessary ports:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
Verifying Apache Installation
Now that we have Apache installed and running on our CentOS 7 server we can check the status and the version of the Apache service, with:
sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2018-04-26 07:13:07 UTC; 11s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 3049 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─3049 /usr/sbin/httpd -DFOREGROUND
├─3050 /usr/sbin/httpd -DFOREGROUND
├─3051 /usr/sbin/httpd -DFOREGROUND
├─3052 /usr/sbin/httpd -DFOREGROUND
├─3053 /usr/sbin/httpd -DFOREGROUND
└─3054 /usr/sbin/httpd -DFOREGROUND
sudo httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Oct 19 2017 20:39:16
Finally to verify if everything works properly, open your server IP address http://YOUR_IP
in your browser of choice, and you will see the default CentOS 7 Apache welcome page as shown below:
Manage the Apache service with systemctl
We can manage the Apache service same as any other systemd unit.
To stop the Apache service, run:
sudo systemctl stop httpd
To start it again, type:
sudo systemctl start httpd
To restart the Apache service:
sudo systemctl restart httpd
To reload the Apache service after you made some configuration changes:
sudo systemctl reload httpd
If you want to disable the Apache service to start at boot:
sudo systemctl disable httpd
And to re-enable it again:
sudo systemctl enable httpd
Apache Configuration File’s Structure and Best Practices
- All Apache configuration files are located in the
/etc/httpd
directory. - The main Apache configuration file is
/etc/httpd/conf/httpd.conf
. - All config files ending with
.conf
located in the/etc/httpd/conf.d
directory are included in main Apache configuration file. - Configuration files which are responsible for loading various Apache modules are located in the
/etc/httpd/conf.modules.d
directory. - For better maintainability it is recommended to create a separate configuration file (vhost) for each domain.
- New Apache vhost files must end with
.conf
and be stored in/etc/httpd/conf.d
directory. You can have as many vhosts as you need. - It is a good idea to follow a standard naming convention, for example if your domain name is
mydomain.com
then you the configuration file should be named/etc/httpd/conf.d/mydomain.com.conf
- Apache log files (
access_log
anderror_log
) are located in the/var/log/httpd/
directory. It is recommended to have a differentaccess
anderror
log files for each vhost. - You can set your domain document root directory to any location you want. The most common locations for webroot include:
/home/<user_name>/<site_name>
/var/www/<site_name>
/var/www/html/<site_name>
/opt/<site_name>