[root@apache1 ~]# yum install httpd -y
[root@apache1 ~]# cd /var/www/html/ 进入默认发布目录
[root@apache1 html]# ls
[root@apache1 html]# vim index.html 编辑默认读取的页面
[root@apache1 html]# systemctl start httpd
###################################################
修改默认发布目录
[root@apache1 html]# vim /etc/httpd/conf/httpd.conf 编辑配置文件
#DocumentRoot "/var/www/html" 注释此行(119左右)
DocumentRoot "/var/www/test" 自己写入要更改的发布目录
Require all granted
DirectoryIndex test.html index.html 修改默认读取的文件
[root@apache1 html]# mkdir /var/www/test 创建自己设置的发布目录(有时需注意权限)
[root@apache1 html]# vim /var/www/test/test.html 编辑自己默认读取的文件(ex:test)
[root@apache1 html]# systemctl restart httpd.service 重启服务,读取文件
页面测试:
#######################################################
黑白目录(名单的设置):
[root@apache1 html]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/test"
Order Allow,Deny ##黑白名单的读取顺序
Allow from ALL ##白名单Allow,允许所有人访问
Deny from 192.168.52.0/24 ##黑名单Deny,拒绝192.168.52.0这个网段的人访问
[root@apache1 html]# systemctl restart httpd.service
测试:
访问失败
此时我们再来修改一下配置文件:
DocumentRoot "/var/www/test"
Order Deny,Allow ##将黑白名单的读取顺序改变一下先Deny,后Allow
Allow from ALL
Deny from 192.168.52.0/24
[root@apache1 html]# systemctl restart httpd.service ##重新服务,读取文件
再次访问时会发现又可以访问通了。由此可得出一个结论:
在黑白名单中,后读取的规则会覆盖先读取的规则。
##################################################
指定用户访问发布目录:
[root@apache1 html]# cd /etc/httpd/
[root@apache1 httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@apache1 httpd]# htpasswd -cm apacheuser admin 建立用户并生成认证文件
New password:
Re-type new password:
Adding password for user admin
[root@apache1 httpd]# htpasswd -m apacheuser bss
New password:
Re-type new password:
Adding password for user bss
[root@apache1 httpd]# cat apacheuser 查看用户信息
admin:$apr1$U.IY9J0s$SWnZa5LQlH59vFF3gVfsz/
bss:$apr1$cKBxKH39$8maYvvh5aDGV.8/iQRrSA1
编辑配置文件
[root@apache1 httpd]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/test"
AuthUserFile /etc/httpd/apacheuser 读取的认证文件
AuthName "Please input user and password!!" 访问页面
AuthType basic 基本的认证方式
Require user admin 只允许admin用户访问
[root@apache1 httpd]# systemctl restart httpd.service
访问
[root@apache1 httpd]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/test"
AuthUserFile /etc/httpd/apacheuser
AuthName "Please input user and password!!"
AuthType basic
Require valid-user ##此时允许所有用户登陆
[root@apache1 httpd]# systemctl restart httpd.service
这样所有的用户都可以登陆
############################################
虚拟主机的创建:
[root@apache1 httpd]# cd /etc/httpd/conf.d/
[root@apache1 conf.d]# vim default.conf
DocumentRoot /var/www/html
CustomLog "logs/default.log" combined
[root@apache1 conf.d]# cp default.conf new.conf
[root@apache1 conf.d]# vim new.conf
ServerName new.bss.com
DocumentRoot "/var/www/html/new"
CustomLog "logs/new.log" combined
Require all granted
[root@apache1 conf.d]# cp new.conf lol.conf
[root@apache1 conf.d]# vim lol.conf
ServerName lol.bss.com
DocumentRoot "/var/www/html/lol"
CustomLog "logs/lol.log" combined
Require all granted
创建虚拟目录,需要与文件中写的一致
[root@apache1 conf.d]# mkdir /var/www/html/new
[root@apache1 conf.d]# mkdir /var/www/html/lol
编写发布页面
[root@apache1 conf.d]# echo "new.txt" > /var/www/html/new/index.html
[root@apache1 conf.d]# echo "lol.txt" > /var/www/html/lol/index.html
添加本地解析(需要用哪台测,就写在哪台机子上)
[root@apache1 conf.d]# vim /etc/hosts
192.168.52.147 lol.bss.com new.bss.com www.bss.com
测试:
###################################################
Apache: php与cgi
Php的支持:
[root@apache1 conf.d]# yum install php -y
[root@apache1 conf.d]# vim /var/www/html/index.php ##写入php测试页面
phpinfo();
?>
编辑apache配置文件,在默认访问页面的模块加入php页面
DirectoryIndex index.php index.html
[root@apache1 conf.d]# systemctl restart httpd.service
测试:
Cgi的支持:
[root@apache1 conf.d]# mkdir /var/www/html/cgi
[root@apache1 conf.d]# vim /var/www/html/cgi/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`
[root@apache1 conf.d]# chmod +x /var/www/html/cgi/index.cgi ##给予cgi可执行权限
[root@apache1 conf.d]# /var/www/html/cgi/index.cgi ##执行cgi脚本
Content-type: text/html
Wed Mar 13 21:30:25 CST 2019
但是此时页面访问只会显示脚本文件内容,不会执行。
此时我们编辑apache配置文件,
[root@apache1 conf.d]# vim /etc/httpd/conf/httpd.conf
Options +ExecCGI
ADDHandler cgi-script .cgi
[root@apache1 conf.d]# systemctl restart httpd.service
测试:
########################################################
https加密认证
[root@apache1 conf.d]# yum install mod_ssl.x86_64 crypto-utils.x86_64 -y
[root@apache1 ~]# genkey www.bss.com
[root@apache1 conf.d]# vim /etc/httpd/conf.d/ssl.conf ##根据图中所示路径修改下面两行
SSLCertificateFile /etc/pki/tls/certs/www.bss.com.crt ##100行
SSLCertificateKeyFile /etc/pki/tls/private/www.bss.com.key ##107行
[root@apache1 conf.d]# systemctl restart httpd ##重启服务
地址转换:
Vim /etc/hosts
192.168.52.147 lls.bss.com
[root@apache1 ~]# vim /etc/httpd/conf.d/bss.conf
ServerName lls.bss.com
DocumentRoot "/var/www/lls"
CustomLog "logs/lls.log" combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.bss.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.bss.com.key
ServerName lls.bss.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
[root@apache1 ~]# mkdir /var/www/lls
[root@apache1 ~]# echo "GOD" > /var/www/lls/index.html
[root@apache1 ~]# systemctl restart httpd
###############################################
代理服务器:
可上网的机器
[root@apache1 ~]# yum install squid.x86_64 -y
[root@apache1 ~]# vim /etc/squid/squid.conf
http_access allow all ##第56行,允许所有人通过
http_port 3128 ##59行
cache_dir ufs /var/spool/squid 100 16 256 ##62行,
[root@apache1 ~]# systemctl restart squid
在不能上网的机器上:
Edit > Preferences > Advanced > Network > Settings > Manual proxy configuration > HTTP Proxy