Apache的管理及优化


前言

Apache用于提供超文本传输协议,本质上是一个软件。超文本传输协议:http://,提供该协议的软件一般有:Apache、nginx、stgw、jfe、Tengine,而Apache在网页应用中使用比较广泛,比如百度所使用的就是该软件。


一、Apache的安装及启用

在 web 被访问时通常使用 http:// 的方式 , http://为 超文本传输协议
http:// 超文本传输协议 提供软件 :
Apache
nginx
stgw
jfe
Tengine

dnf install httpd.x86_64 -y     %dnf安装
systemctl enable --now httpd    %开启服务并设定服务位开机启动
firewall-cmd --list-all			%查看火墙信息
firewall-cmd --permanent --add-service=http	%在火墙中永久开启http访问
firewall-cmd --reload			%刷新火墙使设定生效

请添加图片描述请添加图片描述

二、Apache的基本配置

1、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

2、Apache端口修改

vim /etc/httpd/conf/httpd.conf    %编辑主配置文件
Listen xxxx
firewall-cmd --permanent --add-port=xxxx/tcp  %防火墙设置允许xxxx端口
firewall-cmd --reload             %防火墙信息重新加载
systemctl restart httpd           %重启httpd服务

请添加图片描述
请添加图片描述此时httpd服务的端口为8080,在网页上默认的80端口访问不了,使用8080端口可以访问:

请添加图片描述

请添加图片描述
请添加图片描述

3、默认发布文件

vim /etc/httpd/conf/httpd.conf    %编辑主配置文件
DirectoryIndex westos.html index.html  %添加默认发布文件westos.html
systemctl restart httpd           %重启服务

请添加图片描述
请添加图片描述
请添加图片描述

4、默认发布目录

mkdir /dirname                    %创建目录,在主配置文件中添加下述内容
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/dirname"
<Directory "/dirname">
        Require all granted
</Directory>
systemctl restart httpd 

请添加图片描述

请添加图片描述
可以看到现实的内容已经变成了/var/www/westos中的文件的内容:
请添加图片描述

三、Apache的访问控制

1、基于客户端ip的访问控制

mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
在浏览器中访问 http://ip/westos,可以访问到index.html中编写的内容1)ip白名单       %注意:白名单时,先Deny后Allow才可以,因为顺序颠倒的话,Deny会把Allow给覆盖掉
<Directory "/var/www/html/westos">
        Order Deny,Allow
        Allow from ip		
        Deny from All
</Directory>2)ip黑名单       %注意:黑名单时,先Allow后Deny才可以,因为顺序颠倒的话,Allow会把Deny给覆盖掉  
<Directory "/var/www/html/westos">
        Order Allow,Deny
        Allow from All		
        Deny from ip
</Directory>

白名单:
请添加图片描述
请添加图片描述
可以访问:
请添加图片描述黑名单:
请添加图片描述
此时无法访问其中的内容:
请添加图片描述

2、基于用户认证的访问控制

[root@westosa100 html]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos">
        AuthUserfile /etc/httpd/htpasswdfile    ##指定认证文件
        AuthName "Please input your name and password"    ##认证提示语
        AuthType basic    ##认证类型
        Require user lrt    ##允许通过的认证用户 (此命令与下一行命令21)
 	    Require valid-user    ##允许所有用户通过认证 (此命令与上一行命令21</Directory>

[root@westosa100 httpd]# htpasswd -cm /etc/httpd/htpasswdfile lrt    ##生成认证文件
New password: 
Re-type new password: 
Adding password for user lrt

注意: 当/etc/httpd/htpasswdfile存在那么在添加用户时不要加-c参数否则会覆盖源文件内容

[root@westosa100 httpd]# systemctl restart httpd

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

四、Apache的虚拟主机

在实际情况中,不可能每访问一个网页就需要一台主机来安装Apache并部署相关环境,虚拟主机实现了同一个ip下有多个站点,即可以通过一个ip来访问多个网页内容。

