没有修改配置文件的情况下用start启动httpd程序,可能返回错误提示:
[iyunv@localhost ~]# /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地址:]端口[协议],其中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定义。语法格式为:指令
开启:
[iyunv@localhost ~]# mkdir -p /usr/local/apache2/htdocs/{example,test}
[iyunv@localhost ~]# echo "example.com">/usr/local/apache2/htdocs/example/index.html
[iyunv@localhost ~]# echo "test.com">/usr/local/apache2/htdocs/test/index.html[iyunv@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[iyunv@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[iyunv@localhost ~]# /usr/local/apache2/bin/apachectl restart
网站安全应用案例:
TLS(Transport Layer Security)是对SSL的扩展和优化,它可以提供数据安全,同时确保数据的完整性。Apache HTTP Server通过mod_ssl模块来实现对SSL/TLS的支持。
部署证书
[iyunv@localhost ~]# openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...................................+++
...+++
e is 65537 (0x10001)
[iyunv@localhost ~]# 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) [XX]:CN
State or Province Name (full name) []:Guangzhou
Locality Name (eg, city) [Default City]:Guangzhou
Organization Name (eg, company) [Default Company Ltd]:ABC
Organizational Unit Name (eg, section) []:tech
Common Name (eg, your name or your server's hostname) []:web1
Email Address []:abc@gmail.com
[iyunv@localhost ~]# cp {server.key,server.crt} /usr/local/apache2/conf/
修改Apache配置文件,主配置文件中需要加载mod_ssl以及mod_socache_shmcb两个模块,同时使用Include指令读取conf/extra目录下的http-ssl配置文件。
[iyunv@localhost ~]# gedit /usr/local/apache2/conf/httpd.conf
去掉注释
LoadModule ssl_module modules/mod_ssl.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
[iyunv@localhost ~]# 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