Linux operation and maintenance notes (3)

 

table of Contents

1. Build Apache mainstream WEB server under Linux

Three working modes of Apache

1. Download and unzip the tar package

2. Install apr, apr-util

3. Compile and install

4. Installation is complete and view port

Second, Apache builds a domain-based virtual host

1. Configure httpd-vhosts.conf

2、httpd.conf引用httpd-vhosts.conf

 3. Restart httpd

4. Access the domain name

 


1. Build Apache mainstream WEB server under Linux

Apache HTTP Server (Apache for short) is an open source web server of the Apache Software Foundation. It can run on most computer operating systems. It is widely used due to its multi-platform and security. It is one of the most popular web server-side software. One. It is fast, reliable and can be extended through a simple API to compile Perl/Python and other interpreters into the server. Official website: http://httpd.apache.org/download.cgi

Three working modes of Apache

  • The prefork mode (default mode) is an old but very stable mode. Multiple child processes are used. At the beginning of Apache startup, the control process will create several child processes, then wait for requests to come in, and always view to keep some spare child processes. In order not to generate sub-processes when the request arrives, new sub-processes must be created continuously according to the demand, and the maximum can reach 32 per second until the demand is met. The reason for this is to reduce the overhead of frequently creating and destroying processes. There is only one thread in each child process, and only one request can be processed at a time. In Unix systems, the parent process usually runs as root to bind port 80, while the child process spawned by Apache usually runs as a low-privileged user. The User and Group instructions are used to configure low-privileged users of the child process. The user who runs the child process must have read access to the content he serves, but must have as few permissions as possible for resources other than the service content.
  • The worker mode uses a mixed mode of multi-process and multi-threading. The worker mode also pre-spawns some sub-processes, and then each sub-process creates some threads, and includes a listener thread. Each request will be assigned to a thread. service. The thread is lighter than the process, because the thread shares the memory space of the parent process. Therefore, the memory usage will be reduced. In high concurrency scenarios, there will be more threads available than prefork, and the performance will be better. . In addition, if there is a problem with one thread, it will also cause problems with threads in the same process. If there are problems with multiple threads, it will only affect a part of Apache, but not all. Due to the use of multi-process and multi-threading, thread safety needs to be considered. When using a keep-alive long connection, a thread will always be occupied. Even if there is no request in the middle, it will not be released until the timeout is reached. It also exists in prefork mode). Apache always tries to maintain a spare or idle service thread pool. In this way, the client does not need to wait for the establishment of a new thread or process to be processed. In Unix, in order to be able to bind to port 80, the parent process is generally started as root. Subsequently, Apache creates child processes and threads as users with lower privileges. The User and Group directives are used to configure the permissions of Apache child processes. Although the child process must have read access to the content it provides, it should be given as few privileges as possible. In addition, unless suexec is used, the permissions configured by these instructions will be inherited by the CGI script. The memory usage will be reduced a bit, and it performs better than the prefork mode in high-concurrency scenarios.
  • Event is the latest working mode of Apache. It is very similar to the worker mode. The difference is that it solves the problem of thread resources being wasted when the keep-alive connection occurs (some threads hang there and wait because they are kept-alive). , There are almost no requests in the middle, and wait until the timeout). In the event work mode, there will be some special threads used to manage these keep-alive threads. When a real request comes, the request is passed to the server thread, and after the execution is completed, it is allowed to be released. In this way, one thread can handle several requests, achieving asynchronous non-blocking. This enhances request processing in high concurrency scenarios. The event working mode will fail when it encounters some incompatible modules and will fall back to worker mode, where one worker thread processes one request. The official modules all support event mode. The event working mode requires Linux system (Linux2.6+) support for epoll before it can be enabled. What needs to be added is the HTTPS connection (SSL). Its operating mode is still similar to a worker. The thread will be occupied until the connection is closed. Some older documents say that event MPM does not support SSL. That statement is a few years ago, and it is now supported.

installation steps:

1. Download and unzip the tar package

Official website: http://httpd.apache.org/download.cgi

rpm -qa httpd //Check if httpd is installed, you can use yum install httpd to install (default path /etc/httpd) or the following way

wget https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.41.tar.gz

tar xzf httpd-2.4.41.tar.gz

[root@10 opt]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4                                                                .41.tar.gz
--2020-03-22 13:16:30--  https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/http                                                                d-2.4.41.tar.gz
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8                                                                .193, 2402:f000:1:408:8100::1
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.                                                                8.193|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9267917 (8.8M) [application/octet-stream]
Saving to: ‘httpd-2.4.41.tar.gz’

100%[======================================>] 9,267,917   2.31MB/s   in 3.8s

2020-03-22 13:16:35 (2.31 MB/s) - ‘httpd-2.4.41.tar.gz’ saved [9267917/9267917]
[root@10 opt]# tar xzf httpd-2.4.41.tar.gz
[root@10 opt]# ls
httpd-2.4.41  httpd-2.4.41.tar.gz

2. Install apr, apr-util

 yum install apr apr-util apr-devel apr-util-devel

3. Compile and install

1)./configure --prefix=/usr/local/apache2 [--enable-rewrite --enable-so] //可选参数

2)make

3)make install

Note: Error resolution