1)虚拟主机建立
[root@haha Desktop]# mkdir -p /var/www/virutal/westos.org/{linux,lee}   %创建虚拟主机的默认发布目录}
[root@haha Desktop]# vim /var/www/virutal/westos.org/linux/index.html  %编写虚拟机内容的默认发布页中的内容
[root@haha Desktop]# vim /var/www/virutal/westos.org/lee/index.html
[root@haha Desktop]# vim /etc/httpd/conf/httpd.conf  %注释掉下面几行(之前设置的,如果没有则可以忽略这一步)
 #DocumentRoot "/westos_web"
 #<Directory "/var/www/html/westos">
 #  Order Deny,Allow
 #  Allow from 172.25.254.1
 #  Deny from All
 #</Directory>
[root@haha conf.d]# cat /var/www/virutal/westos.org/linux/index.html
linux.westos.org
[root@haha conf.d]# cat /var/www/virutal/westos.org/lee/index.html
lee.westos.org
[root@haha conf.d]# vim Vhost.conf   %编写该文件内容如下
<VirtualHost _default_:80>
      DocumentRoot /var/www/html
      CustomLog logs/default.log combined
</VirtualHost>

<VirtualHost *:80>
      ServerName lee.westos.org
      DocumentRoot /var/www/virutal/westos.org/lee
      CustomLog logs/lee.log combined
</VirtualHost>

<VirtualHost *:80>
      ServerName linux.westos.org
      DocumentRoot /var/www/virutal/westos.org/linux
      CustomLog logs/linux.log combined
</VirtualHost>
[root@haha conf.d]# systemctl restart httpd.service
(2)在浏览器所在主机添加本地解析
[root@westos_student1 ~]# vim /etc/hosts   %在另外一台主机里添加解析如下
ip www.westos.org linux.westos.org lee.westos.org login.westos.org
(3)测试:在浏览器中访问以下内容:
firefox http://www.westos.org
firefox http://lee.westos.org
firefox http://linux.westos.org

测试:在测试主机先写入以下命令(配置域名解析,ip地址与域名的映射) ,之后在测试主机浏览器进行测试

[root@westos_student70 html]# vim /etc/hosts
172.25.254.200 www.westos.org lee.westos.org linux.westos.org ####前面是ip地址,后面是域名

请添加图片描述

请添加图片描述
请添加图片描述测试:

请添加图片描述

请添加图片描述
请添加图片描述

五、Apache的语言支持

1、php语言

httpd服务默认发布的index.html文件,即超文本标记语言,是Apache默认支持的语言;而当我们在虚拟机westosa共享位置 /var/www/html目录下编写一个index.php文件(文件内容为展示php测试页面)
在浏览器所在真实主机访问该index.php文件时,页面无显示,即index.php文件中的代码不执行,这表明Apache不支持php语言;
在服务器中安装php软件后,在浏览器所在真实主机中再次访问该index.php文件,出现了php测试页面,index.php文件中的代码成功执行,此时Apache支持php语言

命令:

1)php
vim /var/www/html/index.php
<?php
	phpinfo();
?>
dnf install php -y
systemctl restart httpd 
firefox http://ip/index.php2)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`;
chmod +x index.cgi
vim /etc/httpd/conf.d/vhost.conf
<Directory "/var/www/html/cgidir">
	Options +ExecCGI
	AddHandler cgi-script .cgi
</Directory>
systemctl restart httpd 
firefox http://ip/cgidir/index.cgi3)wsgi
mkdir /var/www/html/wsgidir
vim /var/www/html/wsgidir/index.wsgi
def application (env, westos): 
    westos('200 ok', [('Content-Type', 'text/html')])
    return [b'hello wsgi!']
chmod +x index.wsgi
dnf search wsgi
dnf install python3-mod_wsgi.x86_64 -y 
vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
       Servername wsgi.westos.org
       wSGIScriptAlias / /var/www/html/wsgidir/index.wsgi
</VirtualHost>
systemctl restart httpd 
vim /etc/hosts
ip www.westos.org linux.westos.org lee.westos.org login.westos.org wsgi.westos.org
firefox  wsgi.westos.org/index.wsgi

请添加图片描述
请添加图片描述

设置成功!

请添加图片描述

2、perl语言

