[Linux]Apache(web服务器)设置

    xiaoxiao2022-07-05  196

    1、介绍

    Apache作为web服务器软件,可以广泛的使用在所有计算机平台

    以下介绍为LAMP环境搭建

    L:Linux

    A:Apache

    M:MariaDB或MySQL,数据库管理系统

    P:PHP或Python,Perl

    2、安装Apache

    (1)通过yum仓库查找Apache

    命令:yum search Apache

    安装所选内容

    命令:yum install httpd.x86_64

    (2)设置防火墙及selinux

    在services中添加http

    命令:firewall-cmd --permanet --add-service=http

               firewall-cmd --reload

    selinux工作模式为enforcing

    配置文件:/etc/sysconfig/selinux

    3、使用Apache

    (1)使用默认设定

    默认发布目录

    /var/www/html

    默认发布文件

    index.html

    进入浏览器输入本机ip172.25.254.119                                 (ip为个人设定)

    (2)关于Apache基本信息

    主配置文件目录 /etc/httpd/conf 主配置文件 /etc/httpd/conf/httpd.conf 子配置目录 /etc/httpd/conf.d/ 子配置文件 /etc/httpd/conf.d/*.conf 日志信息 /etc/httpd/logs/

    (3)修改默认设置

    端口设置:

    1、查看http能够使用的端口

    命令:semange port -l |grep http

    2、修改配置文件,将端口修改为666

    配置文件:/etc/httpd/conf/httpd.conf

    若修改的端口号在能够使用的端口号中,修改配置文件后重启服务即可

    3、防火墙添加666端口并重新加载

    命令:firewall-cmd --permanent --add-port=666/tcp

                firewall-cmd --permanent --reload

    4、若所设置的端口没在http可使用端口中需添加

    命令:semanage port -a -t http_port_t -p tcp 666

    5、重启服务

    修改默认发布文件:

    1、修改配置文件

    配置文件:/etc/httpd/conf/httpd.conf

    默认发布文件可填写多个,只读取第一个,当第一文件读取失败读取第二个

    在默认发布目录下建立admin.html并编写

    2、浏览器输入本机ip进行测试

    修改默认发布目录:

     1、修改配置文件

    配置文件:/etc/httpd/conf/httpd.conf

    DocumentRoot                                设置默认发布目录

    <Directory>                                      对目录授权

    ......

    </Directory>

    2、添加安全上下文

    命令:semanage fcontext -a -t httpd_sys_content_t '/admin/www(/.*)?'

                 restorecon -RvvF /admin/www/

    3、浏览器输入本机ip进行测试

    4、设置Apache的虚拟主机

    1、建立目录并建立默认发布文件 

    命令:mkdir -p /var/www/virt/host             mkdir -p /var/www/virt/news

    在每个目录下建立默认发布文件index.html

    2、在子配置文件目录下创建配置文件vhost.conf                            (文件名任意,但需以.conf结尾)

    <VirtualHost _default_:80> DocumentRoot "/var/www/html" CustomLog "logs/default.log" combined </VirtualHost> <VirtualHost *:80> ServerName host.admin.com #站点名称 DocumentRoot "/var/www/virt/host" #默认发布目录 CustomLog "logs/host.logs" combined #日志 </VirtualHost> <Directory "/var/www/virt/host"> #对目录进行授权 Require all granted </Directory> <VirtualHost *:80> ServerName news.admin.com DocumentRoot "/var/www/virt/news" CustomLog "logs/news.logs" combined </VirtualHost> <Directory "/var/www/virt/news"> Require all granted </Directory>

     3、设定解析

    配置文件:/etc/hosts

    添加

    172.25.254.119 www.admin.com host.admin.com news.admin.com

    4、测试

    5、Apache访问设置

    基于ip

    配置文件:/etc/httpd/conf.d/vhost.conf

    (在conf.d 下任意名字以.conf结尾)

    Order   为列表读取顺序,后读取的列表会覆盖限度去内容的重复部分

    以上为禁止所有ip访问

    以上为仅允许指定 ip访问

    基于用户

    (1)创建用户

    命令:htpasswd -cm    /etc/httpd/Userlist    admin

               -c   为创建       -m   为md5加密        /etc/httpd       目录           Userlist         文件存储用户            admin为用户名

                htpasswd -m     /etc/httpd/userlist    student

               若文件已经存在则不需要加-c,若加上则会覆盖

    (2)修改配置文件

    配置文件:/etc/httpd/conf.d/vhost.conf

    (在conf.d 下任意名字以.conf结尾)

    (3)输入ip进行访问

    输入正确

    输入错误

     

    6、Apache支持的语言

    (1)html

    以上介绍为html语言

    (2)php

    1、安装php.x86_64

    2、在目录发布目录下编写php文件

    例:

    <?php phpinfo(); ?>

    3、修改配置文件

    配置文件:/etc/httpd/conf/httpd.conf

    将目录发布文件设定为php文件

    4、浏览器输入ip进行测试

    (3)cgi

    1、建立cgi目录

    命令:mkdir -p /var/www/html/cgi

    2、修改安全上下文

    命令:semanager fcontent -a -t  httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?'             restorecon -RvvF /var/www/html/cgi/

    3、编写cgi文件

    路径:/var/www/html/cgi/index.cgi

    例:

    #!/usr/bin/perl print "Content-type: text/html\n\n"; print `date`;

    4、修改cgi文件权限

    增加执行权利

    5、修改配置文件

    配置文件:/etc/httpd/conf.d/vhost.conf 在conf.d 下任意名字以conf结尾

    6、浏览器输入ip进行测试

    (4)wsgi

    1、安装mod_wsgi

    命令:yum install mod_wsgi

    2、编写wsgi文件

    路径:/var/www/html/cgi/webapp.wsgi

    例:

    #!/usr/bin/env python import time def application (environ, start_response): response_body = 'UNIX EPOCH time is now: %s\n' % time.time() status = '200 OK' response_headers = [('Content-Type', 'text/plain'), ('Content-Length', '1'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) return [response_body]

    3、修改配置文件 配置文件:/etc/httpd/conf.d/vhost.conf 在conf.d 下任意名字以conf结尾

    4、添加解析 /etc/hosts 172.25.254.119 wsgi.admin.com

    5、测试

    7、https

    https为超文本传输协议,默认开发的端口为443

    (1)安装mod_ssl和crypto-utils

    mod_ssl为提供https服务

    crypto-utils提供生成证书和密钥

    (2)设置防火墙

    命令:firewall-cmd --permanet --add-service=https

               firewall-cmd --reload

    (3)生成证书和密钥

    命令:genkey www.admin.com

    www.admin.com      (该名称任意设定)

    设置密钥和证书保存位置

    密钥大小

    进行加密,加密途中可能出现加密字符不足,需在shell中敲击键盘

    是否将将证书发送给CA(选择No)

    设置密码(为了方便,不设置)

    设置完成后信息显示

    (4)编辑配置文件

    路径:/etc/httpd/conf.d/ssl.conf

    将生成key的证书添加

    (5)重启服务后测试

    添加证书

    证书信息

    8、设定https虚拟主机并设定网页重写

    当输入网址时,默认是以http方式,当进行网页重写可使用https

    (1)新建l/var/www/virt/login

    在该目录下编写html文件

    (2)修改配置文件

    配置文件:/etc/httpd/conf.d/vhost.conf 在conf.d 下任意名字以conf结尾

    域名设置为login.admin.com

    (3)添加解析

    配置路径/etc/hosts

    (4)浏览器中测试

     

    最新回复(0)