1. Introduction to Apache
Apache is a well-known open source web server.
The early Apache server was maintained by the Apache Group until June 1999 when the Apache Group established a non-profit organization in Delaware, the Apache Software Foundation (ASF).
The website needs a web server to structure, web design artists (flash, dreamweaver, firework, photoshop, etc.), web developers (php, .net, jsp, etc.), after the website is built, we need to maintain, optimize, debug, and structure Extension and expansion, etc.
Simply put, if we want to browse a webpage, basically all websites use the http protocol for data transmission! As for how to transmit, we don't need to go into it as an operation and maintenance. That is something that html front-end developers must consider!
Apache is composed of three levels: kernel, standard modules and modules provided by third parties.
2. Apache under CentOS
1. There are two types of websites
Static website: Apache, Nginx, html
Dynamic website: php/perl/python, jsp(java), .net
2. Overview of Apache Services
Package: httpd, httpd-devel, httpd-manual
Service type: daemon started by systemd
Configuration unit: /usr/lib/systemd/system/httpd.service
Daemon: /usr/sbin/httpd
Port: 80(http), 443(https)
Configuration: /etc/httpd/
Web document: /var/www/html/
Apache logging directory: /var/log/httpd/
There are two kinds of files in this directory:
access_log # Record the client's access to Apache information, such as the client's ip
error_log # Record the error information of the access page
Record log of Apache service startup:
/var/log/messages # This log is a large collection of the system
3. Preparations for configuring the Apache server
System platform: CentOS 7.3
DHCP Server: 192.168.1.20
Step 1: The
server sets a static IP
Step 2: Change the host name, write /etc/hosts record
[root@Apache ~]# echo "192.168.1.20 Apache" >> /etc/hosts --Add ip and hostname to /etc/hosts
[root@Apache ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.20 Apache
Step 3: Turn off the firewall
[root@Apache ~]# systemctl stop firewalld - temporarily turn off the firewall
[root@Apache ~]# systemctl disable firewalld - permanently turn off the firewall
Step 4: Close selinux
Temporary closure:
[root@Apache ~]# setenforce 0
setenforce: SELinux is disabled
Permanently closed:
[root@Apache ~]# vim /etc/selinux/config
SELINUX=disabled # Change enforcing to disabled
[root@Apache ~]# reboot --Restart the system to take effect permanently
Three, the establishment and configuration of Apache service
1. Install Apache software using yum package
[root@Apache ~]# yum -y install httpd*
[root@Apache ~]# rpm -qa | grep httpd --View the installed http package
httpd-manual-2.4.6-67.el7.centos.6.noarch
httpd-tools-2.4.6-67.el7.centos.6.x86_64
httpd-2.4.6-67.el7.centos.6.x86_64
httpd-devel-2.4.6-67.el7.centos.6.x86_64
After successful installation, the following two files will be generated
/etc/httpd/conf/httpd.conf # Main configuration file
/var/www/html # Default website home directory
2. Know the main parameters in the configuration file
[root@Apache ~]# vim /etc/httpd/conf/httpd.conf
31 serverRoot "/etc/httpd" # Directory for storing configuration files
42 Listen 80 # Apache service listening port
66 User apache # The user of the child process
67 Group apache # child process group
86
ServerAdmin root@localhost # Set the administrator email address
119 DocumentRoot "/var/www/html" --Website home directory
# Set the attributes of the specified directory of DocumentRoot
131 <Directory "/var/www/html"> # Website container start identification
144 Options Indexes FollowSymLinks # When the homepage is not found, it will be presented as a directory, and links to other than the website root directory are allowed
151 AllowOverride None # none does not use .htaccess control, all allows
156 Require all granted # granted means to run all access, denied means to deny all access
157 </Directory> # End of container
164 DirectoryIndex index.html # Define the homepage file. When accessing the website directory, if there is a defined homepage file, the website will automatically visit
316 AddDefaultCharset UTF-8 # Character encoding, if Chinese, it may need to be changed to gb2312 or gbk, depending on the default encoding of your website files
3. Start the Apache website
[root@Apache ~]# systemctl start httpd.service
[root@Apache ~]# lsof -i:80 --Check whether the httpd service is started
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 20585 root 4u IPv6 402909 0t0 TCP *:http (LISTEN)
httpd 20586 apache 4u IPv6 402909 0t0 TCP *:http (LISTEN)
httpd 20587 apache 4u IPv6 402909 0t0 TCP *:http (LISTEN)
httpd 20588 apache 4u IPv6 402909 0t0 TCP *:http (LISTEN)
httpd 20589 apache 4u IPv6 402909 0t0 TCP *:http (LISTEN)
httpd 20590 apache 4u IPv6 402909 0t0 TCP *:http (LISTEN)
After the startup is successful, use the browser: enter your IP address and you will see a Red Hat welcome page:
[root@Apache ~]# firefox 192.168.1.20
It is not very convenient to open the browser every time, so we can use a text browser to facilitate testing.
[root@Apache ~]# yum -y install elinks - install elinks text browser
[root@Apache ~]# elinks 192.168.5.20 --Press ctrl + c to exit
[root@Apache ~]# curl 192.168.1.20 --or use curl
Example 1: Create a website homepage, create a homepage file in the root directory of the website
Format 1:
[root@Apache ~]# echo'main page'> /var/www/html/index.html --Add content to index.html
[root@Apache ~]#systemctl restart httpd.service --Restart the service
[root@Apache ~]# firefox http://192.168.1.20 --Test in the browser, the information displayed is the content we entered just now main page
Format 2:
[root@Apache ~]# vim /var/www/html/index.html - Write the homepage file in the format of html tags, add the content
<html>
<head>
<title>Test site</title>
</head>
<body>
<center><h1>Welcome to the test site!@_@</h1></center>
</body>
</html>
[root@Apache ~]# systemctl restart httpd.service --Restart the service
[root@Apache ~]# firefox http://192.168.1.20 --Test in the browser, the information displayed is the content we entered just now main page
Example 2: Modify the website home directory to: /www directory
[root@Apache ~]# mkdir /www --Create /www directory
[root@Apache ~]# vim /etc/httpd/conf/httpd.conf
119 DocumentRoot "/www" - modify the website root directory to /www
131 <Directory "/www"> --change this correspondingly to /www
[root@Apache ~]# systemctl restart httpd.service --Restart the apache service
[root@Apache ~]# echo "This is the newly modified website home directory/www"> /www/index.html --Add content to index.html
[root@Apache ~]# firefox http://192.168.1.20 --Visit the website and see the newly added information under the root directory of the new website
Example 3: Modify the homepage type or homepage name
[root@Apache ~]# vim /etc/httpd/conf/httpd.conf
164 DirectoryIndex index.php --Change index.html to index.php
[root@Apache ~]# systemctl reload httpd.service --Reload service or restart
[root@Apache ~]# echo'php main page'> /www/index.php - At this time, we create an index.php page, and then use the browser to visit it and you can see it
[root@Apache ~]# elinks 192.168.1.20