cgi通用网关接口中主要使用的是perl语言,我们在虚拟机westosa共享位置 /var/www/html目录下建立cgi目录,在该目录下编写index.cgi文件(文件内容为执行date命令显示系统当前时间),在浏览器所在真实主机访问该index.cgi文件时,页面直接显示的是该文件的文本内容,即index.cgi文件中的代码不执行,这表明Apache不支持perl语言

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

3、python语言

wsgi与cgi的功能类似,其主要使用的是python语言,我们在虚拟机westosa共享位置 /var/www/html目录下建立wsgi目录并书写wsgi的测试文件index.wsgi(文件内容为显示hello westos),在浏览器所在真实主机访问该index.wsgi文件时,页面不发布文件内容,直接提示下载该文件,即index.wsgi脚本不执行,这表明Apache不具备执行该脚本的能力

请添加图片描述
请添加图片描述
请添加图片描述在测试主机添加解析:

请添加图片描述

测试成功!
请添加图片描述

六、Apache的加密访问

网页上用户的个人信息传输如果不经过加密手段,安全性将会受到极大的威胁,比如说账户密码信息,此时就需要一种加密手段能够给用户信息加密,相当于给用户信息上锁,而这个锁必须是经过专门机构认证的锁,这个锁就相当于私钥,锁的认证信息相当于证书签名文件,生成证书需要私钥和证书签名文件,二者缺一不可。

操作Apache 的加密访问,在虚拟机里操作:

具体步骤:
[root@haha wsgi-scripts]# dnf install mod_ssl -y
[root@haha wsgi-scripts]# cd /etc/httpd/conf.d/
[root@haha conf.d]# ls
autoindex.conf  php.conf  ssl.conf      Vhost.conf
manual.conf     README    userdir.conf  welcome.conf
[root@haha conf.d]# firewall-cmd --permanent --add-service=https
success
[root@haha conf.d]# firewall-cmd --reload
success
[root@haha conf.d]# systemctl restart httpd
完成上述步骤后在浏览器中加https可以访问到界面

[root@haha conf.d]# openssl genrsa -out /mnt/www.westos.org.key 2048
[root@haha conf.d]# openssl req -new -key /mnt/www.westos.org.key -out /mnt/www.westos.org.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:ShanXi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:www.westos.org
Email Address []:admin@westos.org

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@haha conf.d]# ls /mnt/
www.westos.org.csr  www.westos.org.key
[root@haha conf.d]# openssl x509  -req -days 365 -in /mnt/www.westos.org.csr -signkey /mnt/www.westos.org.key -out /mnt/www.westos.org.crt
Signature ok
subject=C = CN, ST = ShanXi, L = xi'an, O = westos, OU = linux, CN = www.westos.org, emailAddress = admin@westos.org
Getting Private key
[root@haha conf.d]# ls
autoindex.conf  manual.conf  php.conf  README  ssl.conf  userdir.conf  Vhost.conf  welcome.conf
[root@haha conf.d]# ls /mnt/
www.westos.org.crt  www.westos.org.csr  www.westos.org.key
[root@haha conf.d]# cp /mnt/www.westos.org.key /etc/pki/tls/private/
[root@haha conf.d]# cp /mnt/www.westos.org.* /etc/httpd/
[root@haha conf.d]# cd /etc/httpd/
[root@haha httpd]# ls
conf    conf.modules.d  modules  state               www.westos.org.csr
conf.d  logs            run      www.westos.org.crt  www.westos.org.key
[root@haha httpd]# cd ..
[root@haha etc]# cd /etc/httpd/conf.d/
[root@haha conf.d]# ls
autoindex.conf  manual.conf  php.conf  README  ssl.conf  userdir.conf  Vhost.conf  welcome.conf
[root@haha conf.d]# vim ssl.conf 
 85 SSLCertificateFile /etc/httpd/www.westos.org.crt
 86 
 87 #   Server Private Key:
 88 #   If the key is not combined with the certificate, use this
 89 #   directive to point at the key file.  Keep in mind that if
 90 #   you've both a RSA and a DSA private key you can configure
 91 #   both in parallel (to also allow the use of DSA ciphers, etc.)
 92 #   ECC keys, when in use, can also be configured in parallel
 93 SSLCertificateKeyFile /etc/httpd/www.westos.org.key