1)configure: error: C compiler cannot create executables    See `config.log' for more details

config.log:./configure: line 5327: gcc: command not found
yum install gcc / yum install glibc-headers / yum install gcc-c++

2)configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

yum -y install pcre-devel

4. Installation is complete and view port

Default path: /usr/local/apache2 (yum install httpd installation default path /etc/httpd)

Configuration file: /usr/local/apache2/conf/httpd.conf (yum install httpd default configuration file path /etc/httpd/conf/httpd.conf)

Default publishing directory: /usr/local/apache2/htdocs (yum install httpd default publishing path /var/www/html)

Start script: /usr/local/apache2/bin/apachectl start

[root@10 httpd-2.4.41]# cd /usr/local/apache2/
[root@10 apache2]# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules
[root@10 apache2]# cd conf/
[root@10 conf]# ls
extra  httpd.conf  magic  mime.types  original
[root@10 conf]# cd ../htdocs/
[root@10 htdocs]# ls
index.html
[root@10 htdocs]# cd ../bin/
[root@10 bin]# ls
ab         apxs      dbmmanage  envvars-std  htcacheclean  htdigest  httpd      logresolve
apachectl  checkgid  envvars    fcgistarter  htdbm         htpasswd  httxt2dbm  rotatelogs
[root@10 bin]# /usr/local/apache2/bin/apachectl start
[root@10 bin]# ps -ef | grep http
root     20238     1  0 14:00 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
daemon   20239 20238  0 14:00 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
daemon   20240 20238  0 14:00 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
daemon   20241 20238  0 14:00 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
root     20324  1288  0 14:00 pts/0    00:00:00 grep --color=auto http
[root@10 bin]# netstat -an |grep 80
tcp6       0      0 :::80                   :::*                    LISTEN
udp6       0      0 fe80::f582:e24c:6a1:123 :::*
udp6       0      0 fe80::4f14:193b:a52:123 :::*
[root@10 bin]# netstat -ntl |grep 80
tcp6       0      0 :::80                   :::*                    LISTEN
[root@10 bin]# lsof -i :80   // yum install lsof
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd   20238   root    4u  IPv6  29064      0t0  TCP *:http (LISTEN)
httpd   21147 daemon    4u  IPv6  29064      0t0  TCP *:http (LISTEN)
httpd   21148 daemon    4u  IPv6  29064      0t0  TCP *:http (LISTEN)
httpd   21149 daemon    4u  IPv6  29064      0t0  TCP *:http (LISTEN)
httpd   21372 daemon    4u  IPv6  29064      0t0  TCP *:http (LISTEN)
[root@10 bin]# curl http://192.168.56.102
<html><body><h1>It works!</h1></body></html>

Access (pay attention to turn off the firewall)

Second, Apache builds a domain-based virtual host

1. Configure httpd-vhosts.conf

[root@10 extra]# pwd
/usr/local/apache2/conf/extra
[root@10 extra]# ls
httpd-autoindex.conf  httpd-languages.conf           httpd-ssl.conf
httpd-dav.conf        httpd-manual.conf              httpd-userdir.conf
httpd-default.conf    httpd-mpm.conf                 httpd-vhosts.conf
httpd-info.conf       httpd-multilang-errordoc.conf  proxy-html.conf
[root@10 extra]# cat httpd-vhosts.conf
# Virtual Hosts
#
# Required modules: mod_log_config

# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/usr/local/apache2/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/usr/local/apache2/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "logs/dummy-host2.example.com-error_log"
    CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

Sample configuration reference:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/data/webapps/test1"
    ServerName www.test1.com
    <Directory "/data/webapps/test1">
        AllowOverride All
        Options +FollowSymlinks
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog "logs/test1-error_log"
    CustomLog "logs/test1-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/data/webapps/test2"
    ServerName www.test2.com
    <Directory "/data/webapps/test2">
        AllowOverride All
        Options +FollowSymlinks
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog "logs/test2-error_log"
    CustomLog "logs/test2-access_log" common
</VirtualHost>

 mkdir /data/webapps/{test1,test2} -p, create index.html

2、httpd.conf引用httpd-vhosts.conf

 vi /usr/local/apache2/conf/httpd.conf, uncomment

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

 3. Restart httpd

[root@10 bin]# ./apachectl -t
Syntax OK
[root@10 bin]# ./apachectl restart
httpd not running, trying to start

[root@10 bin]# ./apachectl graceful   //平滑重启,不会杀进程
[root@10 bin]#

4. Access the domain name

Local host is bound to hosts, path: C:\Windows\System32\drivers\etc\hosts

Add to:

192.168.56.102 www.test1.com
192.168.56.102 www.test2.com

If access returns Forbidden You don't have permission to access this resource.

Modify httpd.conf

<Directory /> AllowOverride none Require all denied </Directory>

修改为<Directory /> AllowOverride none Require all granted </Directory>

After a successful visit:

 

Apache's IP-based virtual host is the same as the domain name. Configure multiple IPs on the server, and then change the domain name to IP.

 

 

 

to add on:

Common parameters of tar compression and decompression

-c: create a compressed file parameter command (creat)

-x: Unzip a compressed file parameter command

-t: View files in tar file

-z: Whether it has the gzip attribute at the same time, that is, whether it needs to be compressed with gzip

-j: Whether it has the attributes of bzip2 at the same time, that is, whether it needs to be compressed with bzip2

-v: display files during compression

-f: Use the file name, pay attention to the file name immediately after f, no more parameters

--exclude FIFE: Do not pack FILE during compression

 

Guess you like

Origin blog.csdn.net/xlyrh/article/details/105039991