Apache Web server installation configuration tutorial in CentOS 7

  • 2020-11-18 06:34:27
  • OfStack

Learn how to host your own website on Apache in CentOS 7, 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. The 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. Firewall configuration also needs to be performed as root. The result of viewing the installation configuration using a browser should be done as a non-ES16en user. I use the user student on my virtual host.

The installation

Note: The experimental environment I used was a virtual machine with Fedora 27 installed. 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.ES30en server, it requires only a 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.

All configuration files for Apache are located at /etc/httpd/conf and /etc/httpd/conf.d . The site's data is located by default /var/www But you can change it if you want to.

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 ES63en.conf file is no exception because it is well commented. You can use these comments to understand the configuration of the file.

The first to change is the Listen configuration item, which defines the ADDRESS and port of IP to listen for 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)

Listen127.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 external IP address of the host.

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 ES103en.conf file (Listen configuration item). Other configuration items are temporarily not required 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 oruse system-config-firewall
# please donot ask us to add additional ports/services to thisdefault 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 page. In /var/www/html, create a file named ES130en.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 textual data stream, 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 ES138en.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 -TheApache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu2018-02-0813: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
Feb0813:18:54 testvm1 systemd[1]:StartingTheApache HTTP Server...
Feb0813:18:54 testvm1 systemd[1]:StartedTheApache HTTP Server.

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


[root@testvm1 ~]# service httpd start
Starting httpd:[FriFeb0908:18:072018]     [ 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, as simple as it seems. 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.


HelloWorld
<snip>
Commands:Use arrow keys to move,'?'for help,'q' to quit,'<-' to go back.
Arrow keys:UpandDown 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 ES183en.html file and add 1 HTML tag to make it look like this:

< h1 > Hello World < /h1 >

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


HelloWorld
<snip>
Commands:Use arrow keys to move,'?'for help,'q' to quit,'<-' to go back.
Arrow keys:UpandDown 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.

But more than that, the Apache is very flexible and powerful. Next month, I'll talk about using a single Apache to host multiple websites.

conclusion


Related articles: