Apache web服务

Apache web 服务

Apache
基本配置
安装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))
www.westos.org
3Apache
主配置文件: /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
默认主页名称
举例
[root@httpserver ~]# yum install httpd -y
Loaded plugins: langpacks
rhel_dvd                                                | 4.1 kB     00:00    
Package httpd-2.4.6-17.el7.x86_64 already installed and latest version
Nothing to do
[root@httpserver ~]# firewall-cmd --permanent --add-service=http
success
[root@httpserver ~]# firewall-cmd --permanent --add-service=https
success
[root@httpserver ~]# firewall-cmd --reload
success
[root@httpserver ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources:
  services: dhcpv6-client http httpsssh
  ports:
  masquerade: no
  forward-ports:
  icmp-blocks:
[root@httpserver ~]# systemctl restart httpd
[root@httpserver ~]# cd /var/www/html/
[root@httpserver html]# ls
[root@httpserver html]# vim index.html
更改访问目录
[root@httpserver html]# cd
[root@httpserver ~]# mkdir /www/html -p
[root@httpserver ~]# cd /www/html
[root@httpserver html]# ls
[root@httpserver html]# vim test.html
[root@httpserver html]# vim /etc/httpd/conf/httpd.conf
[root@httpserver html]# cat /etc/httpd/conf/httpd.conf
DocumentRoot "/www/html"
<Directory "/www/html/">
 require all granted
</Directory>

<IfModule dir_module>
    DirectoryIndex test.html
</IfModule>
设置权限
<Directory "/www/html/">
 require all granted
</Directory>
<Directory "/www/html/admin">
    order deny,allow
(谁在前先执行谁)
    allow from 172.25.254.37
    deny from all
    require all granted
设置用户加密
DocumentRoot "/www/html"
<Directory "/www/html/">
 require all granted
</Directory>
<Directory "/www/html/admin">
     #order deny,allow
    #allow from 172.25.254.37
    #deny from all
    #require all granted
    AllowOverride all
    Authuserfile  /etc/httpd/passwdfile
    Authname "please input your nameand password"
    Authtype basic
    *Require valid-user
</Directory>
[root@httpserver html]# mkdir admin
[root@httpserver html]# ls
admin  test.html
[root@httpserver html]# cd admin/
[root@httpserver admin]# ls
[root@httpserver admin]# vim test.html
[root@httpserver admin]# vim test.html
[root@httpserver admin]# htpasswd -cm passfile admin
New password:
Re-type new password:
Adding password for user admin
[root@httpserver admin]#
虚拟主机
虚拟主机允许您从一个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>
51. <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
www.westos.org
6Demo:
建立网页发布目录,并设置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
7# 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
举例
[root@httpserver admin]# cd /etc/httpd/conf.d/
[root@httpserver conf.d]# ls
autoindex.conf  login.conf   news.conf README    userdir.conf
default.conf    manual.conf  php.conf  ssl.conf  welcome.conf
[root@httpserver conf.d]# vim default.conf
[root@httpserver conf.d]# cat default.conf
<Virtualhost _default_:80>
         DocumentRoot"/www/html"
         customlog "logs/default.log"combined
</Virtualhost>
<Directory "/www/html">
       Require all granted
</Directory>

[root@httpserver conf.d]# vim news.conf
[root@httpserver conf.d]# cat news.conf
<Virtualhost *:80>
     ServerName news.westos.com
     DocumentRoot "/www/virtual/news/html"
     Customlog "logs/news.log"combined
</Virtualhost>
<Directory "/www/virtual/news/html">
    Require all granted
</Directory>
[root@httpserver conf.d]# mkdir -p /www/virtual/news/html
[root@httpserver conf.d]# vim /www/virtual/news/html/test.html
[root@httpserver conf.d]# systemctl restart httpd
在测试端(浏览器检测)
[root@httpserver conf.d]# vim /etc/hosts
[root@httpserver conf.d]# cat /etc/hosts
127.0.0.1   localhostlocalhost.localdomain localhost4 localhost4.localdomain4
::1         localhostlocalhost.localdomain localhost6 localhost6.localdomain6

172.25.254.254 classroom.example.com
172.25.254.254 content.example.com
172.25.254.137 www.westos.com news.westos.com
      CGI
通用网关接口(CGI)是网站上放置动态内容的最简单的方法。CGI脚本可用于许多目
,但是谨慎控制使用哪个CGI脚本以及允许谁添加和运行这些脚本十分重要。编写质量差的CGI
脚本可能为外部攻击者提供了破坏网站及其内容安全性的途径。因此,Web服务器级别和
SELinux
策略级别,都存在用于限制CGI脚本使用的设置。
Example:
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
# ll -dZ /var/www/cgi-bin/
drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0/var/www/cgi-bin/
www.westos.org
21php
语言支持:
安装php软件包,其中包含mod_php模块:
# yum install -y php
模块配置文件: /etc/httpd/conf.d/php.conf
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
DirectoryIndex index.php
server0上构建php练习环境,此脚本会自动配置mariadb,并生成
/var/www/html/index.php
动态网页:
# lab phpdb setup
安装php的数据库支持:
# yum install -y php-mysql
重启httpd服务后,测试网页是否访问正常.
注意当web服务器连接的数据库在远程时,需要改变Selinux:
# setsebool -P httpd_can_network_connect_db=1
# setsebool -P httpd_can_network_connect=1
(
如果数据库的端口不是3306,需要改此项)
www.westos.org
22WSGI
提供python语言支持:
安装mod_wsgi软件包:
# yum install -y mod_wsgi
执行脚本,会生成python测试文件/home/student/webapp.wsgi:
# lab webapp setup
在虚拟主机中加入以下参数:
<VirtualHost *:443>
servername webapp0.example.com
...
WSGIScriptAlias / /srv/webapp0/www/webapp.wsgi
...
</VirtualHost>
重启httpd服务,并在desktop0上测试:
# curl -k https://webapp0.example.com
举例
1.php
[root@httpserver ~]# cd /www/html
[root@httpserver html]# ls
admin  cgi  index.php test.html
[root@httpserver html]# vim index.php
[root@httpserver html]# cat index.php
<?php
    phpinfo();
?>
[root@httpserver html]# yum install php -y
2.cgi
[root@httpserver ~]# cd /www/html
[root@httpserver html]# ls
admin    index.php  test.html
[root@httpserver html]# mkdir cgi/
[root@httpserver html]# yum install httpd-manual -y
[root@httpserver html]# systemctl restart httpd
[root@httpserver html]# cd cgi/
[root@httpserver cgi]# vim index.cgi
[root@httpserver cgi]# cat index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[root@httpserver cgi]# chmod +x index.cgi
[root@httpserver html]# vim /etc/httpd/conf/httpd.conf
178 <IfModule dir_module>
179     DirectoryIndex test.htmlindex.html index.cgi
180 </IfModule>
[root@httpserver cgi]# systemctl restart httpd
[root@httpserver cgi]# setenforce 0
[root@httpserver cgi]# semanage fcontext -l | grep http

/var/www/[^/]*/cgi-bin(/.*)?                       all files         system_u:object_r:httpd_sys_script_exec_t:s0

[root@httpserver cgi]# ls -Zd /www/html/cgi/
[root@httpserver cgi]# semanage fcontext -a -t "httpd_sys_content_t"'/www/html/cgi(/.*)?'
[root@httpserver cgi]# restorecon -FvvR /www/html/cgi
restorecon reset /www/html/cgi context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:httpd_sys_content_t:s0
restorecon reset /www/html/cgi/index.cgi contextunconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:httpd_sys_content_t:s0
[root@httpserver cgi]# vim /etc/httpd/conf/httpd.conf
[root@httpserver cgi]# cat /etc/httpd/conf/httpd.conf
<IfModule dir_module>
    DirectoryIndex test.html index.htmlindex.cgi
</IfModule>
自定义自签名证书
如果加密的通信非常重要,而经过验证的身份不重要,管理员可以通过生成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
12
记录生成的证书(server0.example.com.crt)和关联的私钥(
server0.example.com.key)

13
继续使用对话框,并选择合适的密钥大小。(默认的2048位密钥为推荐值)
14
在生成随机数时比较慢,敲键盘和移动鼠标可以加速
15
拒绝向认证机构(CA)发送证书请求(CSR)
拒绝加密私钥
16
为服务器提供合适的身份。Common Name必须与服务器的主机全名完全匹配。
(
注意,任何逗号都应使用前导反斜线[\]进行转义)
www.westos.org
17
安装证书及其私钥
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, SSLCertificateFileSSLCertificateKeyFile指令设置为分别指
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 SecurityException”[确认安全例外])
18
网页重写
把所有80端口的请求全部重定向由https来处理
<Virtualhost *:80>
ServerName www0.example.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
19Example: /etc/httpd/conf.d/www0.conf
<VirtualHost *:443>
servername www0.example.com
documentroot /srv/www0/www
SSLEngine on
SSLCertificateChainFile /etc/pki/tls/certs/example-ca.crt
SSLCertificateFile /etc/pki/tls/certs/www0.crt
SSLCertificateKeyFile /etc/pki/tls/private/www0.key
<Directory "/srv/www0/www">
require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
servername www0.example.com
rewriteengine on
rewriterule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值