一.Apache的基础简介
阿帕奇用来提供超文本传输协议:http://
阿帕奇的基础信息
主配置目录: /etc/httpd/conf
主配置文件: /etc/httpd/conf/httpd.conf
子配置目录: /etc/httpd/conf.d/
子配置文件: /etc/httpd/conf.d/*.conf ## 在子配置目录中所有的以.conf结尾的文件
默认发布目录: /var/www/html
默认发布文件: /var/www/html/index.html ##默认发布目录中index.html文件名称固定
默认端口: 80
二.阿帕奇的安装与测试
前提条件:在阿帕器服务器安装前必须保证强制和级selinux处于开启状态,yum源派至正常,火墙处于打开状态
[root@localhost ~]# getenforce ##Selinux处于开启状态
Enforcing
[root@localhost ~]# ifconfig ##网络正常,属于静态网络
ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.4.105 netmask 255.255.255.0 broadcast 172.25.4.255
inet6 fe80::5054:ff:fedb:c711 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:db:c7:11 txqueuelen 1000 (Ethernet)
RX packets 105 bytes 9935 (9.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 97 bytes 13472 (13.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# yum repolist ##yum源配置正常
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
: manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
repo id repo name status
!westos rhel7.3 4,751
repolist: 4,751
[root@localhost yum.repos.d]# yum install httpd -y ##安装httpd服务
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-45.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================================
Installing:
httpd x86_64 2.4.6-45.el7 westos 1.2 M
Transaction Summary
======================================================================================================================================================
Install 1 Package
Total download size: 1.2 M
Installed size: 3.7 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : httpd-2.4.6-45.el7.x86_64 1/1
Verifying : httpd-2.4.6-45.el7.x86_64 1/1
Installed:
httpd.x86_64 0:2.4.6-45.el7
Complete!
[root@localhost ~]# systemctl start httpd ##开启服务
[root@localhost ~]# systemctl enable httpd ##设置为开机自启
[root@localhost ~]# firewall-cmd --list-all ##列出火墙信息
public (active)
target: default
icmp-block-inversion: no
interfaces: ens3
sources:
services: dhcpv6-client ftp ssh ##无http则需要添加
ports:
protocols:
masquerade: no
forward-ports:
sourceports:
icmp-blocks:
rich rules:
[root@localhost ~]# firewall-cmd --permanent --add-service=http ##永久允许http通过
success
[root@localhost ~]# firewall-cmd --reload ##重新加载
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens3
sources:
services: dhcpv6-client ftp http ssh ##加载成功
ports:
protocols:
masquerade: no
forward-ports:
sourceports:
icmp-blocks:
rich rules:
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.html
[root@localhost html]# cat index.html
<h1>hello lucky</h1>
测试
浏览器输入安装阿帕奇服务端的虚拟机i,出现发布文件则测试成功
三.阿帕奇默认端口的修改
修改http端口时分为两种情况,一种是修改为本身允许存在的端口;另一种是修改为不存在的端口
1.默认端口为80
[root@localhost ~]# netstat -antlupe | grep httpd
tcp6 0 0 :::80 :::* LISTEN 0 23009 931/httpd
2.修改端口为8080
[root@localhost~]#vim /etc/httpd/conf/httpd.conf ##在主配置文件中更改开放端口为8080
[root@localhost ~]# systemctl restart httpd.service
[root@localhost ~]# firewall-cmd --add-port=8080/tcp ##防火墙添加8080端口
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens3
sources:
services: dhcpv6-client ftp http ssh
ports: 8080/tcp ##端口添加成功
protocols:
masquerade: no
forward-ports:
sourceports:
icmp-blocks:
rich rules:
[root@localhost ~]# netstat -antlupe | grep httpd ##再次查看httpd的端口是否更改成功
tcp6 0 0 :::8080 :::* LISTEN 0 34619 3371/httpd
测试:
在浏览器端输入测试服务器的ip 172.25.254.105:8080
3.修改端口为8888
[root@localhost~]# vim /etc/httpd/conf/httpd.conf ##在主配置文件修改端口为8888
[root@localhost ~]# systemctl restart httpd.service
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details. ##重启服务出现报错,是因为selinux为强制级不允许没有的端口通过,此时导致服务无法启动,需要在selinux允许的端口中添加8888端口
[root@localhost ~]# semanage port -l | grep http ##查看selinux允许通过的端口,无8888,则需要进行强制性的添加
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
[root@localhost ~]# semanage port -a -t http_port_t -p tcp 8888 ##selinux强制添加端口8888
[root@localhost ~]# semanage port -l | grep http ##添加成功
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 8888, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
[root@localhost ~]# systemctl restart httpd.service
[root@localhost ~]# netstat -antlupe | grep http ##再次查看httpd的端口
tcp6 0 0 :::8888 :::* LISTEN 0 41832 4314/httpd
[root@localhost ~]# firewall-cmd --add-port=8888/tcp ##火墙允许8888端口通过,不许要重新加载火墙为短暂性添加
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens3
sources:
services: dhcpv6-client ftp http ssh
ports: 8080/tcp 8888/tcp
protocols:
masquerade: no
forward-ports:
sourceports:
icmp-blocks:
rich rules:
测试:浏览器输入172.25.4.105:8888
四.阿帕奇默认发布文件及目录的修改
1.阿帕奇默认发布文件的修改
默认发布文件就是访问apache时没有指定文件名称时默认访问的文件
这个文件可以指定多个,有访问顺序,按照其在配置文件中的顺序进行访问
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
index.html westos
[root@localhost html]# vim test ##建立新的发布目录
[root@localhost html]# ls
index.html test westos
[root@localhost html]# cat test
<h1> balabala</h1>
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
[root@localhost html]# systemctl restart httpd.service
测试:浏览器输入172.25.4.105
2.阿帕奇默认发布目录的更改
默认情况下apache的发布目录在/var/www/html
[root@localhost html]# pwd
/var/www/html
更改其默认发布目录在/
[root@localhost html]# mkdir /www/html -p ##/建立一个属性相同的目录作为发布目录
[root@localhost html]# semanage fcontext -a -t httpd_sys_content_t '/www/html(/.*)?' ##selinux属于开启状态,则需要姓改其安全上下文保持一致
[root@localhost html]# restorecon -FvvR /www/ ##刷新发布目录的安全上下文
restorecon reset /www context unconfined_u:object_r:default_t:s0->system_u:object_r:default_t:s0
restorecon reset /www/html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
[root@localhost html]# ls -Zd /www/html/ ##查看发布目录的安全上下文
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /www/html/
[root@localhost html]# vim /www/html/index.html ##建立新的发布文件
[root@localhost html]# cat /www/html/index.html
<h1> hai mian bao bao </h1>
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
[root@localhost html]# systemctl restart httpd.service
测试:浏览器输入172.25.4.105进行测试
五.阿帕奇Apach的访问控制
1.基于id的访问控制(针对主机的访问控制)
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
[root@localhost ~]# systemctl restart httpd.service
浏览器端测试
拒绝172.25.4.250访问,其他主机访问正常
其他主机访问
2.基于用户的控制(针对用户方式的访问控制)
[root@localhost httpd]# htpasswd -cm .htpass_file westos ##建立加密新用户westos;初次建立需要加参数cm
New password:
Re-type new password:
Adding password for user westos
[root@localhost httpd]# htpasswd -m .htpass_file westos1 ##建立加密新用户2westos1,第二次建立参数不需要加c,否则会覆盖第一次的加密文件
New password:
Re-type new password:
Adding password for user westos1
[root@localhost httpd]# cat .htpass_file ##查看加密文件
westos:$apr1$E4W3EvDz$0S8uU/8CzYwnsXspB/NAY.
westos1:$apr1$fBkXt3re$kKeEQIclniS3MSVMqAYD91
[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf ##第一次允许westos访问
[root@localhost httpd]# systemctl restart httpd.service
浏览器端测试:172.25.4.105
[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf ##第二次允许westos1访问
[root@localhost httpd]# systemctl restart httpd.service
浏览器端测试
六.Apach的虚拟主机
[root@localhost ~]# cd /var/www/html
[root@localhost html]# ls
index.html test westos
[root@localhost html]# cat westos
cat: westos: Is a directory
[root@localhost html]# vim index.html
[root@localhost html]# cat index.html
<h1>www.westos.com</h1>
[root@localhost html]# mkdir /web_virt_dir/{music,news}/html -p
[root@localhost html]# ll /web_virt_dir/
total 0
drwxr-xr-x. 3 root root 18 May 2 19:57 music
drwxr-xr-x. 3 root root 18 May 2 19:57 news
[root@localhost html]# ls -Zd /web_virt_dir/
drwxr-xr-x. root root system_u:object_r:default_t:s0 /web_virt_dir/
[root@localhost html]# semanage fcontext -a -t httpd_sys_content_t '.web_virt_dir(/.*)?'
[root@localhost html]# restorecon -RvvF /web_virt_dir/
restorecon reset /web_virt_dir context system_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
restorecon reset /web_virt_dir/music context system_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
restorecon reset /web_virt_dir/music/html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
restorecon reset /web_virt_dir/news context system_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
restorecon reset /web_virt_dir/news/html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
[root@localhost html]# vim /web_virt_dir/music/html/index.html ##编辑发布文件music
[root@localhost html]# cat /web_virt_dir/music/html/index.html
<h1>music.westos.com</h1>
[root@localhost html]# vim /web_virt_dir/news/html/index.html ##编辑发布文件news
[root@localhost html]# cat /web_virt_dir/news/html/index.html
<h1>news.westos.com</h1>
[root@localhost html]# cd /etc/httpd/conf.d
[root@localhost conf.d]# ls
autoindex.conf README userdir.conf welcome.conf
[root@localhost conf.d]# vim vhost.conf ##编辑子配置文件
[root@localhost conf.d]# systemctl restart httpd
测试
在测试端的主机本地解析文件/etc/hosts中,对www.westos.com; music.westos.com; news.westos.com 三个域名进行解析记录,然后通过域名进行访问,显示不同的发布文件:
测试结果
七.Apach支持的语言
1.PHP
[root@localhost ~]# cd /var/www/html
[root@localhost html]# ls
index.html test westos
[root@localhost html]# vim index.php ##编辑php发布文件
[root@localhost html]# cat index.php
<?php
phpinfo();
?>
[root@localhost html]# ls /etc/httpd/conf.d ##该目录中可以看出此时不支持php文件发布
autoindex.conf README userdir.conf vhost.conf welcome.conf
[root@localhost html]# systemctl restart httpd
[root@localhost html]# yum install php -y ##下载php插件
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
: manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
westos | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.4.16-42.el7 will be installed
--> Processing Dependency: php-common(x86-64) = 5.4.16-42.el7 for package: php-5.4.16-42.el7.x86_64
--> Processing Dependency: php-cli(x86-64) = 5.4.16-42.el7 for package: php-5.4.16-42.el7.x86_64
--> Running transaction check
---> Package php-cli.x86_64 0:5.4.16-42.el7 will be installed
---> Package php-common.x86_64 0:5.4.16-42.el7 will be installed
--> Processing Dependency: libzip.so.2()(64bit) for package: php-common-5.4.16-42.el7.x86_64
--> Running transaction check
---> Package libzip.x86_64 0:0.10.1-8.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
php x86_64 5.4.16-42.el7 westos 1.4 M
Installing for dependencies:
libzip x86_64 0.10.1-8.el7 westos 49 k
php-cli x86_64 5.4.16-42.el7 westos 2.7 M
php-common x86_64 5.4.16-42.el7 westos 564 k
Transaction Summary
================================================================================
Install 1 Package (+3 Dependent packages)
Total download size: 4.7 M
Installed size: 17 M
Downloading packages:
--------------------------------------------------------------------------------
Total 20 MB/s | 4.7 MB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libzip-0.10.1-8.el7.x86_64 1/4
Installing : php-common-5.4.16-42.el7.x86_64 2/4
Installing : php-cli-5.4.16-42.el7.x86_64 3/4
Installing : php-5.4.16-42.el7.x86_64 4/4
Verifying : php-5.4.16-42.el7.x86_64 1/4
Verifying : libzip-0.10.1-8.el7.x86_64 2/4
Verifying : php-common-5.4.16-42.el7.x86_64 3/4
Verifying : php-cli-5.4.16-42.el7.x86_64 4/4
Installed:
php.x86_64 0:5.4.16-42.el7
Dependency Installed:
libzip.x86_64 0:0.10.1-8.el7 php-cli.x86_64 0:5.4.16-42.el7
php-common.x86_64 0:5.4.16-42.el7
Complete!
[root@localhost html]# ls /etc/httpd/conf.d ##此时查看支持php语言
autoindex.conf php.conf README userdir.conf vhost.conf welcome.conf
[root@localhost html]# systemctl restart httpd
在浏览器测试此时支持php语言
172.25.4.105/index.php
2.cgi–perl语言
[root@localhost html]# mkdir cgi ##创建cgi目录
[root@localhost html]# ls
cgi index.html index.php test westos ##cgi出现则创建成功
[root@localhost html]# ls -Z ##查看安全上下文
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 cgi
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.php
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 test
[root@localhost html]# semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?' ##更改安全上下文
[root@localhost html]# restorecon -RvvF /var/www/html/cgi ##刷新安全上下文
restorecon reset /var/www/html/cgi context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:httpd_sys_script_exec_t:s0
[root@localhost html]# vim /var/www/html/cgi/index.cgi ##编辑cgi发布文件
[root@localhost html]# cat /var/www/html/cgi/index.cgi ##查看发布文件内容
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[root@localhost html]# python /var/www/html/cgi/index.cgi ##执行发布文件用python语言
Content-type: text/html
Traceback (most recent call last):
File "/var/www/html/cgi/index.cgi", line 3, in <module>
print `date`;
NameError: name 'date' is not defined
[root@localhost html]# chmod +x cgi/index.cgi ##给与发布文件执行权限
[root@localhost html]# ./cgi/index.cgi ##执行并查看执行效果
Content-type: text/html
Fri May 3 19:15:02 CST 2019
[root@localhost html]# vim /etc/httpd/conf.d/vhost.conf ##编辑子配置文件
[root@localhost html]# systemctl restart httpd.service
测试:浏览器端输入172.25.4.105/cgi/index.cgi
3.python语言 --webapp.wsgi
[root@localhost html]# yum install mod_wsgi.x86_64 -y ##安装wsgi插件
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
: manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package mod_wsgi.x86_64 0:3.4-12.el7_0 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
mod_wsgi x86_64 3.4-12.el7_0 westos 76 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 76 k
Installed size: 197 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mod_wsgi-3.4-12.el7_0.x86_64 1/1
Verifying : mod_wsgi-3.4-12.el7_0.x86_64 1/1
Installed:
mod_wsgi.x86_64 0:3.4-12.el7_0
Complete!
[root@localhost html]# rpm -ql mod_wsgi-3.4-12.el7_0.x86_64
/etc/httpd/conf.modules.d/10-wsgi.conf
/usr/lib64/httpd/modules/mod_wsgi.so
/usr/share/doc/mod_wsgi-3.4
/usr/share/doc/mod_wsgi-3.4/LICENCE
/usr/share/doc/mod_wsgi-3.4/README
[root@localhost html]# vim /etc/httpd/conf.d/vhost.conf ##编辑配置文件
[root@localhost html]# cd /var/www/cgi-bin
[root@localhost cgi-bin]# vim webapp.wsgi ##编辑发布文件
[root@localhost cgi-bin]# cat webapp.wsgi
#!/usr/bin/env python
import time
def application (environ, start_response):
response_body = 'UNIX EPOCH time is now: %s\n' % time.time()
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', '1'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
[root@localhost cgi-bin]# systemctl restart httpd ##重启服务
测试:主机端添加wsgi.westos.com进行域名解析
浏览器端输入wsgi.westos.com