目录
2、httpd.conf引用httpd-vhosts.conf
一、构建Linux下Apache主流WEB服务器
Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。官网:http://httpd.apache.org/download.cgi
Apache三种工作模式
- prefork模式(默认模式)是很古老但是非常稳定的模式。使用的是多个子进程,Apache在启动之初,控制进程会建立若干个子进程,然后等待请求进来,并且总是视图保持一些备用的子进程。为了不在请求到来时再生成子进程,所以要根据需求不断的创建新的子进程,最大可以达到每秒32个直到满足需求为止。之所以这样做,是为了减少频繁创建和销毁进程的开销。每个子进程中只有一个线程,在一个时间点内,只能处理一个请求。在Unix系统中,父进程通常以root身份运行以便邦定80端口,而Apache产生的子进程通常以一个低特权的用户运行。User和Group指令用于配置子进程的低特权用户。运行子进程的用户必须要对他所服务的内容有读取的权限,但是对服务内容之外的其他资源必须拥有尽可能少的权限。
- worker模式使用了多进程和多线程的混合模式,worker模式也同样会先预派生一些子进程,然后每个子进程创建一些线程,同时包括一个监听线程,每个请求过来会被分配到一个线程来服务。线程比起进程会更轻量,因为线程是通过共享父进程的内存空间,因此,内存的占用会减少一些,在高并发的场景下会比prefork有更多可用的线程,表现会更优秀一些。另外,如果一个线程出现了问题也会导致同一进程下的线程出现问题,如果是多个线程出现问题,也只是影响Apache的一部分,而不是全部。由于用到多进程多线程,需要考虑到线程的安全了,在使用keep-alive长连接的时候,某个线程会一直被占用,即使中间没有请求,需要等待到超时才会被释放(该问题在prefork模式下也存在)。Apache总是试图维持一个备用(spare)或是空闲的服务线程池。这样,客户端无须等待新线程或新进程的建立即可得到处理。在Unix中,为了能够绑定80端口,父进程一般都是以root身份启动,随后,Apache以较低权限的用户建立子进程和线程。User和Group指令用于配置Apache子进程的权限。虽然子进程必须对其提供的内容拥有读权限,但应该尽可能给予他较少的特权。另外,除非使用了suexec ,否则,这些指令配置的权限将被CGI脚本所继承。内存的占用会减少一些,在高并发的场景下,表现得比 prefork模式好。
- event是Apache最新的工作模式,它和worker模式很像,不同的是在于它解决了keep-alive长连接的时候占用线程资源被浪费的问题(某些线程因为被keep-alive,挂在那里等待,中间几乎没有请求过来,一直等到超时)。在event工作模式中,会有一些专门的线程用来管理这些keep-alive类型的线程,当有真实请求过来的时候,将请求传递给服务器的线程,执行完毕后,又允许它释放。这样,一个线程就能处理几个请求了,实现了异步非阻塞。这增强了在高并发场景下的请求处理。event工作模式在遇到某些不兼容的模块时,会失效,将会回退到worker模式,一个工作线程处理一个请求。官方自带的模块,全部是支持event工作模式的。event工作模式需要Linux系统(Linux2.6+)对epoll的支持,才能启用。需要补充的是HTTPS的连接(SSL),它的运行模式仍然是类似worker的方式,线程会被一直占用,知道连接关闭。部分比较老的资料里,说event MPM不支持SSL,那个说法是几年前的说法,现在已经支持了。
安装步骤:
1、下载并解压tar包
官网:http://httpd.apache.org/download.cgi
rpm -qa httpd //检查是否安装有httpd,可以使用yum install httpd安装(默认路径/etc/httpd)或者如下方式
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、安装apr、apr-util
yum install apr apr-util apr-devel apr-util-devel
3、编译并安装
1)./configure --prefix=/usr/local/apache2 [--enable-rewrite --enable-so] //可选参数
2)make
3)make install
注:错误解决
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、安装完成及查看端口
默认路径:/usr/local/apache2 (yum install httpd安装默认路径/etc/httpd)
配置文件:/usr/local/apache2/conf/httpd.conf (yum install httpd默认配置文件路径/etc/httpd/conf/httpd.conf)
默认发布目录:/usr/local/apache2/htdocs (yum install httpd默认发布路径/var/www/html)
启动脚本:/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>
访问(注意关闭防火墙)
二、Apache构建基于域名的虚拟主机
1、配置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>
示例配置参考:
<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,创建index.html
2、httpd.conf引用httpd-vhosts.conf
vi /usr/local/apache2/conf/httpd.conf,取消注释
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
3、重启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、访问域名
本地主机绑定hosts,路径:C:\Windows\System32\drivers\etc\hosts
添加:
192.168.56.102 www.test1.com
192.168.56.102 www.test2.com
如果访问返回Forbidden You don't have permission to access this resource.
修改httpd.conf中
<Directory /> AllowOverride none Require all denied </Directory>
修改为<Directory /> AllowOverride none Require all granted </Directory>
访问成功后:
Apache基于IP虚拟主机同样跟域名一样,在服务器配置多个IP,然后把域名改成IP即可。
补充内容:
tar压缩解压常见参数
-c:建立一个压缩文件的参数指令(creat)
-x:解开一个压缩文件的参数指令
-t:查看tar file里面文件
-z:是否同时具有gzip属性,亦即是否需要用gzip压缩
-j:是否同时具有bzip2的属性,亦即是否需要用bzip2压缩
-v:压缩的过程中显示文件
-f:使用档名,注意,在f之后要立即接档名,不要再加参数
--exclude FIFE:压缩过程中,不要将FILE打包