安装apache软件包:
# yum install -y httpd httpd-manual 启动apache服务:
# systemctl start httpd ; systemctl enable httpd 查看监听端口:
# ss -antlp |grep httpd
LISTEN 0 128 :::80 :::*
users:(("httpd",4347,4),("httpd",4346,4),("httpd",4345,4),("httpd",4344,4),("httpd"
,4343,4),("httpd",4342,4))
Apache主配置文件: /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd" 用于指定Apache的运行目录
Listen 80 监听端口
User apache 运行apache程序的用户和组
Group apache
ServerAdmin root@localhost 管理员邮箱
DocumentRoot "/var/www/html" 网页文件的存放目录
<Directory "/var/www/html"> <Directory> 语句块自定义目录权限
Require all granted
</Directory>
ErrorLog "logs/error_log" 错误日志存放位置
AddDefaultCharset UTF-8 默认支持的语言
IncludeOptional conf.d/*.conf 加载其它配置文件
DirectoryIndex index.html 默认主页名称
############虚拟主机################
虚拟主机允许您从一个httpd服务器同时为多个网站提供服务。在本节中,我们将了解基于名称的虚
拟主机其中多个主机名都指向同一个IP地址,但是Web服务器根据用于到达站点的主机名提供具有不
同内容的不同网站。
Example:
<virtualhost *:80>
servername wwwX.example.com
serveralias wwwX
documentroot /srv/wwwX.example.com/www
customlog "logs/wwwX.example.com.log" combined
</virtualhost>
<directory /srv/wwwX.example.com/www>
require all granted
</directory>
1. <VirtualHost *:80>
...
</VirtualHost>
这是定义虚拟主机的块
2. ServerName wwwX.example.com
指定服务器名称。在使用基于名称的虚拟主机的情况下,此处的名称必须与客户端请求完全的匹配。
3. ServerAlias serverX wwwX wwwX.example.com
用于匹配的空格分隔的名称列表,如上面的ServerName
4. DocumentRoot /var/www/html
在<VirtualHost>块内部,指定从中提供内容的目录。
5. selinux标签
semanage fcontext -l
semanage fcontext -a -t httpd_sys_content_t “/directory(/.*)?”
restorecon -vvFR /directory
Demo:
建立网页发布目录,并设置selinux标签:
# mkdir -p /srv/{default,www0.example.com}/www
# echo "coming soon" > /srv/default/www/index.html
# echo "www0" > /srv/www0.example.com/www/index.html
# restorecon -Rv /srv/
创建虚拟主机配置文件:
# cat /etc/httpd/conf.d/00-default-vhost.conf
<virtualhost _default_:80>
documentroot /srv/default/www
customlog "logs/default-vhost.log" combined
</virtualhost>
<directory /srv/default/www>
require all granted
</directory>
# cat 01-www0.example.com-vhost.conf
<virtualhost *:80>
servername www0.example.com
serveralias www0
documentroot /srv/www0.example.com/www
customlog "logs/www0.example.com.log" combined
</virtualhost>
<directory /srv/www0.example.com/www>
require all granted
</directory>
启动apache服务
# systemctl start httpd ; systemctl enable httpd
############配置基于用户的身份验证############
Apache无格式文件用户身份验证
在此配置中,用户账户和密码存储在本地.htpasswd文件中。处于安全原因,该文件不能
保存在网站的DocumentRoot中,而应保存在Web服务器不提供服务的一些目录中。特殊
的htpasswd命令用于在.htpasswd文件中管理用户。
配置程序示例:
用两个账户创建Apache密码文件:
[root@serverX ~]# htpasswd -cm /etc/httpd/.htpasswd bob
[root@serverX ~]# htpasswd -m /etc/httpd/.htpasswd alice
假设之前定义VirtualHost块,请将诸如以下内容添加至VirtualHost块:
<Directory /var/www/html>
AuthName “Secret Stuff”
AuthType basic
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
</Directory>
重启apache服务,并使用Web浏览器测试访问,在弹出的对话框中输入上述用户名
和密码。
自定义自签名证书
如果加密的通信非常重要,而经过验证的身份不重要,管理员可以通过生成self-
signed certificate来避免与认证机构进行交互所带来的复杂性。
使用genkey实用程序(通过crypto-utils软件包分发),生成自签名证书及其关联的
私钥。为了简化起见,genkey将在“正确”的位置(/etc/pki/tls目录)创建证书及其
关联的密钥。相应地,必须以授权用户(root)身份运行该实用程序。
生成自签名证书
1. 确保已安装crypto-utils软件包。
[root@server0 ~]# yum install crypto-utils mod_ssl
2. 调用genkey,同时为生成的文件指定唯一名称(例如,服务器的主机全名)。
--days可以指定证书有效期
[root@server0 ~]# genkey server0.example.com
记录生成的证书(server0.example.com.crt)和关联的私钥(
server0.example.com.key)的位置
###########安装证书及其私钥############
1. 确定已安装mod_ssl软件包。
[root@server0 ~]# yum install mod_ssl
2. 由于私钥是敏感信息,请确保其只被root用户读取。
[root@server0 ~]# ls -l /etc/pki/tls/private/server0.example.com.key
-r--------. 1 root root 1737 Dec 22 15:06 /etc/pki/tls/private/server0.example.com.key
3. 编辑/etc/httpd/conf.d/ssl.conf, 将SSLCertificateFile和SSLCertificateKeyFile指令设置为分别指
向X.509证书和密钥文件。
SSLCertificateFile /etc/pki/tls/certs/server0.example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/server0.example.com.key
4. 重启Web服务器。
[root@server0 ~]# systemctl restart httpd
5. 如要进行确认,请使用https协议(https://serverX.example.com)通过Web客户端(如Firefox
)访问Web服务器。
Web客户端可能会发出它不认可证书发行者的警告。这种情况适用自签名证书。要求Web客户端
绕过证书认证。(对于Firefox,请选择“I Understand the Risks” [我了解风险]、“Add Exception” [
添加例外]和“Confirm Security Exception”[确认安全例外]。)
网页重写
把所有80端口的请求全部重定向由https来处理
<Virtualhost *:80>
ServerName www0.example.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>