1、安装
yum install httpd -y ##apache软件
yum install httpd-manual ##apache的手册
systemctl start httpd
systemctl enable httpd
systemctl start firewalld.service
firewall-cmd --permanent --add-service=http ##火墙允许http
firewall-cmd --reload ##火墙重新加载策略
vim /var/www/html/index.html ##写一个发布页面,便于实验效果查看
测试: 浏览器搜索172.25.254.147
2、基础配置(以下实验在防火墙关闭,selinux开启情况下进行的)
/etc/httpd/conf ##主配置目录
/etc/httpd/conf/httpd.conf ##主配置文件
/etc/httpd/conf.d/ ##子配置目录
/etc/httpd/conf.d/*.conf ##子配置文件
/var/www/html ##默认发布目录
index.html ##默认发布文件
80 ##默认端口
httpd_sys_content_t ##默认安全上下文
apache ##程序开启默认用户
/etc/httpd/logs/* ##apache日志
1)修改默认端口、修改默认发布文件
vim /etc/httpd/conf/httpd.conf ##修改主配置文件
164 DirectoryIndex text.html index.html ##修改发布文件(哪个发布文件在前,先读取哪个,当前一个不存在时,读取后一个)
42 Listen 8080 ##修改默认端口(如果防火墙开,端口修改后访问时需要加端口号,且防火墙可能会阻拦此端口)
cd /var/www/html/
vim text.html
hello text
systemctl restart httpd
测试:浏览器搜索172.25.254.147:8080
防火墙开启时,修改端口,可执行下面命令允许修改后端口通过
firewall-cmd --permanent --add-port=8080/tcp ##修改防火墙允许8080端口通过
firewall-cmd --reload
实验: 关掉防火墙(除去防火墙对实验干扰),修改端口和发布文件 访问查看
mkdir /westos/html -p ##新建一个发布目录
vim /westos/html/index.html ##新建一个发布文件
hello wang
vim /etc/httpd/conf/httpd.conf ##修改主配置文件
119 #DocumentRoot "/var/www/html"
120 DocumentRoot "/westos/html" ##指定发布目录
124 ##对新指定的发布目录授权
125 Require all granted
126
semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?' ##修改安全上下文
restorecon -RvvF /westos/ ##刷新
systemctl restart httpd ##重启服务
测试:修改发布目录后访问,与修改前对比
实验结束后恢复原来设置
实验:
3、apache的虚拟主机
在真实主机
vim /etc/hosts中
172.25.254.137 www.westos.com news.westos.com music.westos.com
apache服务端:
mkdir /var/www/virtual/westos.com/music -p
mkdir /var/www/virtual/westos.com/news -p
vim /var/www/virtual/westos.com/music/index.html
music page
vim /var/www/virtual/westos.com/news/index.html
news page
cd /etc/httpd/conf.d/
vim adefault.conf
1 ##当没有指定时查看/var/www/html默认发布文件
2 DocumentRoot "/var/www/html"
3
vim news.conf
1
2 ServerName "news.westos.com"
3 DocumentRoot "/var/www/virtual/westos.com/news" ##访问news时的发布文件
4 CustomLog logs/news.log combined
5
6 ##授权
7 Require all granted
8
cp news.conf music
vim music.conf
1
2 ServerName "music.westos.com"
3 DocumentRoot "/var/www/virtual/westos.com/music" ##访问news时的发布文件
4 CustomLog logs/music.log combined
5
6 ##授权
7 Require all granted
8
systemctl restart httpd
测试:访问news.westos.com ,music.westos.com
实验:
真实主机
apache服务端
4、apache内部的访问控制
1)黑白名单
vim adefault.conf
1
2 DocumentRoot "/var/www/html"
3
4
5 Require all granted
6 Order Deny,Allow ##谁在前先读谁,此处为白名单(Allow,Deny则为黑名单)
7 Allow from 172.25.254.37 ##允许172.25.254.37主机登陆
8 Deny from all ##不允许所有主机登陆
9
systemctl resatrt httpd
测试:分别设置不同黑白名单登陆
mkdir /var/www/html/admin
vim /var/www/html/admin/index.html
admin
htpasswd -cm /etc/httpd/htuser admin ##建立admin用户
htpasswd -m /etc/httpd/htuser admin1 ##建立admin1用户,建立第二个用户时 是-m, -cm会覆盖之前的用户
cat /etc/httpd/htuser ##查看生成用户
cd /etc/httpd/conf.d/
ls
vim adefault.conf ##修改配置文件
9
10 AuthUserFile "/etc/httpd/htuser" ##查看用户
11 AuthName "Please input username and password" ##请输入帐号密码
12 AuthType Basic ##基本检测,即检测帐号和密码
13 Require user admin ##允许admin用户登陆
14 # Require valid-user ##允许所有用户登陆
15
systemctl resatrt httpd ##重启服务
测试: 真机浏览器访问172.25.254.137/admin
注意及时清理缓存 ctrl+shift+delete
实验:
5、系统支持的语言
1)html ##系统默认支持
2)php
vim /var/www/html/index.php
1
yum install php -y
systemctl restart httpd
测试:浏览器172.25.254.137/index.php
实验:
3)cgi
mkdir -p /var/www/html/cgi
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?' ##修改上下文权限
restorecon -RvvF /var/www/html/cgi/
vim /var/www/html/cgi/index.cgi ##cgi语言写的一段指令
1 #!/usr/bin/perl
2 print "Content-type: text/html\n\n";
3 print `date`;
chmod +x /var/www/html/cgi/index.cgi ##给一个操作的权限
vim adefault.conf
16
17
18 Options +ExecCGI ##增添执行cgi权力
19 AddHandler cgi-script .cgi
20
systemctl restart httpd
测试:172.25.254.137/cgi/index.cgi
yum install mod_wsgi -y ##安装一个软件
vim /var/www/html/cgi/westos.wsgi ##此文件中写的内容模板————westos.wsgi(此出是python写的一段命令)
1 #!/usr/bin/env python
2 import time
3
4 def application (environ, start_response):
5 response_body = 'UNIX EPOCH time is now: %s\n' % time.time()
6 status = '200 OK'
7 response_headers = [('Content-Type', 'text/plain'),
8 ('Content-Length', '1'),
9 ('Content-Length', str(len(response_body)))]
10 start_response(status, response_headers)
11 return [response_body]
12
vim /etc/httpd/conf.d/adefault.conf
1
2 DocumentRoot "/var/www/html"
3 WSGIScriptAlias /WSGI /var/www/html/cgi/westos.wsgi ##查询WSGI时执行westos.wsgi
4
systemctl restart httpd
测试:浏览器172.25.254.137/WSGI
yum install mod_ssl -y ##按装软件使可以使用https
yum install crypto-utils -y ##安装软件使可以修改安全证书
genkey www.westos.com ##修改安全证书
注意不要上传
vim /etc/httpd/conf.d/ssl.conf
100 #SSLCertificateFile /etc/pki/tls/certs/localhost.crt
101
102 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt ##安全认证
108 #SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
109
110 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key ##钥匙存放的位置
systemctl restart httpd
浏览器清除之前证书
https://172.25.254.137
查看修改后的证书信息
mkdir /var/www/virtual/westos.com/login
cd /var/www/virtual/westos.com/login/
vim index.html
login page
cd /etc/httpd/conf.d/
cp -p music.conf zlogin.conf
vim zlogin.conf
1 ##修改端口443
2 ServerName "login.westos.com"
3 DocumentRoot "/var/www/virtual/westos.com/login"
4 CustomLog logs/login.log combined
5 SSLEngine on ##开启设置
6 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt ##看/etc/httpd/conf.d/ssl.conf
7 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
8
9
10 Require all granted
11
12
13 ServerName "login.westos.com"
14 RewriteEngine on
15 RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301] ##地址栏输入的地址专成加密
16
systemctl restart httpd
^(/.*)$ ##客户在浏览器地址栏中输入的所有字符
https:// ##强制客户加密访问
%{HTTP_HOST} ##客户请求主机
$1 ##“$1”表示^(/.*)$的值
[redirect=301] ##永久重写 302表示临时
测试: 在真机中 vim /etc/hosts
login.westos.com
浏览器搜索login.westos.com查看效果
8、正向代理
真机上-- 配置保证137主机可以上网—真机作为路由器让137服务主机可以上网
systemctl start firewalld
firewall-cmd --list
firewall-cmd --add-masquerade
137主机--可以上网(服务主机)
yum install squid -y ##安装代理软件
vim /etc/squid/squid.conf ##修改配置文件
56 http_access allow all ##允许所有人
59 http_port 3128 ##代理的端口号,此行不需要改动,用户端在配置时需要查看
62 cache_dir ufs /var/spool/squid 100 16 256 ##缓存下的存放到/var/spool/squid下,100M的存储,多余的会覆盖,有16个子目录,每个子目录下256二级目录
systemctl stop firewalld
237主机--不能上网(用户主机)
,网页配置,添加代理
浏览器中Edit--->Perferences-->Advanced-->Network-->Setting-->Manual proxy-->172.25.254.137 3128-->点Use this proxy-->完成 ##此处3128即137主机中代理程序的端口,172.25.254.137是代理所在主机
测试: 搜索www.baidu.com 成功
shell中ping www.baidu.com 无法访问
注意:测试时及时清理缓存
实验结束后关掉代理
9、反向代理–用户不用配置,企业服务端配置
1)反向代理
用xiian主机代理shenzhen主机
147主机做为主服务器
hostnamectl set-hostname shenzhen
cd /var/www/html/
vim index.html
1 172.25.254.147
237主机作为代理服务器,此主机没有httpd(如果有,apache关掉)
hostnamectl set-hostname xiian
yum install squid -y
vim /etc/squid/squid.conf
56 http_access allow all
59 http_port 80 vhost vport ##此处端口由于代理147服务器,147的apache端口是80,此处80是虚拟端口
60 cache_peer 172.25.254.147 parent 80 0 proxy-only ##代理的服务器是172.25.254.147,关系是parent,代理端口是80,由于没有备用代理服务器,所以是0,proxy-only表示仅支持代理。
62 cache_dir ufs /var/spool/squid 100 16 256
systemctl restart squid
systemctl stop firewalld
测试:用真机浏览器 172.25.254.247
可以看到主服务器中的发布页内容
2)轮循
237主机上
vim /etc/squid/squid.conf
59 http_port 80 vhost vport
60 cache_peer 172.25.254.147 parent 80 0 proxy-only name=web1 round-robin originserver weight=2 ##name=web1命名;round-robin表示轮循;originserver设置后才能轮循;weight=2权重,2即表示147服务两次再轮循到100
61 cache_peer 172.25.254.100 parent 80 0 proxy-only name=web2 round-robin originserver
62 cache_peer_domain web1 web2 www.westos.com ##访问www.westos.com时web1 web2工作
systemctl restart squid
真机解析 www.westos.com
vim /etc/hosts
172.25.254.247 www.westos.com
浏览器搜索 www.westos.com
netstat -antlupe |grep 80 ##查看80端口状态
10、常见两种web架构
LAMP = Linux + Apache + Mysql +PHP
LNMP = Linux + Nginx + Mysql +PHP/Perl/Python
11、上线
shenzhen主机
[root@shenzhen html]# pwd
/var/www/html
unzip Discuz_X3.2_SC_UTF8.zip ## upload 目录中的文件在服务器
systemctl start mariadb
cd readme/
ls
vim readme.txt
cd ..
ls
cd upload/
ll
chmod 777 -R config/ ##根据readme.txt中的要求调整权限
chmod 777 -R data/
getenforce
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/upload(/.*)?'
restorecon -FvvR /var/www/html/upload/
chmod 777 uc_* -R