Read in 3 minutes
How To Install Apache on CentOS 7
Series: Install LAMP Stack on CentOS 7
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.
Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.
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 you are running a firewall you need to open HTTP and HTTPS ports,
443. You can 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
Check Apache status
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
- The main Apache configuration file is
- All config files ending with
.conflocated in the
/etc/httpd/conf.ddirectory are included in main Apache configuration file.
- Configuration files which are responsible for loading various Apache modules are located in the
- For better maintainability it is recommended to create a separate configuration file (vhost) for each domain.
- New Apache vhost files must end with
.confand be stored in
/etc/httpd/conf.ddirectory. 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.comthen you the configuration file should be named
- Apache log files (
error_log) are located in the
/var/log/httpd/directory. It is recommended to have a different
errorlog files for each vhost.
- You can set your domain document root directory to any location you want. The most common locations for webroot include:
You have successfully installed Apache on your CentOS 7 server. You’re now ready to start deploying your applications and use Apache as a web or proxy server.
If you have any question or feedback feel free to leave a comment.