Setting Up a Linux Web Server Using Apache HTTP

There are many reasons to build your own web server. Maybe you want to host a website or even maintain your own package mirror (how ambitious of you). This post will show you how to set up your web server using httpd and then go a step further to show how you can use it to host your own yum repository.

Note: This blog pairs well with another one I wrote that gives more detail about creating your own yum repo using createrepo. That blog can be found here

Let's get started!

Step 1. Install HTTPD

The simplest way to install HTTPD on CentOS/RedHat is to use yum:

yum install httpd  

You can also download it from the Apache website.

Step 2. Ensure the Service is Running and the Ports/Firewalls are Open

Starting the service

You can start, stop, restart, and check the status of the service using systemctl.

Ensure the service is running:

systemctl start httpd  
systemctl status httpd  
Opening the port(s)

Now that the service is running, you can check the port on which it is listening (usually port 80) by looking under /etc/httpd/conf/httpd.conf.

Once you know the port number, ensure that your firewall and security groups (if using AWS or a similar cloud provider) allow incoming connections on that port.

For example, if you are running iptables you can check its configuration for port 80 by issuing service iptables status | grep 80.

If the port is not open, you will need to run some commands like the following:

iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT  
service iptables save  

Now run service iptables status | grep 80 again to make sure it is working as expected.

For more on iptables, see this article.

Step 3. Test Your Web Server

HTTPD will create a directory under /var/www/html by default. This is the directory from which all content will be served. So if you have webpages or files that you want to access via web calls, store them here!

Let's use an HTML file as an example. Copy the HTML below and store it as /var/www/html/test.html:


You can test your webserver by issuing a command like the following:

curl http://myserverip/test.html  

*Notice that since the DocumentRoot in the config file specifies /var/www/html, we do not need that full path in the URL. The first / after the IP or FQDN puts us in the document root automatically.

If all went well, you chould see the HTML output in your terminal.

You can also test this in a browser, in which case the HTML would render and you would see "Success!!" all alone on a webpage in the browser.

Troubleshooting common issues

The most common problem is a 403 Forbidden error. The likely culprit is that the permissions on your webserver are incorrect. To remedy this, you need to make sure the group that owns the directories and files under /var/www/html is root:apache and the permissions are at least 644 for the files and at least 610 for the directories. To make it easy, I will just use 654 on everything under /var/www/html.

cd /var/www/html  
chown -R root:apache *  
chmod -R 654 *  
ls -l  

Now test it again to make sure it works the way you expect it to work.

And that's it! Now you have a working web server - congrats!

Good Use Case

One reason that you might need a webserver is to host your own yum repo for installing and managing packages within an enclave. Once you have the steps above completed, this is very simple to do. Follow the directions in this blog for using createrepo and just place the packages under /vaw/www/html/local-repo or something like that. Then in your etc/yum.repos.d/local.repo file you would just change the URL to have http://myserverip/local-repo

Enjoy being an Apache warrior and let me know if you have any questions!
[email protected]