一、Apache的作用
在web被访问时通常使用http://的方式
http:// ##超文本传输协议
http:// 超文本传输协议提供软件:
Apache
nginx
stgw
jfe
Tengine
二、Apache的安装以及启用
dnf install httpd.x86_64 -y ##安装httpd
firewall-cmd --permanent --add-service=http ##将http服务加到防火墙的服务名单中
firewall-cmd --permanent --add-service=https ##将https服务加到防火墙的服务名单中
firewall-cmd --reload ##重启防火墙,让刚才的操作生效
systemctl enable --now httpd ##启动httpd服务
vim /var/www/html/index.html ##编辑文件
三、Apache的基本配置
配置文件:/etc/httpd/conf/httpd.conf
端口设置:
Listen:8080
默认发布文件:
vim /var/www/html/westos.html##编辑文件
168行左右
<IfModule dir_module>
DirectoryIndex westos.html index.html
firefox查询:172.25.254.137
默认发布目录:
mkdir /westos_apache
vim /westos_apache/index.html
semanage fcontext -a -t httpd_sys_content_t '/westos_apache(/.*)?'
vim /etc/httpd/conf/httpd.conf :
123行左右
#DocumentRoot "/var/www/html"
DocumentRoot "/westos_apache"
<Directory "/westos_apache">
Require all granted
</Directory>
firefox查询:172.25.254.137
四、Apache访问控制
1)ip限制:
vim /etc/httpd/conf/httpd.conf :
DocumentRoot "/var/www/html"
#DocumentRoot "/westos_apache"
<Directory "/var/www/html/westos">
Order Deny,Allow
Deny from all
Allow from 172.25.254.137
</Directory>
systemctl restart httpd
2)用户限制:
cd /etc/httpd/
mkdir westos ##建立查看文件
vim /westos/index.html
htpasswd -cm .htpasswd admin ##建立用户(环境)
vim /etc/httpd/conf/httpd.conf:
<Directory "/var/www/html/westos">
AuthUserFile (用户建立环境在那就指向那里)
AuthName "Please input username and password"
AuthType basic
# Require user admin
Require valid-user
systemctl restart httpd
五、Apache的虚拟主机
建立域名、相关文件
mkdir -p /var/www/vhost/westos.org/{news,music,yifan.org}
echo news.westos.org> /var/www/vhost/westos.org/news/index.html
echo music.westos.org> /var/www/vhost/westos.org/music/index.html
echo yifan.westos.org> /var/www/vhost/westos.org/yifan/index.html
echo yifan.org.westos.org> /var/www/vhost/westos.org/yifan.org/index.html
编辑配置文件(服务器)
<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 yifan.org.westos.org
DocumentRoot /var/www/vhost/westos.org/'yifan'
CustomLog logs/yifan.org.log combined
</VirtualHost>
手动解析域名(浏览器在哪里就在那里写)
vim /etc/hosts:
172.25.254.137 www.westos.org music.westos.org news.westos.org yifan.org.westos.org
测试:
firefox:www.westos.org | music.westos.org | news.westos.org | yifan.org.westos.org(任意一个)
六、Apache的语言支持
#php#
vim /var/www/html/index.php
<?php
phpinfo();
?>
dnf install php -y
systemctl restart httpd
firefox http://192.168.0.11/index.php
#cgi#
mkdir /var/www/html/cgidir
vim /var/www/html/cgidir/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
systemctl restart httpd
vim /etc/httpd/conf.d/vhost.conf
<Directory "/var/www/html/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
firefox http://192.168.0.11/cgidir/index.cgi
#wsgi#
书写wsgi的测试文件
vim /var/www/html/wsgi/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 httpd
vim /etc/httpd/conf.d/vhost
<VirtualHost *:80>
ServerName wsgi.westos.org
WSGIScriptAlias / /var/www/html/wsgi/index.wsgi
</VirtualHost>
七、Apache的加密访问
mkdir /etc/httpd/tls ##建立目录用来存放锁和钥匙
openssl req --newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/tls/westos.org.key -x509 -days 365 -out /etc/httpd/tls/westos.org.crt ##建立锁和钥匙
vim /etc/httpd/conf.d/vhost.conf