linux基础3.9apache

来源:互联网 发布:淘宝店铺新手装修步骤 编辑:程序博客网 时间:2024/05/22 08:08

1.安装httpd服务

yum install httpd -y

yum install httpd-manual -y

systemctl start httpd 

systemctl enable httpd

ss -antlp |grep httpd    ##查看监听端口

 

2.配置apache

/etc/httpd/conf/httpd.conf   ##apache主配置文件

ServerRoot "/etc/httpd"     ##用于指定Apache的运行目录

Listen 80                    ##监听端口

User apache                  ##运行apache程序的用户和组

Group apache

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    ##默认主页名称

 

3.虚拟主机

虚拟主机允许从一个httpd服务器同时为多个网站提供服务。

[root@server4 ~]# cd /etc/httpd/conf.d/ 

[root@server4 conf.d]# vim default.conf

vim:

<Virtualhost _default_:80>  ##虚拟主机的块

   DocumentRoot"/var/www/html"

   Customlog"log/defaule.log" combned

</Virtualhost>

<Directory "/var/www/html"> ##授权

    Require all granted

</Directory>

:wq

 

[root@server4 conf.d]# vim news.conf

vim:

<VirtualHost *:80>  ##虚拟主机的块

   ServerName wwwX.example.com   ##指定服务器名称

   ServerAlias serverX wwwXwwwX.example.com   ##用于匹配的空格分隔的名称列表

   DocumentRoot /var/www/html  ##在<VirtualHost>块内部,指定从中提供内容的目录。

</VirtualHost>

<Directory "/var/www/html"> ##授权

    Require all granted

</Directory>

 

selinux标签

semanage fcontext -l

semanage fcontext -a -t httpd_sys_content_t “/directory(/.*)?”

restorecon -vvFR /directory

 

4.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>

www.westos.org

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>

 

systemctl start httpd

systemctl enable httpd

 

5.配置基于用户的身份验证

示例:

用两个账户创建Apache密码文件:

[root@serverX ~]# htpasswd -cm /etc/httpd/.htpasswd bob

[root@serverX ~]# htpasswd -m /etc/httpd/.htpasswd alice

 

在Virtualhost块中添加以下内容:

<Directory /var/www/html>

     AuthName “Secret Stuff”

     AuthType basic

     AuthUserFile/etc/httpd/.htpasswd

     Require valid-user

</Directory>

systemctl restart httpd

在Web浏览器上测试

 

6.自定义自签名证书

  使用genkey实用程序(通过crypto-utils软件包分发),生成自签名证书及其关联的私钥。为了简化起见,genkey将在“正确”的位置(/etc/pki/tls目录)创建证书及其关联的密钥。

yum install crypto-utils mod_ssl -y ##安装服务

genkey server0.example.com   ##生成证书

在生成随机数时比较慢,可以敲键盘和移动鼠标加快进度。

 

安装证书及其私钥

<1> 确保私钥只被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

<2> 编辑/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

<3> 重启Web服务器。

[root@server0 ~]# systemctl restart httpd

<4> 使用https协议(https://serverX.example.com)通过Web客户端(如Firefox

)访问Web服务器。

Web客户端可能会发出它不认可证书发行者的警告。这种情况适用自签名证书。要求Web客户端绕过证书认证。(对于Firefox,请选择“I Understand the Risks” [我了解风险]、“Add Exception” [添加例外]和“Confirm Security Exception”[确认安全例外]。)

 

7.网页重写

所有80端口的请求全部重定向都由https来处理

<Virtualhost *:80>

    ServerName www0.example.com

    RewriteEngine on

    RewriteRule ^(/.*)$https://%{HTTP_HOST}$1 [redirect=301]

</Virtualhost>

0 0