您的位置:首页 > 运维架构 > Apache

Linux之Apache

2018-01-31 12:06 134 查看
1.关于Apache

        apache是企业常用的web服务,用来提供http://(超文本传输协议),由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。

2.Apache的安装    

(1)先配置好yum源,Linux镜像中有Apache的安装包

yum install httpd -y                ##apache软件

yum install httpd-manual       ##apache手册

这里的yum源是在http的共享目录/var/www/html/rhel下



systemctcl start          httpd       ##开启httpd

systemctcl enable       httpd       ##为了使用方便开机自启

(2)设置防火墙

firewall-cmd   --list-all          ##列出火墙信息,查看那些服务是火墙允许通过的

防火墙默认允许ssh



firewall-cmd --permanent --add-service=http    ##永久允许http

firewall-cmd   --reload          ##火墙重新加载,读取重新加入的服务



3.Apache的基本信息

(1)/var/www/html/                    ###默认发布目录

/var/www/html/index.html   ###默认发布文件   

端口 :80



/etc/httpd/conf/httpd.conf     ###主配置文件

/etc/httpd/conf.d/*.conf         ###子配置文件

httpd_sys_content_t             ##默认安全上下文

(2)在/var/www/html/index.html写文件浏览器可以通过IP访问到



4.Apache的配置文件的更改

(1)默认发布文件可以指定多个

vim /etc/httpd/conf/httpd.conf            ###主配置文件

DirectoryIndex index.html westos     ##两个默认发布目录,当index.html不存在时,访问westos按顺序访问



systemctcl restart httpd                     ##重启httpd



(2)修改默认发布目录

(1)DocumentRoot “/westos/html"

<Directory "/westos/html">

    Require all granted

</Directory>



修改默认发布目录后,访问到的页面就是修改的/westos/html/index.html



(3)修改端口为8080



firewall-cmd --permanent --add-port=8080/tcp

firewall-cmd   --reload        ##火墙重新加载



ss -antlupe | grep httpd       ##查看端口号



访问时要加上端口号:8080(因为发布目录改了,是/westos/html/index.html)



5.Apache内部的访问控制(浏览器端IP:172.25.254.4,http端IP:172.25.254.104)
(1)针对与主机的访问控制
@<Directory "/westos/html/test">

order Allow,Deny     ##按顺序执行

Allow from all       ##允许所有的用户进入但是172.25.254.4不能访问

Deny  from 172.25.254.4    ##这个IP不能访问

</Directory>



限制172.25.254.4后,其访问不到:



@@<Directory "/westos/html/test">

order Deny,Allow            ##按顺序执行

Allow from 172.25.254.4     ##允许172.25.254.4访问

Deny  from  all              ##不允许所有的用户访问但是172.25.254.4能访问

</Directory>



(2)用户方式的访问控制

htpasswd -cm /etc/httpd/webuser   admin    ##-c:创建webuseer

htpasswd -m  /etc/httpd/webuser   admin1   ##创建过了就不用加-c



vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/html/">

 AuthUserFile /etc/httpd/webuser                        ##认证目录

 AuthName "please input your name and passwd"  ##提示

 AuthType basic                       ##认证类型

 #Require  user admin             ##只允许admin访问

 Require  valid-user              ##在webuser里的用户都可以访问

@@注意:Require 两者开启一个,不能同时开启

</Directory>



当使用/westos/html/为默认发布目录时,要注意在/westos/html/里再建立一个目录,

把这个目录设为默认发布目录,

当再次访问时,需要输入之前设定的密码和用户名



输入配置文件内允许的用户,成功登陆



6.Apache虚拟主机

(1)在浏览器端

vim /etc/hosts

:

172.25.254.4 www.westos.com news.westos.com music.westos.com

#####要访问的IP+域名



(2)服务器端

cd /etc/httpd/conf.d        ##配置文件所在目录

vim a_default.conf          ##默认页

:

<VirtualHost _default_:80>      ##端口80

    DocumentRoot /var/www/html   ##默认目录

    CustomLog logs/default.log combined    ##混合日志  

</VirtutalHost>



####建立music发布目录

mkdir -p /var/www/virtual/music.westos.com/html

####建立news发布目录

mkdir -p /var/www/virtual/news.westos.com/html



####建立music发布目录的内容

vim /var/www/virtual/music.westos.com/html/index.html

:this is music



####建立news发布目录的内容

vim /var/www/virtual/news.westos.com/html/index.html

:this is news





在/etc/httpd/conf.d/下建立子配置文件,同样也要重启http服务



####建立news的配置文件

vim news.conf

:

<VirtutalHost *:80>             ##默认端口80

  ServerName news.westos.com    ##访问的域名

  DocumentRoot /var/www/virtual/news.westos.com/html  ##news发布目录

  CustomLog logs/news.log combined       ##混合日志

</VirtutalHost>

<Directory "/var/www/virtual/news.westos.com/html">

   Require  all granted

</Directory>



####建立music的配置文件

vim music.conf                

:

<VirtutalHost *:80>

 ServerName music.westos.com            ##访问的域名

 DocumentRoot /var/www/virtual/music.westos.com/html  ##music发布目录

 CustomLog logs/music.log combined      ##混合日志

</VirtutalHost>

<Directory "/var/www/virtual/music.westos.com/html">  ##目录授权

   Require  all granted                 ##所有用户可以访问

</Directory>



重启http服务,子配置文件同样需要重启生效

www.news.com的访问页面



www.music.com的访问页面



7.三种常见的web语言

html       ###静态页面

php       ###动态页面,

php需要安装



vim  /var/www/html/index.php         ##默认发布目录中建立



访问显示出



cgi         ###注册表常用,后缀一定要加上



vim  /var/www/html/cgi/index.cgi



访问显示,每刷新一次时间也随着变化



8.https(明文加密)

(1)yum install mod_ssl          -y                   ###生成443端口和https服务



systemctl restart httpd.service

firewall-cmd --permanent --add-service=https    ###火墙允许通过https

firewall-cmd --reload                                            ##重新加载

(2)修改证书信息

yum install crypto-utils.x86_64             ##生成证书和文件的软件

genkey www.westos.com                     ##生成www.westos.com的证书和密钥



填写要修改的证书的信息



生成的证书和钥匙



vim /etc/httpd/conf.d/ssl.conf

把证书和文件加到生成的指定路径,



重启http服务,

信息变成之前编辑的www.westos.com 的证书信息



注意:ctrl+shift+delete                ###清空缓存

cd /dev/radom        ###加密字符都在这里提取,移动鼠标和打字都能生成

9.网页重写

输入指定网址http:// 强制转换为 https://进行加密

(1)在浏览器端(地址解析)

vim /etc/hosts

:

172.25.254.4 www.westos.com login.westos.com



(2)服务器端

建立虚拟主机的文件夹

mkdir -p /var/www/virtual/login.westos.com/html

建立login的发布文件

vim  /var/www/virtual/login.westos.com/html



建立login的配置文件

<Virtualhost *:443>                            ##https加密的端口为443

   ServerName login.westos.com      ##访问的域名

   DocumentRoot /var/www/virtual/login.westos.com/html     ##指定发布目录的路径

   CustomLog logs/login.log combined                                   ##混合日志

   SSLEngine on        ##https,443加密开启

   SSLCertificateFile  /etc/pki/tls/certs/www.westos.com.crt  ###证书的路径

   SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key  ###私钥路径

</Virtualhost>

<Directory /var/www/virtual/login.westos.com/html>      ##路径授权

           Require all granted

</Directory>

<Virtualhost *:80>                                ###http的端口为80

   ServerName login.westos.com        ###要加密的网址

   RewriteEngine on                             ###重写开启

   RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]

### ^(/.*)$         ##客户在浏览器地址栏输入的所有字符

    https://        ##强制客户加密访问

    %{HTTP_HOST}    ##客户请求主机

    $1              ##‘$1'表示^(/.*)$的所有值

    [redirect=301]  ##临时重写 ;302永久转换

</Virtualhost>



(3)在浏览器端测试,即使输入http://login.westos.com 也会强制转换为https://进行加密

首先要获取证书





成功访问



值得注意的是:

虚拟主机配置文件的建立,

浏览器端的地址解析,

加密的密钥和加密文件的获取

使用自定义的发布目录时,要修改目录安全上下文,否则http服务不识别
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux apache