Apache Web server installation configuration method

  • 2020-12-09 01:25:16
  • OfStack

Learn how to host your own website on Apache, a reliable, popular and easy to configure Web server.

I have hosted my own website for many years. Since switching from OS/2 to Linux more than 20 years ago, I have always used Apache as my server software. Apache is reliable, popular, and easy to install with basic configuration. For more complex Settings, such as multiple websites, it's not that difficult.

The installation and configuration of Apache Web server must be performed as root. The configuration of the firewall also needs to be performed as root. The result of viewing the installation configuration using a browser should be done as a non-ES15en user. (I use student on my virtual host.)

The installation

Note: The experimental environment I used was a virtual machine with Fedora 27. Apache version was 2.4.29. If you are using different distributions or different versions of Fedora, the location and content of your commands and configuration files may vary. However, the configuration rows that you need to modify are the same.

The Apache Web server is very easy to install. On my CentOS 6.x server, it requires only one simple yum command. It installs all the necessary dependencies (if needed). I used the dnf command below on my Fedora virtual machine. The syntax for dnf and yum is the same, except for the name of the command itself.

dnf -y install httpd

This virtual machine is a very basic desktop environment that I'm using as a test platform for writing books. Even on this system, only six dependencies were installed, which took one minute.

Apache all configuration files in the/etc/httpd conf and/etc/httpd/conf d. The site's data is at /var/www by default, but you can change it if you wish.

configuration

Apache main configuration file is/etc/httpd/conf/httpd conf. It contains many configurations that do not need to be changed in the base installation. In fact, you can get a simple web site up and running with just a few changes to this file. The file is very large, so I won't confuse this article with a lot of unnecessary stuff, but only the instructions that need to be changed.

First, take some time to familiarize yourself with the 1 httpd.conf file. One of the reasons I like Red Hat is that its configuration file annotations are very detailed. The httpd.conf file is no exception, as it is well commented. You can use these comments to understand the configuration of the file.

The first item to modify is the Listen configuration item, which defines the IP address and port that Apache wants to listen on page requests. For now, you just need to make the site accessible locally, so use the localhost address. When finished, the line should look like this: (LCTT's IP address is 127.0.0.1, and 80 is port)

Listen 127.0.0.1:80

By setting this configuration item to the IP address of localhost, Apache will listen only for connections from local hosts. If you want the Web server to listen for connections from a remote host, you can use the host's external IP address.

The DocumentRoot configuration item specifies the location of the HTML files that make up the web page. This configuration item does not need to change because it already points to the standard location. The line should look like this:

DocumentRoot "/var/www/html"

The Apache installation package creates the /var/www directory. Use this configuration item to do this if you want to change the location where you store your web site files. For example, you might want to use a different name for the www directory to more explicitly identify the site. It could look something like this:

DocumentRoot "/var/mywebsite/html"

These are the Apache configuration items that need only 1 modification to create a simple website. For this small exercise, only 1 modification has been made to the ES108en.conf file (Listen configuration item). The other configuration items are temporarily not configured for a simple Web server.

Another change is to open port 80 in our firewall. I use iptables as my firewall, so I changed the /etc/sysconfig/iptables file to add the HTTP protocol allowed. The entire file looks like this:


# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

I added a new line to the penultimate line of the file, which allows traffic to be entered on port 80. Now I reload the iptables configuration file.


[root@testvm1 ~]# cd /etc/sysconfig/ ; iptables-restore iptables

Create the index.html file

index.html is the default file when you use a domain name to visit a website rather than a specific web page. In /var/www/html, create a file named index.html and add the string Hello World to it. You don't need to add any HTML flags to do this. The only job of the web server is to provide a stream of textual data, and the server has no idea what the data is or how to render it. It simply passes data to the requesting host.

After saving the file, set the ownership to ES143en.apache.


[root@testvm1 html]# chown apache.apache index.html

Start the Apache

Apache is easy to start. The current version of Fedora uses systemd. Run the following command to start it, and then check the status of the server: (LCTT)


[root@testvm1 ~]# systemctl start httpd
[root@testvm1 ~]# systemctl status httpd
 low  httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Active: active (running) since Thu 2018-02-08 13:18:54 EST; 5s ago
   Docs: man:httpd.service(8)
 Main PID: 27107 (httpd)
  Status: "Processing requests..."
  Tasks: 213 (limit: 4915)
  CGroup: /system.slice/httpd.service
       ├ ─ 27107 /usr/sbin/httpd -DFOREGROUND
       ├ ─ 27108 /usr/sbin/httpd -DFOREGROUND
       ├ ─ 27109 /usr/sbin/httpd -DFOREGROUND
       ├ ─ 27110 /usr/sbin/httpd -DFOREGROUND
       └ ─ 27111 /usr/sbin/httpd -DFOREGROUND
Feb 08 13:18:54 testvm1 systemd[1]: Starting The Apache HTTP Server...
Feb 08 13:18:54 testvm1 systemd[1]: Started The Apache HTTP Server.

The commands on your server may be different. On the Linux system using the SystemV startup script, the commands are as follows:


[root@testvm1 ~]# service httpd start
Starting httpd: [Fri Feb 09 08:18:07 2018]     [ OK ]
[root@testvm1 ~]# service httpd status
httpd (pid 14649) is running...

If you have a browser like Firefox or Chrome on your host, you can use URL localhost on the URL line of your browser to display your web page, even though it looks simple. You can also view web pages using a text-mode web browser like Lynx. First, install Lynx (if it has not already been installed).


[root@testvm1 ~]# dnf -y install lynx

Then use the following command to display the web page.


[root@testvm1 ~]# lynx localhost

It turns out in my terminal it looks like this. I've removed a lot of white space on the page.


 Hello World
<snip>
Commands: Use arrow keys to move, '?' for help, 'q' to quit, '<-' to go back.
 Arrow keys: Up and Down to move. Right to follow a link; Left to go back.
 H)elp O)ptions P)rint G)o M)ain screen Q)uit /=search [delete]=history list```

Next, edit your ES184en.html file and add 1 HTML tag to make it look like this:

<h1>Hello World</h1>

Now refresh the browser. For Lynx, use the key combination Ctrl + R. The results look a little different. If your terminal supports color text is colored, Lynx will show the title, which is now centered. In the GUI browser, text will be displayed in large font.


 Hello World
<snip>
Commands: Use arrow keys to move, '?' for help, 'q' to quit, '<-' to go back.
 Arrow keys: Up and Down to move. Right to follow a link; Left to go back.
 H)elp O)ptions P)rint G)o M)ain screen Q)uit /=search [delete]=history list

Afterword.

As you can see from this little exercise, it is easy to set up an Apache Web server. It depends on your distribution and the version of Apache that is available with that distribution. In my environment, this is a very simple exercise.


Related articles: