Apache

Apache

Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python解释器编译到服务器中。

systemctl start httpd    开启http服务

systemctl enable httpd    开机自动启动

firewall-cmd --list-all    列出火墙信息

firewall-cmd --permanent --add-service=http    使火墙允许http服务

firewall-cmd --reload    火墙重新加载策略

Apache

Apache

Apache

ApacheApache

/var/www/html    apache的/目录。默认发布目录

/var/www/html/index    apache的默认发布文件

例 vim /var/www/html/index.html
<h1> hello  </h1>

退出查看ip就可以了 172.25.25.1/index.html

Apache

Apache

Apache

Apache 配置文件

主配置目录:/etc/httpd/conf

主配置文件:/etc/httpd/conf/httpd.conf
子配置目录:/etc/httpd/conf.d
子配置文件:/etc/httpd/conf.d/*.conf
/etc/httpd/conf/httpd.conf
164     DirectoryIndex index.html
默认发布目录/var/www/html
Apache
默认发布文件: index.html
默认端口:80
默认安全上下文 :httpd——sys——content_t
程序默认用户:apache

apache日至:/etc/httpd/logs/*


修改默认发布文件
/etc/httpd/conf/httpd.conf
164 DirectoryIndex index.html test.html    当index 不能用的时候看的是test

默认发布目录

ApacheApache

ApacheApache

ApacheApache

mkdir /westos/html 创建

vim /etc/httpd/conf/httpd.conf

修改默认发布目录
           主配置文件:注释掉原目录
             /etc/httpd/conf/httpd.conf
           120 DocumentRoot "/westos/html"
           121 <Directory "/westos/html">
           122            Require all granted

           123 </Directory>

如果selinux=enforcing,需要修改新建目录的安全上下文

            semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?'  修改安全上下文

            restorecon -RvvF /www/      刷新安全上下文(永久)

systemctl restart httpd
修改默认端口
/etc/httpd/conf/httpd.conf

43 listen 8080    端口号

防火墙添加:firewall-cmd --permanent --add-port=8080/tcp
                       firewall-cmd --reload

           测试:http://172.25.254.110:8080/tcp

apache内部访问控制
针对与主机的访问控制
<Directory> "/var/www/html/admin" 给这个文件授权    
    Order deny,allow    列表读取顺序,后读取的列表会覆盖
    Allow from 172.25.254.31
    Deny from all

</Directory>

用户访问控制
htpasswd -cm /etc/httpduserpass admin    创建含用户密码的文件,-c新建

htpasswd -m /etc/httpd/userpass    admin1    新增,-c会覆盖原来内容

htpasswd -cm webuser admin    创建用户设置密码
c 创建
m 设定

第二次添加只需要m如果cm则会覆盖之前的cm
添加密码登陆
cd /etc/httpd/    进入httpd目录
htpasswd -cm webuser admin    给webuser设置密码
输入密码
cat webuser    查看密码
mkdir  /var/www/html/admin    创建一个admin目录
vim /etc/httpd/conf/httpd.conf    进入配置文件
DocumentRoot “/var/www/html”    默认
<Directory> "/var/www/html/admin"    进入的目录
    AuthUserfile /etc/httpd/webuser    用户
    AUthName "Please input username and password!!"
    AuthType basic    类型
    Require user admin(密码正确则通过否则进不去)或者 Require valid-user
</Directory>

systemctl restart httpd

Require 通过了能进去不能通过就进步去

apache 虚拟主机

使所在的主机进行本地解析    www.westos.com    news.westos.com music.westos.com

cd /etc/httpd/conf.d   进入apache的主配置目录

Apache

vim a_default.conf    创建一个以a开头.conf 文件 a-z 重a开始最先打开
<VirtualHost _default_:80>
    DocumentRoot /var/www/html    
    CustomLog logs/default.log combined    

</VirtualHost>

mkdir /var/www/virutal/news.westos.com/html -p    创建news目录

cd /var/www/virtual/news.westos.com 进入news目录下

vim index.html    添加news主页面的内容

mkdir /var/www/virutal/music.westos.com/html -p    创建music目录

vim index.html    添加music的主页面的内容

vim news.conf    创建一个news.conf新加配置文件

Apache

<VirtualHost *:80>
    ServerName news.westos.com    名称
    DocumentRoot /var/www/virtual/news.westos.com/html    文件地址
    CustomLog logs/news.log combined    混合日志方式
</VirtualHost>
<Directory "/var/www/virtal/news.westos.com/html">    授权
    Require all granted
</Directory>

vim music.conf    创建一个music.conf 新加配置文件

Apache

<VirtualHost *:80>
    ServerName news.westos.com    名称
    DocumentRoot /var/www/virtual/music.westos.com/html    文件地址
    CustomLog logs/music.log combined    混合日志方式
</VirtualHost>
<Directory "/var/www/virtal/music.westos.com/html">    授权
    Require all granted

</Directory>

在你需要的电脑上添加你更改的解析
vim /etc/hosts
增加172.25.53.1 的解析

172.25.53.1      www.westos.com news.westos.com music.westos.com

测试

Apache

Apache

apache 支持的语言    php 和cgi(selinux=disabled)

安装php
yum inatall php -y
cd /var/www/html/
vim index.php 新建php的主页的配置文件
<?php
    phpinfo();

?>

Apache

ApacheApacheApache

systemctl restart httpd    重启服务

安装CGI
yum inatall httpd-manual -y    安装手册
cd /var/www/html
mkdir cgi    创建cgi目录
cd cgi        进入cgi目录
vim index.cgi    添加CGI的配置文件
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, World.";

chmod +x /cgi/index.cgi 给文件加执行权限

Apache


vim a_default.conf 编辑解析
<VirtualHost _default_:80>
    DocumentRoot /var/www/html    
    CustomLog logs/default.log combined    
</VirtualHost>
<Directory /var/www/html/cgi>    解析cgi
    Options +ExecCGI
    AddHandler cgi-script .cgi

</Directory>

测试

Apache

https 设置

添加https    加密(锁)
yum insatll mod_ssl -y    添加加密服务    
systemctl restart httpd    重启
firewall-cmd --list-all    查看防火墙
firewall-cmd --permanent --add-service=https    把https添加入防火墙的策略中
fire-cmd --reload 更新

Apache

yum whatprovides  */genkey   根据功能搜索服务软件包

 yum install crypto-utils-2.4.1-42.el7.x86_64 -y   自动安装

vim /etc/httpd/conf.d/ssl.conf    在/etc/httpd/conf.d/生成文件ssl.conf

Apache

Apache

Apache

Apache

ApacheApache

SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt     将genkey生成的认证文件替换原来的文件(100行)

SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key 将genkey生成的钥匙文件替换原来的文件(107行)

Apache

 systemctl restart httpd

ApacheApache


ctrl+shfit+delete       清除浏览器在浏览器中

8. 设定https虚拟主机并设定网页重写
新建目录:
    mkdir /var/www/virtual/log.westos.com/html -p
    vim /var/www/virtual/log.westos.com/html/index.html
    vim /etc/httpd/conf.d/log.conf
编辑文件log.conf内容:
    <VirtualHost *:443>        443端口,https服务端口
        ServerName log.westos.com    访问域名
        DocumentRoot /var/www/virtual/log.westos.com/html    指定servername访问的目录
        CustomLog logs/log.log combined    日志类型
        SSLEngine on
        SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt    ssl认证文件
        SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key    ssl认证私钥文件
    </VirtualHost>
    <Directory /var/www/virtual/log.westos.com/html>
        Require all granted
    </Directory>
    <VirtualHost *:80>
        ServerName log.westos.com
        RewriteEngine on
        RewriteRule ^(/.*)$ https://%{HTTP_HOST}$ [redirect=301](301临时重写,302永久重写)
    </VirtualHost>