apache的基本信息
apache为同步阻塞模式,稳定性高,nginx为异步非阻塞模式
apache为企业中常用的web服务,用来提供http://(超文本传输协议)
主配置目录:/etc/httpd/conf
主配置文件:/etc/httpd/conf/http.conf
子配置目录:/etc/httpd/conf.d
子配置文件:/etc/httpd/conf.d/*.conf
默认发布目录:/var/www/html/
默认发布文件:index.html
默认端口:80
1.安装部署apache
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# netstat -antlupe | grep httpd
tcp6 0 0 :::80 :::* LISTEN 0 36458 1714/httpd
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.html
hello
浏览器测试:172.25.254.152
yum install httpd -y
测试:
2.更改默认发布目录
[root@localhost html]# mkdir /linux/html/ -p
[root@localhost html]# cd /linux/html/
[root@localhost html]# vim index.html
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
[root@localhost html]# systemctl restart httpd
#DocumentRoot "/var/www/html" #注释掉原来的默认发布目录
DocumentRoot "/linux/html" #写入新的发布目录
<Directory "/linux">
require all granted
</Directory>
浏览器测试:172.25.254.152
vim /etc/httpd/conf/httpd.conf 内容:
测试:
3.更改默认发布文件
1)
[root@localhost html]# vim test.html
test
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/linux/html"
<Directory "/linux">
require all granted
DirectoryIndex test.html #新的发布文件
</Directory>
浏览器测试:172.25.254.152
vim /etc/httpd/conf/httpd.conf 内容:
测试:
2)
[root@localhost html]# mkdir lin
[root@localhost html]# mv index.html lin/
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
<Directory "/linux/html/lin">
DirectoryIndex index.html
</Directory>
[root@localhost html]# systemctl restart httpd
浏览器测试:172.25.254.152/lin
vim /etc/httpd/conf/httpd.conf 内容:
测试:
4.更改默认端口
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
listen 8080 #默认端口改为8080
[root@localhost html]# systemctl restart httpd
[root@localhost html]# netstat -antlupe | grep httpd
访问测试:172.25.254.152:8080
/etc/httpd/conf/httpd.conf内容:
测试:
5.主机访问权限设置
[root@localhost html]# vim index.html
[root@localhost html]# mv index.html lin/
[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/lin">
order Allow,Deny #顺序为先读allow,再读deny
Allow from All
Deny from 172.25.254.155 #不允许172.25.254.155访问
</Directory>
[root@localhost html]# systemctl restart httpd
访问:172.25.254.152
vim /etc/httpd/conf/httpd.conf 内容:
测试:
6.用户访问权限设置
[root@localhost lin]# cd /etc/httpd/
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# htpasswd -cm apacheuser admin
New password:
Re-type new password:
Adding password for user admin
[root@localhost httpd]# htpasswd -m apacheuser tom
New password:
Re-type new password:
Adding password for user tom
[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/lin">
AuthUserFile /etc/httpd/apacheuser
AuthName "Please input user and password!!"
AuthType basic
# Require user admin 允许admin
Require valid-user 允许所有用户
</Directory>
访问测试172.25.254.152
vim /etc/httpd/conf/httpd.conf 内容:
访问测试172.25.254.152
7.多节点测试(apache的虚拟主机)
真实主机:
[root@foundation40 ~]# vim /etc/hosts
172.25.254.152 www.linux.com news.linux.com music.linux.com login.linux.com #本地解析
虚拟机:
[root@localhost httpd]# cd conf.d/
[root@localhost conf.d]# vim default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog "logs/default.log" combined
</VirtualHost>
[root@localhost conf.d]# mkdir /var/www/virtual/linux.com/news -p #建立news的发布目录
[root@localhost conf.d]# mkdir /var/www/virtual/linux.com/music -p #建立music的发布目录
[root@localhost conf.d]# vim /var/www/virtual/linux.com/news/index.html
news
[root@localhost conf.d]# vim /var/www/virtual/linux.com/music/index.html
music
[root@localhost conf.d]# vim news.conf
<VirtualHost *:80>
ServerName news.linux.com
DocumentRoot "/var/www/virtual/linux.com/news"
CustomLog "logs/default.log" combined
</VirtualHost>
<Directory "/var/www/virtual/westos.com/news/">
Require all granted
</Directory>
[root@localhost conf.d]# vim music.conf
<VirtualHost *:80>
ServerName music.linux.com
DocumentRoot "/var/www/virtual/linux.com/music"
CustomLog "logs/default.log" combined
</VirtualHost>
<Directory "/var/www/virtual/westos.com/music/">
Require all granted
</Directory>
[root@localhost conf.d]# systemctl restart httpd
访问测试music.linux.com news.linux.com
真机里:vim /etc/hosts #解析
虚拟机里:
/etc/httpd/conf.d/default.conf内容
vim /var/www/virtual/linux.com/news/index.html内容:
vim /var/www/virtual/linux.com/music/index.html内容:
/etc/httpd/conf.d/news.conf
/etc/httpd/conf.d/music.conf
测试:
8.证书生成
[root@localhost conf.d]# yum install mod_ssl -y
[root@localhost conf.d]# systemctl restart httpd
[root@localhost conf.d]# yum install crypto-utils.x86_64
[root@localhost conf.d]# genkey www.linux.com
[root@localhost conf.d]# vim ssl.conf
SSLCertificateFile /etc/pki/tls/certs/www.linux.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.linux.com.key
访问:https://www.linux.com
点next
选择大小
虚拟机里敲键盘加速
点no
next
填好信息,点next
文件已经生成
/etc/httpd/conf.d/ssl.conf 注释掉原来的,将两个文件写进去
测试:点add.exception获取证书
点get certificate ,再点confirm sercurity exception
9.网页重写:
[root@localhost conf.d]# mkdir /var/www/virtual/linux.com/login -p
[root@localhost conf.d]# vim /var/www/virtual/linux.com/login/index.html
login
[root@localhost conf.d]# vim login.conf
<VirtualHost *:443>
ServerName login.linux.com
DocumentRoot "/var/www/virtual/linux.com/login"
CustomLog "logs/default.log" combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.linux.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.linux.com.key
</VirtualHost>
<Directory "/var/www/virtual/westos.com/login/">
Require all granted
</Directory>
<VirtualHost *:80>
ServerName login.linux.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301] #访问80端口时自动转为https
</VirtualHost>
[root@localhost conf.d]# systemctl restart httpd
访问:login.linux.com
/var/www/virtual/linux.com/login/index.html内容:
/etc/httpd/conf.d/login/conf内容:
测试:重复上面生成证书测试时的步骤
10.apache支持的语言
1)php
[root@localhost conf.d]# cd /var/www/html/
[root@localhost html]# vim index.php
<?php
phpinfo();
?>
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
177 DirectoryIndex index.php index.html
[root@localhost html]# systemctl restart httpd
[root@localhost html]# yum install php -y
[root@localhost html]# systemctl restart httpd
访问172.25.254.152
/var/www/html/index.php内容
/etc/httpd/conf/httpd.conf 内容
测试:
2)cgi
[root@localhost html]# mkdir cgi
[root@localhost html]# vim cgi/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[root@localhost html]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# chmod +x /var/www/html/cgi/index.cgi
[root@localhost conf.d]# vim default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog "logs/default.log" combined
</VirtualHost>
<Directory "/var/www/html/cgi">
Options +ExecCGI
AddHandler cgi-script .cgi
DirectoryIndex index.cgi
</Directory>
[root@localhost conf.d]# systemctl restart httpd
访问:172.25.254.152/cgi
/var/www/html/cgi/index.cgi内容
/etc/httpd/conf.d/default.conf内容
测试:
11.squid正向代理
在可以上网的主机
[root@foundation155 ~]# yum install squid -y
[root@foundation155 ~]# vim /etc/squid/squid.conf
http_access allow all
cache_dir ufs /var/spool/squid 100 16 256
[root@foundation155 ~]# systemctl restart squid.service
在不能上网的主机设置,测试
在可以上网的主机:
/etc/squid/squid.conf内容
在不能上网的主机:测试—>不能上网
点edit 选择preferences
选择advanced—>network—>settings
刷新:成功上网
12.squid反向代理
在apache主机上安装部署apache
在squid主机
[root@lin ~]# yum install squid.x86_64
[root@lin ~]# vim /etc/squid/squid.conf
http_access allow all
http_port 80 vhost vport
cache_peer 172.25.254.152 parent 80 0 proxy-only
cache_dir ufs /var/spool/squid 100 16 256
[root@lin ~]# systemctl restart squid
第三台主机访问:172.25.254.252
squid主机
/etc/squid/squid.conf内容:
第三台主机测试:squid主机的ip,显示apache主机的内容