目录
一.Apache的作用 :
在web被访问时通常使用http://的方式
http:// ##超文本传输协议
http:// 超文本传输协议提供软件:
Apache
nginx
stgw
jfe
Tengine
二.Apache的安装 :
dnf install httpd.x86_64 -y
三.Apache的启用 :
systemctl enable --now httpd ##开启服务并设定服务位开机启动
firewall-cmd --list-all ##查看火墙信息
firewall-cmd --permanent --add-service=http ##在火墙中永久开启http访问
firewall-cmd --permanent --add-service=https ##在火墙中永久开启https访问
firewall-cmd --reload ##刷新火墙使设定生效
四. Apache的基本信息 :
服务名称: httpd
配置文件:
/etc/httpd/conf/httpd.conf ##主配置文件
/etc/httpd/conf.d/*.conf ##子配置文件
默认发布目录: /var/www/html
默认发布文件: index.html
默认端口: 80 #http
443 #https
用户: apache
日志: /etc/httpd/logs
五. Apache的基本配置 :
#1.Apache端口修改# (45行)
netstat -antlupe | grep httpd #查看默认端口号,为80
vim /etc/httpd/conf/httpd.conf #修改默认端口号,改成8080
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
systemctl restart httpd
测试:172.25.254.219:8080
#2.默认发布文件## (167左右)
vim /etc/httpd/conf/httpd.conf
167行 :DirectoryIndex westos.html index.html
vim /var/www/html/westos.html #写入内容westos's page
vim /var/www/html/index.heml #上面创建了,没有就写入 hello westos
systemctl restart httpd
测试:172.25.254.219 默认 以及 172.25.254.219/westos.html
#3.默认发布目录 (124左右)
mkdir /westos_apache
vim /etc/httpd/conf/httpd.conf
添加:
123 DocumentRoot "/westos_apache"
124 <Directory "/westos_apache>
125 Require all granted
126 </Directory>
并且注释掉原来的默认目录 #DocumentRoot "/var/www/html"vim /westos_apache/index.html ##建立默认发文件
semanage fcontext -a -t httpd_sys_content_t '/westos_apache(/.*)?' ##更改发目录安全上下文
restorecon -RvvF /westos_apache/
systemctl restart httpd
测试 :172.25.254.219
六.pache的访问控制 :
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
vim /etc/httpd/conf/httpd.conf
————基于ip访问
改:124
#白名单
<Directory "/var/www/html/westos">
Order Deny,Allow
Allow from 172.25.254.23
Deny from All
</Directory>
#黑名单
<Directory "/var/www/html/westos">
Order Allow,Deny
Allow from All
Deny from 172.25.254.23
</Directory>
————基于用户认证访问
创建两个虚拟用户
htpasswd -cm .htpasswd admin
htpasswd -m .htpasswd admin1
cat .htpasswd
注意:创建另一个用户的时候去掉c,不然会覆盖原文件的内容
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html 随便写内容
vim /etc/httpd/conf/httpd.conf
改:124
<Directory "/var/www/html/westos">
AuthUserfile /etc/httpd/.htpasswd ##指定认证文件
AuthName "Please input your name and password" ##认证提示语
AuthType basic ##认证类型
Require user admin ##允许通过的认证用户空格隔开用户
Require valid-user ##允许所有用户通过认证
</Directory>
七.Apache的虚拟主机 :
239 mkdir -p /var/www/vhost/westos.org/{news,music,qiang} ##创建几个文件
240 ls /var/www/vhost/westos.org/
241 echo news.westos.org> /var/www/vhost/westos.org/news/index.html
242 echo music.westos.org> /var/www/vhost/westos.org/music/index.html
243 echo leiqiang.westos.org> /var/www/vhost/westos.org/qiang/index.html
244 vim /etc/httpd/conf.d/vhosts.conf ##编写文件中的内容
<VirtualHost _default_:80> ##默认
DocumentRoot /var/www/html
CustomLog logs/default.log combined
</VirtualHost><VirtualHost *:80>
ServerName music.westos.org
DocumentRoot /var/www/vhost/westos.org/music
CustomLog logs/music.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName news.westos.org
DocumentRoot /var/www/vhost/westos.org/news
CustomLog logs/news.log combined
</VirtualHost><VirtualHost *:80>
ServerName leiqiang.westos.org
DocumentRoot /var/www/vhost/westos.org/leiqiang
CustomLog logs/leiqiang.log combined
</VirtualHost>然后在你浏览器中所在的主机中
vim /etc/hosts
172.25.254.123 www.westos.org music.westos.org news.westos.org leiqiang.westos.org
ping一下 ping music.westos.org 然后在浏览器输入 http://leiqiang.westos.org/就可以
八. Apache的语言支持 :
————1.php
cd /var/www/html/
266 ls
267 vim index.php ##编写默认发文件
写
<?php
phpinfo();
?>
268 dnf search php
269 dnf install php.x86_64
270 systemctl restart httpd
271 ls
测试:http://172.25.254.123/index.php
————2.cgi (给权限,给安装上下文)
cd /var/www/html/
mkdir cgi
cd cgi
ls
vim index.cgi
写入 :
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;chmod +x index.cgi
ls -Zd /var/www/cgi-bin
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?'
restorecon -RvvF /var/www/html/cgi/
./index.cgi
vim /etc/httpd/conf.d/vhosts.conf
写入
<Directory "/var/www/html/cgi">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>systemctl restart httpd
测试:http://172.25.254.123/cgi/index.cgi
————3.python :
mkdir wsgi
cd wsgi/
ls
vim index.wsgi
写入
def application(env, westos):
westos('200 ok',[('Content-Type', 'text/html')])
return [b'hello westos ahhahahahah!']
dnf install python3-mod_wsgi
systemctl restart httpdvim /etc/httpd/conf.d/vhosts.conf
写入
<VirtualHost *:80>
ServerName wsgi.westos.org
WSGIScriptAlias / /var/www/html/wsgi/index.wsgi
</VirtualHost>这此测试需要下载
然后在真机中vim /etc/hosts加入域名:wsgi.westos.org效果:
九.Apache的加密访问 :
1.安装加密插件
2.创建目录:
3.生成证书:
4.创建文件编写文件
mkdir /var/www/vhost/westos.org/login
vim /var/www/vhost/westos.org/login/index.html
写入: login.westos.org5.vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:443>
ServerName login.westos.org
DocumentRoot /var/www/vhost/westos.org/login
CustomLog logs/login.log combined
SSLEngine on
SSLCertificateFile /etc/httpd/tls/westos.org.crt
SSLCertificateKeyFile /etc/httpd/tls/westos.org.key<VirtualHost *:80>
ServerName login.westos.org
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</VirtualHost>重启httpd
6.在测试机中 vim /etc/hosts加入 login.westos.org
7.测试:
十.Squid+Apache
一.#squid 正向代理#
实验环境:
单网卡主机设定ip不能上网
双网卡主机设定ip1可以连接单网卡主机,设定ip2可以上网实验效果
让单网卡主机不能上网但浏览器可以访问互联网页1. 设置好双网卡的服务器,并且该服务器可以上网
16 dnf install squid -y
17 systemctl status squid.service
18 vim /etc/squid/squid.conf ##取消65行注释,56行deny改成allow
19 systemctl restart squid.service
20 netstat -antlupe | grep squid
21 firewall-cmd --permanent --add-service=squid
22 firewall-cmd --reload
23 firewall-cmd --list-all2.在另一台主机配置ip等,然后安装好火狐在浏览器中找到三道杠,然后Nrework Settings,选择Settings,选择Manual,HTTP Proxy填写代理商IP地址,Port写代理商端口号3128即可
二.#squid反向代理#
172.25.254.123 ##Apache服务器
172.25.254.223 ##squid,没有数据负责缓存1.在服务器(123)中:
dnf install httpd -y
systemctl enable --now httpd
firewall-cmd -permanent -add-service=http
firewall-cmd --reload
ifconfig
echo 172.25.254.223 >/var/www/html/index.html
在分代理机中,要有squid服务
配置文件vim /etc/squid/squid.conf
62,63行 : 62 http_port 80 vhost vport ##vhost 支持虚拟域名 vport 支持虚拟端口
63 cache_peer 172.25.254.123 parent 80 0 proxy-only #当172.25.254.30的80端口被访问会从172.25.254.20的80端口缓存数据
然后重启 systemctl restart squid
systemctl status squid
然后在你的真机中:172.25.254.223测试代理机器,就可以访问到服务器的相关资源