In this tutorial, we will show you how to install and configuration of Apache on your CentOS 7. For those of you who didn’t know, Apache, also known as the Apache HTTP Server, is a popular open-source web server software that powers a significant portion of the internet. It is known for its reliability, security, and flexibility, making it a preferred choice for hosting websites and web applications.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation Apache web server on a CentOS 7 server.
Prerequisites
- A server running one of the following operating systems: CentOS 7.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- Basic knowledge of Linux command line.
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Apache on CentOS 7
Step 1. To ensure a smooth installation process and maintain the security of your server, it is essential to update your system before installing any new software. Updating the system will fetch the latest security patches, bug fixes, and software versions available in the repositories. To update your CentOS 7 system, run the following command:
yum clean all yum -y update
Step 2. Installing Apache on CentOS 7.
Apache is available in CentOS 7’s default software repositories, making the installation process straightforward. To install Apache, run the following command:
yum install httpd openssl mod_ssl
Once Apache has finished installing, the httpd
service will need to be started and enabled so it will run automatically when the server starts:
sudo systemctl restart httpd sudo systemctl status httpd sudo systemctl enable httpd
Once the installation is complete, you can verify that Apache is installed correctly by running the following command:
httpd -v
To verify that Apache is running, use the following command:
sudo systemctl status httpd
If Apache is running correctly, you will see an output similar to this:
● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2024-04-14 10:30:00 UTC; 5s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 1234 (httpd) Status: "Processing requests..." Tasks: 6 Memory: 23.0M CGroup: /system.slice/httpd.service ├─1234 /usr/sbin/httpd -DFOREGROUND ├─1235 /usr/sbin/httpd -DFOREGROUND ├─1236 /usr/sbin/httpd -DFOREGROUND ├─1237 /usr/sbin/httpd -DFOREGROUND ├─1238 /usr/sbin/httpd -DFOREGROUND └─1239 /usr/sbin/httpd -DFOREGROUND
You can verify that Apache is really running by opening your favorite web browser and entering the URL http://your-server's-address
. you should get a “Testing 123″ page similar to the image below:
Step 3. Configure Firewalld for Apache.
By default, CentOS 7 comes with a firewall (firewalld) that blocks incoming network traffic. To allow access to your Apache web server from the internet, you need to configure the firewall to permit HTTP (port 80) and HTTPS (port 443) traffic.
To allow HTTP and HTTPS traffic, run the following command:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
After adding the necessary rules, reload the firewall to apply the changes:
systemctl restart firewalld.service
Step 4. Configure Virtual Hosts.
Virtual hosts allow you to host multiple websites on a single Apache server. Each virtual host can have its own domain name, document root, and configuration settings.
Create a new configuration file for your virtual host in the /etc/httpd/conf.d/
directory. For example, sudo nano /etc/httpd/conf.d/example.com.conf
Add the following configuration to the file, replacing example.com with your domain name and /var/www/example.com/html
with the desired document root path:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined </VirtualHost>
Save the file and exit the editor.
Next, create the document root directory for your virtual host:
sudo mkdir -p /var/www/example.com/html
Set the appropriate ownership and permissions for the document root directory:
sudo chown -R apache:apache /var/www/example.com/html sudo chmod -R 755 /var/www/example.com
Restart the Apache service to load the new virtual host configuration:
sudo systemctl restart httpd
Your virtual host is now set up and ready to serve content. Place your website files in the document root directory (/var/www/example.com/html
) and access your website using the domain name.
Congratulations! You have successfully installed Apache. Thanks for using this tutorial for installing the Apache webserver on the CentOS 7 system. For additional help or useful information, we recommend you check the official Apache website.