Apache网站服务器
源自《Linux 运维之道》丁一明编著 一书的总结Apache HTTP Server项目是在Windows和UNIX等平台上可以运行的跨平台开源HTTP服务器的软件,该项目的目标是提供安全、高效、可扩展的HTTP服务。Apache HTTP Server非常重要的是它采用了模块化设计模型,Apache模块分为静态模块和动态模块,静态模块是Apache是最基本的模块,是无法随时添加和卸载的模块,静态模块在编译软件时设定。动态模块是可以随时添加和删除的模块,这样的设计师的企业部署Apache时可以获得最大的灵活性。Apache模块将被编译为动态共享对象(DSO),这些动态共享对象独立于httpd程序,DSO模块可以在编译Apache添加的,也可以在后期随时通过Apache Extension Tool(apxs)工具编译添加模块。可以使用httpd –M命令查看模块加载清单。
下载并安装
# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.9.tar.gz
# wget http://mirror.bjtu.edu.cn/apache/apr/apr-util-1.5.3.tar.gz
# wget http://mirror.bjtu.edu.cn/apache/apr/apr-1.5.1.tar.gz
安装依赖的软件:
# yum -y install gcc autoconf automake make pcre pcre-devel openssl openssl-devel
# tar -xzf httpd-2.4.9.tar.gz -C /usr/src/
# tar -xzf apr-1.5.1.tar.gz -C /usr/src/
# tar -xzf apr-util-1.5.3.tar.gz -C /usr/src/
# cd /usr/src/apr-1.5.1/
# ./configure
# make && make install
# cd /usr/src/apr-util-1.5.3/
# ./configure --with-apr=/usr/local/apr/
# make && make install
# ./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl --enable-rewrite --with-mpm=worker --with-suexec-bin --with-apr=/usr/local/apr/
# make && make install
可以通过./configure –help查看脚本支持的
选项
描述
--prefix
指定Apache httpd程序的主安装目录
--enable-so
开启模块化功能,支持DSO(动态共享对象)
--enable-ssl
支持SSL加密
--enable-rewrite
支持地址重写
--with-mpm
设置Apache httpd工作模式
--with-suexec-bin
支持SUID、SGID
--withapr
支持apr程序绝对路径
启动服务:
# /usr/local/apache2/bin/apachectl start
# netstat -tulnp | grep http
# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
# service iptables save
安装后,Apache会提供名为apachectl启动脚本。脚本在/usr/local/apache2/bin目录下,该脚本用来进行Apache httpd的启动、关闭以及测试功能,具体的参数见下表。
参数
描述
Start
启动httpd程序
Stop
关闭httpd程序
Restart
重启httpd程序
Graceful
启动httpd,不中断现有的http连接请求
Graceful-stop
关闭httpd,不中断现有的http连接
Status
查看httpd程序当前状态
Configtest
检测httpd主配置语法
没有修改配置文件的情况下用start启动httpd程序,可能返回错误提示:
# /usr/local/apache2/bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
(98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
AH00015: Unable to open logs
但是确实服务已经启动了。
配置文件解析:
配置文件默认在/usr/local/apache2/conf目录下,该目录的主要配置文件是httpd.conf文件。还有一些位于extra目录下的附加配置文件。主配置文件以容器组成,容器使用开始,以结尾,容器的指令一般仅在容器内有效。
² SeverRoot指令:设置Apache软件的安装主目录,如果采用源码安装,这默认路径是/usr/local/apache2
² Listen指令:设置服务器监听IP和端口号。语法格式为:Listen端口[协议],其中ip地址与协议可为可选项。可以多吃使用listen指令来开启多个端口。
² LoadModule指令:在编译时加上—enable-so将mod_so以静态方式编译到Apache核心模块中,LoadModule指令的作用就是加载模块。
² LoadFile指令:通过绝对路径加载modules目录下的模块文件。
² serverAdmin指令:提供一个解决问题的邮件地址。
² ServeName指令:设置服务器本机的主机名称和端口,对URL地址的重定向很重要。
² DocumentRoot指令:设置Web服务对客户端开放可见的文档根目录,也就是客户端访问网站的根路径,默认为/usr/local/apache2/htdocs。
² ErrorLog指令:定位服务器错误日志的位置,默认使用相对路径,为ServerRoot目录下的“logs/error_log”文件。
² ErrorLogFormat指令:设置错误日志的格式,Apache HTTP Server预先定义了很多格式字串可以直接引用。
² CustomLog指令:该指令设置客户端的访问日志文件以及日志格式,格式为“logs/access_log”,语法格式为CustomLog 文件名 格式
² LogFormat指令:该指令描述用户日志文件格式,可以直接使用Apache预先设置的格式字串,一般我们会为LogFormat指令设置的日子格式格式创建别名。
² Include指令:语法格式为:Options[+|-]选项[[+|0-]选项]。选项可以设置为None,代表不启动任何额外的功能。也可以使用如下常用选项:All,开启除MultiViews之外的所有选项;ExecCGI:允许执行Options指定目录下的所有CGI脚本;FollowSymlinks:允许Options指定目录下的文件链接到目录外的文件或目录。Indexes:如果与URL对应的Options目录下找不到DirectoryIndex指定的首页文档,则Apache将会把当前目录的所有文件索引出来。
² Order指令:控制默认访问状态以及Allow和Deny的次序,如果使用Order deny,allow,则先检查拒绝,然后在允许。如果有冲突,允许优先,默认规则为允许。如果使用Order allow,deny,则先检查允许,然后在拒绝。如果有冲突,拒绝优先,默认规则为拒绝。例如:
案例一,所有都拒绝:
Order deny,allow
Deny from all
案例二,除192.168.118.254以外都拒绝:
Order allow,deny
Allow from 192.168.118.254
案例三,拒绝所有:
Order allow,deny
Allow from 192.168.118.254
Deny from All
案例四:除192.168.118.254以外都拒绝:
Order deny,allow
Deny from all
Allow from 192.168.118.254
² ifDefine容器:仅在启动Apache时测试条件为真才会被处理,测试条件需要在启动Apache时通过httpd –D定义。语法格式为:指令
LoadModule mem_cache_module modules/mod_mem_cache.so
说明只有在启动Apache时,如果使用了httpd –D Memcache,这Apache将加载mod_mem_cache模块,如果没有则Apache将不加载这些模块。
² ifModule容器
使用IfModule容器可以封装仅在条件满足时才会处理的指令。语法格式:指令
例子:
User daemon
Group daemaon
仅在加载了unixd_module模块后,User daemon和Group daemon才会被Apache处理。
² Directory容器:仅应用特定的文件系统目录、子目录以及目录下的内容,语法格式:指令。路径可以使用~匹配正则表达式。
例子:
Options Indexs FollowSymLinks
Options Indexs FollowSymLinks仅对/usr/local/apache2/htdocs目录有效。
² :类似于Directory,但可以直接使用正则表达式匹配。无需要使用~符号才可以匹配。
² File容器:类似于Directory,但Files容器内的指令应用于特定的文件,语法格式为:指令
² FilesMatch容器:类似于File,但可以直接使用正则表达式匹配。无需要使用~符号才可以匹配。
² Location容器:仅对特定的URL有效,语法格式:指令
² LocationMathch容器和VirtualHost容器
虚拟主机应用案例:
将主配置文件的语句开启:
# gedit /usr/local/apache2/conf/httpd.conf
去掉注释
Include conf/extra/httpd-vhosts.conf
更改httpd-vhosts.conf文件
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/apache2/htdocs/example"
ServerName www.example.com
ServerAlias web.example.com
ErrorLog "logs/example.com-error_log"
CustomLog "logs/example.com-access_log" common
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/apache2/htdocs/test"
ServerName www.test.com
ServerAlias web.test.com
ErrorLog "logs/test.com-error_log"
CustomLog "logs/test.com-access_log" common
开启:
# mkdir -p /usr/local/apache2/htdocs/{example,test}
# echo "example.com">/usr/local/apache2/htdocs/example/index.html
# echo "test.com">/usr/local/apache2/htdocs/test/index.html# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
# /usr/local/apache2/bin/apachectl restart
网站安全应用案例:
TLS(Transport Layer Security)是对SSL的扩展和优化,它可以提供数据安全,同时确保数据的完整性。Apache HTTP Server通过mod_ssl模块来实现对SSL/TLS的支持。
部署证书
# openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...................................+++
...+++
e is 65537 (0x10001)
# openssl req -new -x509 -key server.key -out server.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) :CN
State or Province Name (full name) []:Guangzhou
Locality Name (eg, city) :Guangzhou
Organization Name (eg, company) :ABC
Organizational Unit Name (eg, section) []:tech
Common Name (eg, your name or your server's hostname) []:web1
Email Address []:abc@gmail.com
# cp {server.key,server.crt} /usr/local/apache2/conf/
修改Apache配置文件,主配置文件中需要加载mod_ssl以及mod_socache_shmcb两个模块,同时使用Include指令读取conf/extra目录下的http-ssl配置文件。
# gedit /usr/local/apache2/conf/httpd.conf
去掉注释
LoadModule ssl_module modules/mod_ssl.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
# gedit /usr/local/apache2/conf/extra/httpd-ssl.conf
修改内容:
Listen 443
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLPassPhraseDialog builtin
SSLSessionCache "shmcb:/usr/local/apache2/logs/ssl_scache(512000)"
SSLSessionCacheTimeout 300
DocumentRoot "/usr/local/apache2/htdocs/secret"
ServerName www.abc.com:443
ServerAdmin you@example.com
ErrorLog "/usr/local/apache2/logs/error_log"
TransferLog "/usr/local/apache2/logs/access_log"
SSLEngine on
SSLCertificateFile "/usr/local/apache2/conf/server.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"
SSLOptions +StdEnvVars
SSLOptions +StdEnvVars
BrowserMatch "MSIE " \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
CustomLog "/usr/local/apache2/logs/ssl_request_log" \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
# mkdir -p /usr/local/apache2/htdocs/secret
# echo "secret">/usr/local/apache2/htdocs/secret/index.html
# /usr/local/apache2/bin/apachectl restart
# iptables -I INPUT -p tcp --dport 443 -j ACCEPT
# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
# gedit /etc/hosts
页:
[1]