Install and Configure Apache webserver on CentOS7
In this example, we will be installing the Apache web server on CentOS version 7. Once installed and configured, we will then setup a sample website on the server to show how to install fully functional website, and confirm everything is working properly
Step 1: Update the software catalog
On a Linux machine, there is a catalog of available software and dependencies. We need to update the catalog from the latest version online.
To update the catalog, log into the CentOS server and open a terminal. In the terminal type:
sudo yum update
Step 2: Install Apache
To install the Apache webserver, which is referred to as httpd, type the following in the terminal
sudo yum install httpd
Step 3: Set Apache to auto-start at boot time
Currently Apache is NOT running and configured to NOT start when the system reboots. To configure the system to automatically start after reboot, type the following in the terminal
sudo systemctl enable httpd
Step 4: Start, Stop, Restart, and Reload Apache
We use the same systemctl command to manage and monitor most of the services/daemons on our Linux machine.
To start the Apache service, type:
sudo systemctl start httpd
To stop the Apache service, type:
sudo systemctl stop httpd
To restart the Apache service, which is a combination of stop and start, type:
sudo systemctl restart httpd
To reload the Apache service, which reloads the configuration in the running webserver, type:
sudo systemctl reload httpd
Step 5: Enable the firewall for web traffic
By default, CentOS block most inbound network traffic, so we need to configure the firewall to allow web traffic. Web traffic traditionally operates on ports 80 (http) and 443 (https). To open these ports in the firewall, type:
sudo firewall-cmd ––permanent ––add-port=80/tcp
sudo firewall-cmd ––permanent ––add-port=443/tcp
Once the firewall has been configured, we need to reload the configuration for the changes to take effect. Type:
sudo firewall-cmd --reload
Step 6: Confirm Apache is working
To confirm the webserver is now working, there are 3 ways we can validate it is functioning properly. It is often helpful to perform all 3 of these options to ensure there are no issues.
Validation 1: Confirm the service status.
On the webserver, type the command
sudo systemctl status httpd
This will return the status of the current httpd service. Confirm the status is Running and no major errors are reported.
Validation 2: Browse the website locally
On the webserver, open Firefox or other web browser. In the address bar, browse to http://localhost
This will browse to the local computer, and should work even if the network isnt configured correctly. You should see a sample page at this point.
Validation 3: Browse the website from another computer
On another computer, open a web browser and browse to the webserver’s name or IP address.
This will confirm the webserver is running, the network is configured properly, and that the firewall is configured.
Setup sample website
Now that the webserver is running, we need to configure a website. Depending on your needs, this may be a sample website downloaded from the internet, a website sent to you from a developer, or a custom website you are coding.
Step 1: Download website sample
In my example, I am going to download a free sample website template from https://www.free-css.com/free-css-templates. This site has several free sample sites that can be downloaded and used.
On the webserver, open Firefox and browse to the site. Find a sample site template you like and click to download. In my example, I am downloading the Selecao template from https://www.free-css.com/free-css-templates/page244/selecao.
If prompted by Firefox, choose to save the ZIP file on your computer in the default Downloads directory
Step 2: Unzip website files
The website template is downloaded as a ZIP file that contains several other files. Once the files are downloaded, we need to UnZIP them to use them as our website. First, in our terminal we need to change directory into our Downloads folder. Type the following
In the Downloads directory, you can type
ls to view all the files in your directory. You should see the ZIP file you downloaded. To unzip the file, type the following (substituting your correct file name)
unzip "Selecao Free Webste Template - Free-CSS.com.zip" -d web
This command will extract the files from the ZIP file into a directory called web
Step 3: Move files to /var/www directory
Now that the files are extracted, we need to move them to a location usable by the webserver. The webserver operates in the directory /var/www, so we need to move the files there. In the terminal, type:
sudo mv web/Selecao /var/www
We can confirm the files have moved by typing:
Step 4: Configure Apache to use the new website
Now that the files are in a location usable by the Apache webserver, we need to configure it to use the new files instead of the default sample page. This configuration is stored in the /etc/httpd/conf/httpd.conf file. To edit the file, type:
Using the arrow keys on the keyboard, scroll down the configuration file until you see the lines:
We want to update these lines to point to our new folders (substitute the correct directory name as appropriate)
To edit these lines, press i on your keyboard to enter insert mode. Use the arrow keys to navigate to the lines and edit them appropriately. When finished, pres Esc on your keyboard, followed by :wq to write and quit the vi editor. Hit enter to finish.
NOTE: There is a lot of things you can do in vi and this is not intended to be a vi tutorial. If you have problems, look online for more details on how to use the editor properly.
Step 5: Disable SELinux security (temporarily)
SELinux is another layer of security built into the Linux operating system and is intended to help minimize security breaches. This becomes a problem in our environment because we initially downloaded the files to our home directory (which SELinux identifies as user_home_t) and trying to access it as webserver files (which SELinux identifies as httpd_sys_content_t).
A temporary fix for this is to disable SELinux by typing the following. This will only disable it until the next reboot at which time it will be reenabled
sudo setenforce 0
NOTE: SELinux has potentially very large and complex configurations. If you have issues with SELinux, it is suggested to learn more about the technology instead of disabling it.
Step 6: Reload Apache configuration
Once the files have been copied, and the configuration files updated, we need to reload the configuration for it to take effect.
sudo systemctl reload httpd
Step 7: Validate the website is working
Finally, using a web browser, browse to the web server to confirm the updated website is listed.