Introduction
Apache is one of the most popular web servers in the world, and it is used to host websites and web applications. Installing Apache on CentOS 7 is a relatively straightforward process, and it can be done in a few simple steps. In this guide, we will show you how to install Apache web server on CentOS 7. We will also provide some basic configuration steps to get your web server up and running.
How to Install Apache Web Server on CentOS 7
1. Update the system
Before you start installing any package on your CentOS system, it is recommended that you update the system. You can do this by running the following command:
sudo yum update -y
2. Install Apache
Once the system is updated, you can install Apache web server using the following command:
sudo yum install httpd -y
3. Start Apache
Once the installation is complete, you can start the Apache service using the following command:
sudo systemctl start httpd
4. Enable Apache
To make sure that Apache starts automatically after a system reboot, you can enable it using the following command:
sudo systemctl enable httpd
5. Check the Status
You can check the status of the Apache service using the following command:
sudo systemctl status httpd
6. Allow Apache Through Firewall
If you have a firewall running on your system, you need to allow Apache through it. You can do this by running the following command:
sudo firewall-cmd –permanent –add-service=http
sudo firewall-cmd –reload
7. Test Apache
At this point, Apache should be up and running. You can test it by visiting your server’s IP address in your web browser. If everything is working correctly, you should see the Apache test page.
Introduction
Apache is a Linux application for running web servers. It is part of the LAMP stack – a package of applications that form the basis for most web technology. LAMP stands for Linux, Apache, MyPHP, and PHP.
This tutorial will show you how to install and configure the Apache web server on CentOS 7.
Prerequisites
- A user account with sudo privileges
- Access to a command line terminal (menu > application > utilities > terminal)
- The yum package manager, installed by default
Installing Apache on CentOS
Step 1: Update Software Versions List
Ensure you are using the latest versions of the software. In a terminal window, input the command:
sudo yum update
The system should reach out to the software repositories and refresh the list to the latest versions.
Step 2: Install Apache
To install Apache on your CentOS server, use the following command:
sudo yum install httpd
The system should download and install the Apache software packages.
Step 3: Activate Apache
To activate Apache, start its service first.
1. Enter the following command in a terminal window:
sudo systemctl start httpd
This will start the Apache service.
2. Next, set the Apache service to start when the system boots:
sudo systemctl enable httpd
Step 4: Verify Apache Service
Display information about Apache, and verify it’s currently running with:
sudo systemctl status httpd
Step 5: Configure firewalld to Allow Apache Traffic
In a standard installation, CentOS 7 is set to prevent traffic to Apache.
Normal web traffic uses the http protocol on Port 80, while encrypted web traffic uses the https protocol, on Port 443.
1. Modify your firewall to allow connections on these ports using the following commands:
sudo firewall-cmd ––permanent ––add-port=80/tcp
sudo firewall-cmd ––permanent ––add-port=443/tcp
2. Once these complete successfully, reload the firewall to apply the changes with the command:
sudo firewall-cmd ––reload
Step 6: Configure Virtual Hosts on CentOS 7 (optional)
Virtual hosts are different websites that you run from the same server. Each website needs its own configuration file.
Make sure these configuration files use the .conf extension, and save them in the /etc/httpd/conf.d/ directory.
There are a couple of best practices to use when you’re setting up different websites on the same server:
- Try to use the same naming convention for all your websites. For example:
- /etc/httpd/conf.d/MyWebsite.com.conf
- /etc/httpd/conf.d/TestWebsite.com.conf
- Use a different configuration file for each domain. The configuration file is called a vhost, for a virtual host. You can use as many as you need. Keeping them separate makes troubleshooting easier.
1. To create a virtual host configuration file, enter the following into a terminal window:
sudo vi /etc/httpd/conf.d/vhost.conf
This will launch the Vi text editor, and create a new vhost.conf file in the /etc/httpd/conf.d directory.
2. In the editor, enter the following text:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName MyWebsite.com
ServerAlias www.MyWebsite.com
DocumentRoot /var/www/html/MyWebsite.com/public_html/
ErrorLog /var/www/html/MyWebsite.com/logs/error.log
CustomLog /var/www/html/MyWebsite.com/logs/access.log combined
</VirtualHost>
Save the file and exit.
3. Next, enter the following command to create a directory for you to store your website files in:
sudo mkdir /var/www/MyWebsite/{public_html, logs}
4. Restart the Apache service to apply your changes by entering:
sudo systemctl restart httpd
Once the system finishes, you should be able to open a browser window to MyWebsite.com and see a default Apache test page.
You can replace MyWebsite above with the name of your domain. If you are hosting more than one domain, make sure you create a new directory in /var/www/ for each one. You can copy the code block in your /etc/httpd/conf.d/vhost.conf file, and replace MyWebsite with another domain name that you’re hosting.
Apache Directories and Files
One of the main ways Apache functions is through configuration files. They are located at /etc/httpd.
Apache has a main configuration file: /etc/httpd/conf/httpd.conf .
If there are any other configuration files, they are included in the main configuration file. They should use the .conf extension and should be stored in the /etc/httpd/conf.d/ directory.
You can enhance Apache’s functionality by loading additional modules.
The configuration files for these modules should be stored in: /etc/httpd/conf.modules.d/ directory.
Log files record all the activity of the Apache service, including client activity on the websites your system is hosting. These logs can be found in: /var/log/httpd/.
Commands For Managing Apache Service
Other commands that you can use to control the Apache service include:
Stop Apache Service:
sudo systemctl stop httpd
Prevent or disable Apache from starting when the system boots:
sudo systemctl disable httpd
Re-enable Apache at boot:
sudo systemctl enable httpd
Restart Apache and apply any changes you have made:
sudo systemctl restart httpd
Conclusion
Great job, you have installed Apache on your CentOS 7 server. The basic installation and management of the Apache service are simple.
Ready to build out your application stack? Read our guide on installing MySQL on CentOS.
How to Install Apache Web Server on CentOS 7
Apache is an open-source web server that can be used to serve web pages on the internet. It is one of the most popular web servers in the world, and is commonly used to host websites on Linux servers. In this tutorial, we will show you how to install Apache web server on CentOS 7.
Prerequisites
Before you begin with this guide, you should have a non-root user with sudo privileges set up on your server. You can learn how to set up a user with these privileges by following our initial server setup guide for CentOS 7.
Step 1 — Installing Apache
The Apache web server is available in the default CentOS 7 repositories. We can install it by running the following command:
sudo yum install httpd
Once the installation is complete, start the Apache service and enable it to start on boot with the following command:
sudo systemctl start httpd.service
sudo systemctl enable httpd.service
You can check the status of the Apache service with the following command:
sudo systemctl status httpd.service
You should see the following output:
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2016-07-07 11:53:50 EDT; 1min 32s ago
Main PID: 5951 (httpd)
CGroup: /system.slice/httpd.service
├─5951 /usr/sbin/httpd -DFOREGROUND
├─5952 /usr/sbin/httpd -DFOREGROUND
├─5953 /usr/sbin/httpd -DFOREGROUND
├─5954 /usr/sbin/httpd -DFOREGROUND
└─5955 /usr/sbin/httpd -DFOREGROUND
Jul 07 11:53:50 centos7 systemd[1]: Starting The Apache HTTP Server...
Jul 07 11:53:50 centos7 systemd[1]: Started The Apache HTTP Server.
Step 2 — Adjusting the Firewall
If you have a firewall enabled on your server, you need to open port 80 to allow HTTP traffic. You can do this by running the following command:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --reload
You can verify that the port is open by running the following command:
sudo firewall-cmd --zone=public --list-services
You should see the following output:
http
ssh
Step 3 — Testing Apache
At this point, Apache is installed and running. We can test it by visiting our server’s public IP address in our web browser. You should see the Apache test page, which should look something like this:
If you see this page, then your web server is now correctly installed and accessible via the internet.
Conclusion
In this guide, we have shown you how to install Apache web server on CentOS 7. You should now have a working web server that you can use to host your websites.