[root@haha conf.d]# systemctl restart httpd
[root@haha conf.d]# mkdir /var/www/virutal/westos.org/login
[root@haha conf.d]# vim /var/www/virutal/westos.org/login/index.html    #里面的内容任意
[root@haha conf.d]# vim /etc/httpd/conf.d/Vhost.conf
 23 <VirtualHost *:443>
 24       SSLEngine on
 25       SSLCertificateFile /etc/httpd/www.westos.org.crt
 26       SSLCertificateKeyFile /etc/httpd/www.westos.org.key
 27       ServerName login.westos.org
 28       DocumentRoot /var/www/virutal/westos.org/login
 29       CustomLog logs/linux.log combined
 30 </VirtualHost>
 31 
 32 <VirtualHost *:80>
 33       ServerName login.westos.org
 34       RewriteEngine on
 35       RewriteRule ^(/*)$ https://%{HTTP_HOST}$1
 36 </VirtualHost>
[root@haha conf.d]# systemctl restart httpd
[root@haha conf.d]# vim /etc/httpd/conf.d/Vhost.conf 
[root@haha conf.d]# cd /var/www/html/
[root@haha html]# ls
cgidir  index.html  index.php  westos  westos.html  wsgi-scripts

安装加密插件:

请添加图片描述

请添加图片描述

请添加图片描述
请添加图片描述
请添加图片描述测试:

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
设置特定的网页生成证书(如登录界面,这种用户与网页会进行交互的页面为了安全需要加密访问),也就是将某些网页http转换为https:

########服务器配置
[root@westosa100 html]# cat /var/www/westos.com/login/index.html 
login
[root@westosa100 conf.d]# vim /etc/httpd/conf.d/vhost.conf 
<VirtualHost _default_:80>
        DocumentRoot "/var/www/html"
        CustomLog logs/default.log combined
</VirtualHost>

<VirtualHost *:80>
        ServerName login.westos.org
        RewriteEngine on
        RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</VirtualHost>

<VirtualHost *:443>
        ServerName login.westos.org
        DocumentRoot "/var/www/westos.com/login"
        CustomLog logs/news.log combined
        SSLEngine on
        SSLCertificateFile /etc/httpd/westos.org.crt
        SSLCertificateKeyFile /etc/httpd/westos.org.key
</VirtualHost>

[root@westosa100 conf.d]# systemctl restart httpd

#########测试主机配置域名解析
[root@westos_student50 Desktop]# vim /etc/hosts
172.25.254.100 www.westos.com login.westos.org
^(/.*)$ ##客户地址栏中输入的地址
 %{HTTP_HOST} ##客户主机
 $1 ##RewriteRule后面跟的第一串字符的值
 整句的意思是讲:启动rewrite模块,将访问的域名请求,url地址内容不变,将http://变成https:

请添加图片描述

请添加图片描述

测试主机:

请添加图片描述

请添加图片描述
请添加图片描述

七、squid代理

使得一台主机能访问squid服务器上的缓存资源,达到更快的进行数据传输的作用:

正向代理:

实验环境:
单网卡主机设定ip不能上网
双网卡主机设定ip1可以连接单网卡主机,设定ip2可以上网
实验效果
让单网卡主机不能上网但浏览器可以访问互联网页
操作:
在双网卡主机中
dnf install squid -y
vim /etc/squid/squid.conf
59 http_access allow all
65 cache_dir ufs /var/spool/squid 100 16 256
systemctl restart squid
firewall-cmd --permanent --add-port=3128/tcp
firewall-cmd --reload在单网卡专辑中选择
NetWork Proxy
172.25.254.30
3128
测试:
在单网卡主机中
ping www.baidu.com
不通
在浏览器中访问www.baidu.com可以

双网卡主机即squid服务器:

请添加图片描述

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述请添加图片描述

此时单网卡主机上不了网:

请添加图片描述单网卡主机火狐:

请添加图片描述请添加图片描述
设置完即可以通过网页上网。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值