How To Install Apache 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.
Once the installation is completed, enable and start the Apache service:
Finally to verify if everything works properly, open your server IP address
To start it again, type:
To restart the Apache service:
To reload the Apache service after you made some configuration changes:
If you want to disable the Apache service to start at boot:
And to re-enable it again:
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 calledhttpd. To install the package run the following command:
sudo yum install httpd
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
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
sudo systemctl start httpd
sudo systemctl restart httpd
sudo systemctl reload httpd
sudo systemctl disable httpd
sudo systemctl enable httpd
Apache Configuration File’s Structure and Best Practices
- All Apache configuration files are located in the
/etc/httpddirectory. - The main Apache configuration file is
/etc/httpd/conf/httpd.conf. - 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
/etc/httpd/conf.modules.ddirectory. - 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/etc/httpd/conf.d/mydomain.com.conf - Apache log files (
access_loganderror_log) are located in the/var/log/httpd/directory. It is recommended to have a differentaccessanderrorlog 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>
Comments
Post a Comment