找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1220|回复: 0

apache服务器

[复制链接]

304

主题

42

回帖

1346

积分

管理员

积分
1346
发表于 2021-4-21 09:01:01 | 显示全部楼层 |阅读模式
  1. 实验-安装apache,并提供服务
  2. 在gzpu1上
  3. yum install -y httpd
  4. echo basictest > /var/www/html/index.html
  5. systemctl restart httpd
  6. systemctl enable  httpd
  7. netstat -anplut| grep httpd
  8. firewall-cmd --permanent --add-service=http
  9. firewall-cmd --reload

  10. 在gzpu1和gzpu2上
  11. firefox &
  12. http://192.168.100.1

  13. 在gzpu1和gzpu2上
  14. http://192.168.100.1/soft

  15. 实验-基于域名的虚拟主机
  16. mkdir /var/www/gzpu
  17. mkdir /var/www/bing
  18. echo gzpu > /var/www/gzpu/index.html
  19. echo bing > /var/www/bing/index.html

  20. cp /etc/unbound/local.d/gzpu.com.conf /etc/unbound/local.d/bing.com.conf
  21. vim /etc/unbound/local.d/bing.com.conf
  22. :%s/gzpu/bing/g
  23. systemctl restart unbound

  24. vim /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf        #模板
  25. vim /etc/httpd/conf.d/0.conf
  26. <VirtualHost 192.168.100.1:80>
  27.     ServerAdmin admin@XXX.com
  28.     DocumentRoot "/var/www/html"
  29.     ServerName 192.168.100.1
  30.     ErrorLog "/var/log/httpd/192.168.100.1-error_log"
  31.     CustomLog "/var/log/httpd/192.168.100.1-access_log" common
  32. </VirtualHost>

  33. vim /etc/httpd/conf.d/gzpu.com.conf
  34. <VirtualHost www.gzpu.com:80>
  35.     ServerAdmin admin@gzpu.com
  36.     DocumentRoot "/var/www/gzpu"
  37.     ServerName www.gzpu.com
  38.     ErrorLog "/var/log/httpd/www.gzpu.com-error_log"
  39.     CustomLog "/var/log/httpd/www.gzpu.com-access_log" common
  40. </VirtualHost>

  41. cp /etc/httpd/conf.d/gzpu.com.conf /etc/httpd/conf.d/bing.com.conf
  42. :%s/gzpu/bing/g
  43. systemctl restart httpd

  44. 在gzpu1和gzpu2上
  45. http://www.gzpu.com
  46. http://www.bing.com

  47. 实验-基于IP的虚拟主机
  48. 添加一块网卡,设置IP为192.168.100.11/24
  49. nmcli device show | grep DEVICE
  50. nmcli connection show
  51. connection add type ethernet con-name eth1 ifname eno33554984
  52. nmcli connection modify eth1 ipv4.method manual ipv4.addresses 192.168.100.11/24
  53. nmcli connection down eth1
  54. nmcli connection up eth1

  55. vim /etc/httpd/conf.d/0.conf
  56. <VirtualHost 192.168.100.1:80>
  57.     ServerAdmin root@gzpu.com
  58.     DocumentRoot /var/www/gzpu
  59.     ServerName www.gzpu.com
  60.     ErrorLog "/var/log/httpd/www.gzpu.com-error_log"
  61.     CustomLog "/var/log/httpd/www.gzpu.com-access_log" common
  62. </VirtualHost>
  63. <VirtualHost 192.168.100.11:80>
  64.     ServerAdmin root@bing.com
  65.     DocumentRoot /var/www/bing
  66.     ServerName www.bing.com
  67.     ErrorLog "/var/log/httpd/www.bing.com-error_log"
  68.     CustomLog "/var/log/www.bing.com-access_log" common
  69. </VirtualHost>
  70. systemctl restart httpd

  71. 在gzpu1和gzpu2上
  72. http://192.168.100.1/index.html                #不加index.html可能网页缓存会指向php页面
  73. http://192.168.100.11

  74. 实验-基于端口的虚拟主机
  75. semanage port -l | grep http
  76. semanage port -a -t http_port_t -p tcp 8899
  77. firewall-cmd --permanent --add-port=8899/tcp
  78. firewall-cmd --reload
  79. vim /etc/httpd/conf/httpd.conf
  80. Listen 8899        #增加

  81. mkdir /var/www/8899
  82. echo 8899 > /var/www/8899/index.html

  83. vim /etc/httpd/conf.d/0.conf
  84. <VirtualHost 192.168.100.1:80>
  85.     DocumentRoot "/var/www/html"
  86.     ServerName 192.168.100.1
  87. </VirtualHost>
  88. <VirtualHost 192.168.100.1:8899>
  89.     DocumentRoot "/var/www/8899"
  90.     ServerName 192.168.100.1
  91. </VirtualHost>
  92. systemctl restart httpd

  93. 在gzpu1和gzpu2上
  94. http://192.168.100.1
  95. http://192.168.100.1:8899

  96. 实验-LAMP
  97. yum install -y php* mariadb*
  98. unzip Discuz.zip
  99. rm -rf /var/www/gzpu/*
  100. cp -rf upload/* /var/www/gzpu/
  101. semanage fcontext -l | grep http | grep rw
  102. chcon -R -t httpd_sys_rw_content_t /var/www/gzpu/
  103. chown -R apache:apache /var/www/gzpu/
  104. systemctl restart mariadb
  105. systemctl enable mariadb
  106. mysqladmin -u root password '123456'
  107. systemctl restart httpd

  108. 在gzpu1和gzpu2上
  109. http://www.gzpu.com        #需要输入数据库密码123456,更改表前缀

  110. 实验-软链接网站
  111. mkdir /local
  112. echo gzputest > /local/index.html
  113. semanage fcontext -a -t httpd_sys_content_t '/local(/.*)?'
  114. restorecon -vvFR /local
  115. ln -s /local/ /var/www/html/soft

  116. 实验-alias       
  117. 还原gzpu1到基于域名的虚拟主机环境
  118. mkdir /disk
  119. echo disk > /disk/index.html
  120. semanage fcontext -a -t httpd_sys_content_t '/disk(/.*)?'
  121. restorecon -vvFR /disk/

  122. vim /etc/httpd/conf.d/httpd-vhosts.conf
  123. <VirtualHost *:80>
  124.     ServerAdmin root@gzpu.com
  125.     DocumentRoot /var/www/gzpu
  126.     ServerName www.gzpu.com
  127.     Alias /net /disk                #增加
  128.     ErrorLog "/var/log/httpd/www.gzpu.com-error_log"
  129.     CustomLog "/var/log/httpd/www.gzpu.com-access_log" common
  130. </VirtualHost>
  131. <Directory /disk>                #增加,添加对文件夹/disk的设置
  132.     AllowOverride none                #增加,不允许覆盖写入
  133.     Require all granted                #增加,允许所有人访问
  134. </Directory>                        #增加

  135. 在gzpu1和gzpu2上
  136. http://www.gzpu.com/net

  137. 实验-调用脚本

  138. vim /var/www/cgi-bin/shell.sh
  139. #!/bin/bash
  140. echo -en "Content-Type: text/html; charset=UTF-8\n\n";
  141. date +%c
  142. #注意复制时可能会自动生成#

  143. vim /var/www/cgi-bin/perl.pl
  144. #!/usr/bin/perl
  145. print "Content-Type: text/html; charset=UTF-8\n\n";
  146. $now=localtime();
  147. print "$now\n";
  148. #注意复制时可能会自动生成#

  149. yum install -y mod_wsgi
  150. vim /var/www/cgi-bin/python.py
  151. #!/usr/bin/env python
  152. import time

  153. def application (environ, start_response):
  154.     response_body = 'UNIX EPOCH time is now: %s\n' % time.time()
  155.     status = '200 OK'
  156.     response_headers = [('Content-Type', 'text/plain'),
  157.                         ('Content-Length', '1'),
  158.                         ('Content-Length', str(len(response_body)))]
  159.     start_response(status, response_headers)
  160.     return [response_body]

  161. chmod a+x /var/www/cgi-bin/shell.sh
  162. chmod a+x /var/www/cgi-bin/perl.pl
  163. #shell和pear必须有执行权限,python调用模块可以不加执行权限

  164. vim /etc/httpd/conf.d/gzpu.conf
  165. <VirtualHost *:80>
  166.     ServerAdmin root@gzpu.com
  167.     DocumentRoot /var/www/gzpu
  168.     ServerName www.gzpu.com
  169.     ErrorLog "/var/log/httpd/www.gzpu.com-error_log"
  170.     CustomLog "/var/log/httpd/www.gzpu.com-access_log" common
  171. <IfModule alias_module>                                #增加
  172. ScriptAlias /jiaoben/ "/var/www/cgi-bin/"        #增加,支持shell和perl
  173. </IfModule>                                        #增加
  174. WSGIScriptAlias  /python  /var/www/cgi-bin        #增加,支持python
  175. </VirtualHost>

  176. systemctl restart httpd

  177. 在gzpu1和gzpu2上
  178. http://www.gzpu.com/jiaoben/pear.pl
  179. http://www.gzpu.com/jiaoben/shell.sh
  180. http://www.gzpu.com/python/python.py

  181. 实验-拒绝访问
  182. 在gzpu1上
  183. mkdir /var/www/gzpu/gzpu2deny
  184. echo gzpu2deny > /var/www/gzpu/gzpu2deny/index.html

  185. vim /etc/httpd/conf.d/gzpu.conf
  186. <VirtualHost *:80>
  187.     ServerAdmin root@gzpu.com
  188.     DocumentRoot /var/www/gzpu
  189.     ServerName www.gzpu.com
  190.     ErrorLog "/var/log/httpd/www.gzpu.com-error_log"
  191.     CustomLog "/var/log/httpd/www.gzpu.com-access_log" common
  192. </VirtualHost>
  193. <Directory "/var/www/gzpu/gzpu2deny">
  194.     order allow,deny
  195.     allow from all
  196.     deny from 192.168.100.2        #网段采用192.168.100
  197. </Directory>
  198. <VirtualHost *:80>
  199. systemctl restart httpd

  200. gzpu2不可以访问http//www.gzpu.com/gzpu2deny,其他机器都可以访问

  201. 一些例子
  202. Order   deny,allow //默认充许所有主机访问
  203. Deny  from  192.168.0.100  //单独禁止

  204. Order deny,allow
  205. allow from all
  206. deny from 219.204.253.8
  207. #全部都可以通行
  208. -------------------------------
  209. Order deny,allow
  210. deny from 219.204.253.8
  211. allow from all
  212. #全部都可以通行
  213. -------------------------------
  214. Order allow,deny
  215. deny from 219.204.253.8
  216. allow from all
  217. #只有219.204.253.8不能通行
  218. -------------------------------
  219. Order allow,deny
  220. allow from all
  221. deny from 219.204.253.8
  222. #只有219.204.253.8不能通行
  223. -------------------------------
  224. -------------------------------
  225. Order allow,deny
  226. deny from all
  227. allow from 219.204.253.8
  228. #全部都不能通行
  229. -------------------------------
  230. Order allow,deny
  231. allow from 219.204.253.8
  232. deny from all
  233. #全部都不能通行
  234. -------------------------------
  235. Order deny,allow
  236. allow from 219.204.253.8
  237. deny from all
  238. #只允许219.204.253.8通行
  239. -------------------------------
  240. Order deny,allow
  241. deny from all
  242. allow from 219.204.253.8
  243. #只允许219.204.253.8通行
  244. -------------------------------
  245. --------------------------------
  246. Order deny,allow
  247. #全部都可以通行(默认的)
  248. -------------------------------
  249. Order allow,deny
  250. #全部都不能通行(默认的)
  251. -------------------------------
  252. Order allow,deny
  253. deny from all
  254. #全部都不能通行
  255. -------------------------------
  256. Order deny,allow
  257. deny from all
  258. #全部都不能通行
  259. -------------------------------
  260. 对于上面两种情况,如果换成allow from all,则全部都可以通行!
  261. -------------------------------
  262. Order deny,allow
  263. deny from 219.204.253.8
  264. #只有219.204.253.8不能通行
  265. -------------------------------
  266. Order allow,deny
  267. deny from 219.204.253.8
  268. #全部都不能通行
  269. -------------------------------
  270. Order allow,deny
  271. allow from 219.204.253.8
  272. #只允许219.204.253.8通行
  273. -------------------------------
  274. Order deny,allow
  275. allow from 219.204.253.8
  276. #全部都可以通行

  277. 实验-账号密码访问
  278. htpasswd -cm /etc/httpd/.htpasswd  gzpu                #设置用户gzpu的密码为gzpu
  279. htpasswd -m /etc/httpd/.htpasswd  bing                #设置用户bing的密码为bing新增用户不能加参数c,否则又会重建文件,之前的账号密码就没了
  280. <virtualhost 192.168.100.1:80>
  281. servername www.gzpu.com
  282. documentroot /var/www/gzpu/
  283. </virtualhost>
  284. <Directory /var/www/gzpu>
  285. AuthName "gzpu"
  286. AuthType Basic
  287. AuthUserFile /etc/httpd/.htpasswd
  288. require valid-user
  289. </Directory>

  290. 实验-ssl
  291. 在gzpu1上制作用于认证网站的证书和key
  292. cd /etc/pki/tls/certs
  293. make gzpu.crt
  294. Enter pass phrase:                                                #输入123.com
  295. Verifying - Enter pass phrase:                                        #输入123.com
  296. Enter pass phrase for gzpu.key:                                        #输入123.com
  297. Country Name (2 letter code) [XX]:                                #输入CN
  298. State or Province Name (full name) []:                                #输入BEIJING
  299. Locality Name (eg, city) [Default City]:                        #输入BEIJING
  300. Organization Name (eg, company) [Default Company Ltd]:                #输入REDHAT
  301. Organizational Unit Name (eg, section) []:                        #输入WEB
  302. Common Name (eg, your name or your server's hostname) []:        #输入www.gzpu.com

  303. cp  gzpu.key  /etc/pki/tls/private/gzpu.key
  304. #相当于已经有了CA中心,并且CA中心已经颁发了证书gzpu.crt,一般证书保存在cert文件夹下,密钥保存在private文件夹下

  305. yum install -y mod_ssl.x86_64        #安装ssl模块
  306. httpd -M | grep -i mod_ssl        #查看apache加载的模块
  307. #如果报错AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message,一般是域名解析的错误,可以添加hosts记录,或者添加DNS记录,并更改配置文件的ServerName内容

  308. vim /etc/httpd/conf.d/ssl.conf
  309. SSLEngine off                #更改,今后使用对每个网站的单独引擎

  310. firewall-cmd --permanent --add-service=https
  311. firewall-cmd --reload

  312. mkdir /var/www/443
  313. echo html > /var/www/html/index.html
  314. echo 443 > /var/www/443/index.html
  315. cat /etc/httpd/conf.d/ssl.conf | grep -i ^ssl        #复制尾部5行

  316. vim /etc/httpd/conf.d/0.conf
  317. <VirtualHost *:80>
  318.     DocumentRoot /var/www/html
  319.     ServerName www.gzpu.com
  320. </VirtualHost>

  321. vim /etc/httpd/conf.d/443.conf                #增加加密的www.gzpu.com网站,新增行可用cat /etc/httpd/conf.d/ssl.conf | grep ^SSL | tail -n 5获得
  322. <VirtualHost *:443>
  323.     DocumentRoot /var/www/443
  324.     ServerName www.gzpu.com                                        #必须和证书输入的域名一致
  325.         SSLEngine on                                                #激活引擎
  326.         SSLProtocol all -SSLv2 -SSLv3                                #除了-SSLv2和-SSLv3协议
  327.         SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
  328.         SSLCertificateFile /etc/pki/tls/certs/gzpu.crt                #证书位置
  329.         SSLCertificateKeyFile /etc/pki/tls/private/gzpu.key        #密钥位置
  330. </VirtualHost>

  331. 在gzpu1和gzpu2上
  332. vim /etc/hosts
  333. 192.168.100.1        www.gzpu.com

  334. http://www.gzpu.com
  335. https://www.gzpu.com

  336. 实验-访问http网站自动转为https
  337. 在gzpu1上
  338. vim /etc/httpd/conf.d/443.conf
  339. <VirtualHost *:80>
  340.     DocumentRoot /var/www/gzpu
  341.     ServerName www.gzpu.com
  342.     RewriteEngine on                                        #增加,激活https从定向引擎
  343.     RewriteRule ^/(.*) https://%{HTTP_HOST}$1 [L]        #增加,http重定向为https
  344. </VirtualHost>
  345. <VirtualHost *:443>
  346.     DocumentRoot /var/www/443
  347.     ServerName www.gzpu.com
  348. SSLEngine on
  349. SSLProtocol all -SSLv2 -SSLv3
  350. SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
  351. SSLCertificateFile /etc/httpd/conf/gzpu.crt
  352. SSLCertificateKeyFile /etc/httpd/conf/gzpu.key
  353. </VirtualHost>
  354. <VirtualHost *:80>

  355. 在gzpu1和gzpu2上
  356. http://www.gzpu.com        #自动重定向为https://www.gzpu.com
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|外汇论坛 ( 粤ICP备16021788号 )

GMT+8, 2024-11-25 05:26 , Processed in 0.058190 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表