一、基础知识
- Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。
- apache #使用perfork时间处理器,同步阻塞模型。稳定,可处理动态页面。例:php界面
- nginx #web服务器,使用fork模型时间处理器,异步非阻塞。速度快,不稳定,不能处理动态模型。做反向代理,
静态资源处理器。(反向代理相当于10086转接人工服务)例:qq
查询:
curl -I www.douyu.com
curl -I www.sina.com
lamp架构=linux apache mysql php
lnmp架构=linux nginx mysql php
apache 企业中常用的web服务器,用来提供http协议访问 http:// (超文本传输协议)
二、apache的安装部署
本次实验在我的虚拟机server上进行
ip为172.25.254.176
【1】
关闭selinux
getenforce
vim /etc/sysconfig/selinux
SELINUX=disabled
reboot
getenforce
【2】
yum install httpd -y
yum install httpd-manual -y #apache手册
systemctl start httpd
systemctl enable httpd
ab #提供压力测试
【3】
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
firewall-cmd --list-all
【4】
/var/www/html #apache的根目录,默认发布目录
/var/www/html/index.html #apache的默认发布文件
修改:
vim /var/www/html/index.html
hello world!
systemctl restart httpd
测试:
浏览器输入:
http://172.25.254.119
http://172.25.254.119/manual
三、apache服务配置
1、查看
rpm -qa | grep http #查看安装包
rpm -qc httpd-2.4.6-17.el7.x86_64 #查看相应的配置文件
rpm -ql httpd-2.4.6-17.el7.x86_64 #安装httpd时都装了哪些文件
2、apache的基础信息
主配置目录 | /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 |
apache日志 | /etc/httpd/logs/* |
测试:
【1】
vim /etc/httpd/conf/httpd.conf #存放配置信息
【2】
id apache #查看用户id信息,安装apache服务,生成apache用户
【3】
ps aux
root #顶级进程,监控apache进程,不处理业务
apache #处理用户请求
3、日志存放文件
/etc/httpd/logs 存放产生的日志
access_log #哪个客户端访问过我,使用的浏览服务器等
error_log #记录错误日志
ll -h 查看文件大小
定期做日志截断,否则文件太大,没保存之前都在内存中保存,保存不下
3、修改默认端口
默认访问端口 80
vim /etc/httpd/conf/httpd.conf
42 Listen 8080 #修改默认端口为8080
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
测试:
netstat -tnlp | grep http
http://172.25.254.19:8080
4、修改默认发布文件
默认发布文件就是访问apache时没有指定文件名时默认访问的文件
这个文件可以指定多个,有访问顺序
vim /etc/httpd/conf/httpd.conf
164 DirectoryIndex westos index.html #当westos不存在时访问index.html
systemctl restart httpd
vim /var/www/html/westos
westos test
测试:
【1】
恢复默认端口
vim /etc/httpd/conf/httpd.conf
43 Listen 80
【2】
firefox搜索:
http://172.25.254.176
5、修改默认发布目录
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot"/westos/html"
121 <Directory "/westos">
122 Require all granted
123 </Directory>
【2】
mkdir /westos/html/ -p
vim /westos/html/index.html
<h1>SEE ME</h1>
systemctl restart httpd
测试:
【1】恢复默认发布文件
vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.html
systemctl restart httpd
【2】
firefox搜索:
http://172.25.254.176
6、访问控制:
vim /var/www/html/index.html
<h1>172.25.254.176</h1>
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
<h1>test page</h1>
vim /etc/httpd/conf/httpd.conf
120 <Directory "/var/www/html/westos">
121 Order Allow,Deny
122 Allow from All
123 Deny from 172.25.254.76
164 DirectoryIndex westos index.html
systemctl restart httpd
注意:Allow,Deny #后者会覆盖前者,设置需注意前后顺序
测试:
【1】
在172.25.254.76客户端主机上
搜索http://172.25.254.176
【2】
在172.25.254.176配置主机上
搜索http://172.25.254.176
7、指定用户访问
【1】
cd /etc/httpd/conf
htpasswd -cm westosuser admin1 #创建密码认证文件westosuser,创建登陆用户admin
cat westosuser
htpasswd -m westosuser admin2 第二次创建不加-c,否则会覆盖之前创建的用户
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos">
AuthUserFile /etc/httpd/conf/westosuser
AuthType basic
AuthName "Please input your name and password!!!"
Require user admin1 #只允许admin1登陆访问,user指定单用户
#Require valid-user #文件中存在的用户都可以登陆
</Directory>
systemctl restart httpd
注意:一次登陆后,下次登陆则不再需要输入用户和密码,所以每次登陆完需要清除记录 ctrl+shift+Delete
测试:
【1】
注释掉访问控制权限
【2】
搜索http://172.25.254.176
未允许用户admin2登陆,登陆不进去:
被允许的用户admin1登陆:
8、一个物理机开多个虚拟机实现在apache上实现多个资源访问
【1】
修改本地域名解析文件
vim /etc/hosts
172.25.254.176 www.westos.com news.westos.com music.westos.com
【3】
mkdir /var/www/virtual/news/html -p
mkdir /var/www/virtual/music/html -p
vim /var/www/virtual/news/html/index.html
<h1>news test</h1>
vim /var/www/virtual/music/html/index.html
<h1>news music</h1>
【3】
未单独指定的本机域名访问文件及日志保存
vim /etc/httpd/conf.d/default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog logs/default.log combined #所有日志放到一起
</VirtualHost>
【5】
使用域名news.westos.com查询时相应文件及权限
vim /etc/httpd/conf.d/news.conf
<VirtualHost *:80>
ServerName news.westos.com
DocumentRoot /var/www/virtual/news/html
CustomLog logs/default.log combined #所有日志放到一起
</VirtualHost>
<Directory "/var/www/virtual/news/html">
Require all granted
</Directory>
[6]
使用域名music.westos.com查询时相应文件及权限
vim /etc/httpd/conf.d/music.conf
<VirtualHost *:80>
ServerName music.westos.com
DocumentRoot /var/www/virtual/music/html
CustomLog logs/default.log combined #所有日志放到一起
</VirtualHost>
<Directory "/var/www/virtual/music/html">
Require all granted
</Directory>
测试:
http://news.westos.com
http://music.westos.com
四、CGI、PHP、WSGI相关知识与应用
【1】cgi 单线程 使用prel语言,动态脚本语言
公共网关接口(Common Gateway Interface,CGI)是Web 服务器运行时外部程序的规范,按CGI 编写的程序可以扩展服务器功能。CGI 应用程序能与浏览器进行交互,还可通过数据API与数据库服务器等外部数据源进行通信,从数据库服务器中获取数据。格式化为HTML文档后,发送给浏览器,也可以将从浏览器获得的数据放到数据库中。几乎所有服务器都支持CGI,可用任何语言编写CGI,包括流行的C、C ++、VB 和Delphi 等。
【2】 PHP 即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常
用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。主要适用于Web开发领域。
Apache默认不支持php,写的index.php文件查看不到,需要安装php插件
【3】Wsgi 支持多线程,多个业务同时在线执行,使用phython语言
WSGI是Web Server Gateway Interface的缩写。以层的角度来看,WSGI所在层的位置低于CGI。但与CGI不同的是WSGI具有很强的伸缩性且能运行于多线程或多进程的环境下,这是因为WSGI只是一份标准并没有定义如何去实现。
1、php+cgi动态数据交互
【1】
写php脚本,但不能执行,在firefox中搜索到空白页面
vim /var/www/html/index.php
<?php
phpinfo()
?>
http://172.25.254.176/index.php
【2】
下载php
yum install -y php
systemctl restart httpd
cd /etc/httpd/conf.d/ #生成php.conf
此时搜索http://172.25.254.176/index.php
【3】
mkdir /var/www/html/cgi
touch /var/www/html/cgi/index.cgi
vim /var/www/html/cgi/index.cgi
#! /vsr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
chmod 775 index.cgi
systemctl restart httpd
./index.cgi #执行脚本
在帮助手册manual中查看标准cgi语言编写
vim /var/www/html/cgi/index.cgi
【4】
vim /etc/httpd/conf.d/default.conf
<VirtualHost_default_:80>
DocumentRoot /var/www/html
CustomLog logs/default.log combined #所有日志放到一起
</VirtualHost>
<Directory "/var/www/html/cgi">
Options +ExecCGI
AddHandler cgi-scripts .cgi
</Directory>
systemctl restart httpd
搜索到执行后的脚本
刷新,时间会变,动态
2、apache的ssl认证
netstat -tulp | grep :443
yum install mod_ssl.x86_64 -y
systemctl restart httpd
https://172.25.254.119
查看443端口是否开启:
查找ssl加密安装包:
安装完成:
生成证书:
查看证书
https://172.25.254.119—>锁—>More Information—>Security—>General
删除证书
Edit—>Preferences—>Advanced—>View Certificates—>Server—>选中自己的证书—>Delete—>ctrl+shift+Delete
3、自签名证书(生成自己的认证证书)
【1】
yum install -y crypto-utils
【2】
genkey www.westos.com #加密站点
此时安装慢。可通过打开一个新的shell,敲击键盘,加速:
生成密钥文件及
【3】
vim /etc/httpd/conf.d/ssl.conf
101 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt #证书
108 SSLCertificateFile /etc/pki/tls/private/www.westos.com.key #密钥
【4】
systemctl restart httpd
测试:
在网站上生成自己的证书:
https://172.25.254.100 #登陆查看
I Understand the Risks—>ADD Exception—>Get Certificate—>Confirm
查看证书
https://172.25.254.119—>锁—>More Information—>Security—>General
4、网页重写
强制重定向,将访问端口强制从80到443
访问http自动转换为https
例:
curl -I http://www.westos.com #未开启强制重定向,直接访问的为其主服务器
【1】
cp /etc/httpd/conf.d/news.conf /etc/httpd/conf.d/lodin.conf
vim /etc/httpd/conf.d/login.conf
vim /etc/httpd/conf.d/default.conf
<VirtualHost *:443>
ServerName login.westos.com
DocumentRoot /var/www/virtual/login/html
CustomLog logs/login.log combined #所有日志放到一起
SSLEngine On
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
</VirtualHost>
<Directory "/var/www/html/login/html">
Require all granted
</Directory>
<VirtualHost *:80>
ServerName login.westos.com
RewriteEngine On
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>
【2】
mkdir -p /var/virtual/login/html
vim /var/www/virtual/login/html/index.html
<h1>login page</h1>
【3】
vim /etc/hosts
172.25.254.119 login.westos.com
【4】
systemctl restart httpd
测试:
【1】
curl -I http://login.westos.com
【2】
浏览器搜索http://login.westos.com
加载证书后:
5、wsgi的部署
【1】
mkdir /var/www/html/wsgi
cd /var/www/html/wsgi
【2】
下载一个wsgi脚本,在浏览器中搜索250共享的脚本,复制地址
wget http://172.25.254.250/RHCEPACKAGES/materials/script.wsgi
内容:
【3】
vim /etc/httpd/conf.d/Virtmsgi.conf
<VirtualHost *:80>
ServerName www.wsgi.com
WSGIScriptAlias / /var/www/html/wsgi/scripts.wsgi
</VirtualHost>
# 访问这个域名是就是访问其根目录,定义到此文件
【4】
vim /etc/hosts
172.25.254.119 www.wsgi.com
【5】
yum install -y mod_wsgi
systemctl restart httpd
时间刷新会变,动态: