CentOS 7 build Web sites using the Apache Service

In the Internet environment, Web services is undoubtedly the most popular applications. With Web sites, enterprises can fully display their products, promote corporate image. Web site also provides businesses with customers, e-commerce trading platform rich web applications, deployment and maintenance of Web services operation and maintenance engineers must master a skill.

Windows build Web services can refer Bowen: Windows Server 2016 set up IIS (web) service

Apache origin

1.Apache HTTP Server is an outstanding representative of open source software projects, web browsing services provide a standards-based network protocols HTTP, Apache server can run on Linux, UNIX, Windows and other operating systems;

2.Apache server integration for a number of Web server program before appearing, formed after the perfect software, its name comes from "A Patchy Server", the meaning is "to modify (patch) based on the code of the original Web service program forming a server program ";

3. The current Apache project has been responsible for the management and maintenance of the ASF. ASF's official website is http://www.apache.org

Apache's main features:

  • Open source;
  • Cross-platform applications;
  • Web supports a variety of programming languages;
  • Modular design;
  • Running very stable;
  • Good security.

Install httpd server
before configuring Apache web service, you need to properly install the httpd server software. install httpd server can use RPM to install, compile and install the source code of these two methods. The former is relatively simple, fast, but there are some limitations in function, in the actual production environment, the way to use the source code to compile and install a way to be more common.

This experiment will download the source code package httpd-2.4.25.tar.gz for example, describes the installation process. (Source Package httpd-2.4.25.tar.gz can download the Linux commune Station resources:

------------------------------------------Dividing line------ ------------------------------------

Specific download directory / 2019 data / Aug / 16 / CentOS 7 build Web sites using the Apache service /

------------------------------------------Dividing line------ ------------------------------------

Steps:
CentOS 7 build Web sites using the Apache Service

Specific experimental procedures

First, the preparatory work

In order to avoid the phenomenon of port conflicts, conflict program, it is recommended to uninstall using the httpd RPM mode installation.

[root @ localhost ~] # rpm -e httpd --nodeps 
error: httpd package is not installed

Apache configuration and operation needed to support the package apr, pcre the like, and therefore should be pre-mounted system disk image and software development kits (devel), in order to provide appropriate support to ensure successful completion of the installation of Apache.
Need rpm package are:

[root@localhost Packages]# rpm -ivh apr-1.4.8-3.el7.x86_64.rpm
[root@localhost Packages]# rpm -ivh apr-devel-1.4.8-3.el7.x86_64.rpm 
[root@localhost Packages]# rpm -ivh cyrus-sasl-devel-2.1.26-20.el7_2.x86_64.rpm
[root@localhost Packages]# rpm -ivh expat-devel-2.1.0-8.el7.x86_64.rpm 
[root@localhost Packages]# rpm -ivh libdb-devel-5.3.21-19.el7.x86_64.rpm 
[root@localhost Packages]# rpm -ivh openldap-devel-2.4.40-13.el7.x86_64.rpm 
[root@localhost Packages]# rpm -ivh apr-util-1.5.2-6.el7.x86_64.rpm 
[root@localhost Packages]# rpm -ivh apr-util-devel-1.5.2-6.el7.x86_64.rpm 
[root@localhost Packages]# rpm -ivh pcre-devel-8.32-15.el7_2.1.x86_64.rpm 
[root@localhost Packages]# rpm -ivh pcre-8.32-15.el7_2.1.x86_64.rpm 

According to successfully complete the installation.

Second, the source compiler installation

The downloaded source code httpd-2.4.25.tar.gz uploaded to the server locally, then began to compile and install.

1. Unwrap

[root@localhost ~]# tar zxf httpd-2.4.25.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/httpd-2.4.25/

2. Configure
depending on the application servers, the flexibility to set different custom items, to learn a variety of configuration options are available and what they mean, you can do "./configure --help" command, "/" indicates that the current directory, the following configure some of the most commonly used configuration options:

[root@localhost httpd-2.4.25]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi

The above-described configuration command, the meaning of each option:
CentOS 7 build Web sites using the Apache Service
3. compilation and installation
Once configured, perform "make" command to compile the source code to convert an executable program; then execute "make install" command to complete the final assembly process.

[root@localhost httpd-2.4.25]# make && make install

Third, confirm the installation directory
because the specified installation directory is / usr / local / httpd, httpd therefore a variety of programs and services, modules, help files will be copied under this directory.

[root@localhost httpd-2.4.25]# ls /usr/local/httpd
bin    cgi-bin  error   icons    logs  manual
build  conf     htdocs  include  man   modules

In / usr local post installation / / httpd directory, subdirectories main purposes:
CentOS 7 build Web sites using the Apache Service

service httpd directory structure:
the main directory and file
services directory: / usr / local / httpd /
master configuration file: /usr/local/httpd/conf/httpd.conf
Web Directory: / usr / local / httpd / htdocs /
Services script: / usr / local / httpd / bin / apachectl
execute the program: / usr / local / httpd / bin / httpd
access log: / usr / local / httpd / log / access_log
error log: / usr / local / httpd / log / error_log

Fourth, optimize the execution path
through service httpd source installation, the program path is not in the default search path, in order to make the service more convenient in use, you can add a symbolic link to relevant procedures.

[root@localhost httpd-2.4.25]# ln -s /usr/local/httpd/bin/* /usr/local/bin
[root@localhost httpd-2.4.25]# ls -l /usr/local/bin/httpd /usr/local/bin/apachectl
lrwxrwxrwx. 1 root root 30 7月  13 19:20 /usr/local/bin/apachectl -> /usr/local/httpd/bin/apachectl
lrwxrwxrwx. 1 root root 26 7月  13 19:20 /usr/local/bin/httpd -> /usr/local/httpd/bin/httpd

So that the associated command will not need to enter a lengthy path. such as:

[root @ localhost httpd-2.4.25] # httpd -v // see the program version 
Server Version: the Apache / 2.4.25 (Unix) 
Server Built: Jul 13 2019 18:49:20

Fifth, add the httpd service system
CentOS under 7 system, compile and install the program to add a system service in two ways:
① add a system service by chkconfig command, and then by the systemctl management (Note that you can not set this way the boot program from the start);
② in / lib / systemd / system / directory, written in the end unit .service (unit) profile manually.

1) add a system service using chkconfig
If you want to add httpd as a system service, for management by chkconfig, need to set up a controlled server script.

[root @ localhost ~] # cp / usr / local / httpd // bin / apachectl /etc/init.d/httpd 
[root @ localhost ~] # vim /etc/init.d/httpd // add the following 
#! / bin / the bash 
# the chkconfig: 21 is 35 85        
\\ service identification parameter, starting in the level 3, 5; startup and shutdown sequence are 85,21 
#description: the startup the Script for the HTTP Server the Apache 
\\ service description information, can ignore 
                                                        ............ // ignore part 
[root @ localhost httpd-2.4.25] # chkconfig --add httpd 
\\ add httpd as a system service 
[root @ localhost httpd-2.4.25] # systemctl Start httpd 
// service httpd start 
[root @ localhost httpd-2.4.25] # chkconfig httpd ON --level 35 
// httpd service is set to boot from the start 
[root @ localhost httpd-2.4.25] # systemctl is-enabled httpd
IS A Native Service not httpd.service, Redirecting to / sbin / chkconfig. 
Executing / sbin / chkconfig httpd --level 5 = 
Enabled 
// httpd service to see whether the boot from the start, enabled to boot from the start

2) to establish [-Service] .service profile
in / lib / systemd / system / directory, to establish a .service end unit (unit) profile for controlling the httpd service. Configuration is divided into three parts:
① control unit (unit) is defined;
② service [-Service] defined;
③ mounting portion [install].
Details as follows:

[root@localhost ~]# vim /lib/systemd/system/httpd.service
//填写如下内容
[Unit]
Description=The Apache HTTP Server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart=/usr/local/httpd/bin/apachectl $OPTIONS
ExecReload=/bin/kill  -HUP  $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=graphical.target

A profile of configuration items, as shown:
CentOS 7 build Web sites using the Apache Service
After httpd.service file completion, it may be provided from the start of the boot

[root @ localhost ~] # systemctl enable httpd // set the boot from the start 
the Created symlink from /etc/systemd/system/graphical.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. 
[ root @ localhost ~] # systemctl is -enabled httpd // view since the launch of the service status 
enabled

Built by Apache httpd service has been completed! The client would be able to visit, but we have to understand some basic configuration:

First, configure and start the service

1. Configure httpd service
to edit the service httpd main configuration file httpd.conf, find the configuration items "ServerName" remove before "#" sign can be! (If there is a legitimate domain name, proposed changes to the legal domain names, domain names in the future)

[the root @ localhost ~] # Vim /usr/local/httpd/conf/httpd.conf 
                       ............ // Part omitted 
ServerName www.example.com:80 
                       ............ // omitted part

After editing, we recommend using the apachectl command with "-t" option of the configuration file syntax checking!

[root@localhost ~]# apachectl -t
Syntax OK

2. Start httpd service

[root @ localhost ~] # systemctl start httpd // start httpd service 
[root @ localhost ~] # netstat -anpt | grep httpd // view the listening port 
tcp6 0 0 ::: 80 ::: * LISTEN         59183 / httpd

Second, the deployment of web documents

httpd server has provided a test page called index.html, visit the Web site as the default home page (you can edit yourself)!

[root@localhost ~]# cat /usr/local/httpd/htdocs/index.html 
<html><body><h1>It works!</h1></body></html>

Third, the client accesses
client access via IP
CentOS 7 build Web sites using the Apache Service

If you need to access using https , you need:

[yum.repos.d the root @ localhost] # yum the install mod_ssl -Y 
\\ install module selected ssl 
[yum.repos.d the root @ localhost] # systemctl the restart httpd 
\\ restart the httpd server

Client access test:
CentOS 7 build Web sites using the Apache Service
use https access, the default home page will display APache official website home!

If desired domain names, you need to set up DNS service, set up the DNS service can refer Bowen CentOS 7 set up DNS service

Fourth, view the Web site visits
httpd server uses two types of logs: access and error logs.
By viewing access log file access_log, you can keep abreast of visits Web site.
The access log record to access each row corresponds to a record of the IP address of the client, server, access date and time, and other information requested web object.

[root@localhost ~]# tail /usr/local/httpd/logs/access_log 
192.168.1.100 - - [13/Jul/2019:20:29:40 +0800] "GET / HTTP/1.1" 200 45

By looking at the error log err_log, can provide a reference for the investigation of the failed server is running.
Error log file each line corresponds to an error log, recording the date and time the error occurred, the error event type, description etc. wrong time.

The above process is the basic steps used to build Apache Web server, which relates to the amount of change is very small httpd.conf configuration file to build a simple Web server is very simple.

We come to know the main configuration file httpd services and various meanings:

[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
ServerRoot "/usr/local/httpd"
Listen 80
User daemon
Group daemon
ServerAdmin [email protected]
ServerName www.example.com:80
DocumentRoot "/usr/local/httpd/htdocs"
DirectoryIndex index.html
ErrorLog "logs/error_log"
LogLevel warn
CustomLog "logs/access_log" common
PidFile "logs/httpd.pid"
AddDefaultCharset UTF-8
Include conf/extra/httpd-default.conf

Set above the line, the global meaning of each configuration item, as shown:
CentOS 7 build Web sites using the Apache Service
The above configuration item is Httpd.conf major global configuration file entries, there are many other configuration items, this is not introduced one by one, if necessary you can view the Apache server-related help manuals.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160128.htm