Apache基本配置
来源:互联网 发布:苹果手机相机软件 编辑:程序博客网 时间:2024/06/04 08:03
1.安装
yum install -y httpd httpd-manual ##安装apache软件包
systemctl start httpd ##启动apache服务
systemctl enable httpd ##开机启动
2.虚拟主机
虚拟主机允许您从一个httpd服务器同时为多个网站提供服务
虚拟主机的设置:
mkdir -p /var/www/westos/news.westos.com
##创建需要存放其站点的目录
mkdir -p /var/www/westos/game.westos.com
vim /var/www/westos/news.westos.com/index.html
##输入编辑网页内容
vim /var/www/westos/game.westos.com/index.html
##其中的index.html文件名称是在配置文件中指定的,可自行修改
如果不在/var/www/下创建的目录。需要修改目录的安全上下文
例如:
semanage fcontext -a -t httpd_sys_content ‘/westos(/.*)?’
##修改/westos目录下的所有文件安全上下文为httpd_sys_content
restorecon -RvvF /westos/
##更新安全上下文
创建虚拟主机配置文件:
比如其中.conf文件的内容如下 <Virtualhost *:80>
ServerName www.example.com
DocumentRoot /srv/www.example.com/www
CustomLog "logs/www.example.com.log" combined
</Virtualhost>
<Directory /srv/www.example.com/www>
require all granted
</Directory>
这些东西所表示的含义:
1. <VirtualHost *:80>
...
</VirtualHost>
##定义虚拟主机的块
2. ServerName www.example.com
##指定服务器名称。在使用基于名称的虚拟主机的情况下,此处的名称必须与客户端请求完全的匹配。
3. DocumentRoot /srv/www.example.com/www
##在块内部,指定从中提供内容的目录
4.<directory /srv/www.example.com/www>
require all granted
</directory>
##这是配置文件的结构体,表示所有人都可访问,访问内容为srv/www.example.com/www目录下的内容
我们的具体配置如下:
cd /etc/httpd/conf.d/
vim default.conf
<virtualhost _default_:80> DocumentRoot /var/www/html CustomLog "logs/default.log" combined</virtualhost><directory “/var/www/html”> Require all granted</directory>
##定义主配置文件访问的端口和网址,并设置为任何人都可访问
vim news.westos.com.conf
<virtualhost *:80> ServerName news.westos.com DocumentRoot /var/www/westos/news.westos.com CustomLog "logs/news.log" combined</virtualhost><directory /var/www/westos/news.westos.com> Require all granted</directory>
##定义访问news.westos.com时的端口和网址,并设置为任何人都可访问
vim game.westos.com.conf
<virtualhost *:80> ServerName game.westos.com DocumentRoot /var/www/westos/game.westos.com CustomLog "logs/game.log" combined</virtualhost><directory /var/www/westos/game.westos.com> Require all granted</directory>
##定义访问news.westos.com时的端口和网址,并设置为任何人都可访问
启动apache服务 systemctl restart httpd
systemctl enable httpd
测试:
在测试机里添加本地dns解析内容: vim /etc/hosts
172.25.254.137 www.westos.com news.westos.com game.westos.com
打开浏览器:
在输入www.westos.com读取的为/var/www/html/index.html的内容
在输入news.westos.com读取的为/var/www/westos/news.westos.com/index.html的内容
在输入www.westos.com读取的为/var/www/westos/news.westos.com/index.html的内容
但是这样配置可能会出现在输入www.westos.com访问的并不是var/www/html/index.html的内容,需要将default.conf文件定义虚拟主机的块内加一条:ServerName www.westos.com ,但是这样做弊端是会固定主域名的网址。
3.配置基于用户的身份验证
Apache无格式文件用户身份验证
在此配置中,用户账户和密码存储在本地.htpasswd文件中。处于安全原因,该文件不能保存在网站的DocumentRoot中,而应保存在Web服务器不提供服务的一些目录中。
配置程序:
用两个账户创建Apache密码文件:
htpasswd -cm /etc/httpd/.htpasswd admin
##在/etc/httpd/.htpasswd文件中创建admin用户,密码会弹出提示让你输入
htpasswd -m /etc/httpd/.htpasswd admin1
##在/etc/httpd/.htpasswd文件中追加admin1用户,密码会弹出提示让你输入,注意是-m,不是-cm
配置文件:
vim /etc/httpd/conf/httpd.conf
<Directory /var/www/html>AuthName “Secret Stuff”AuthType basicAuthUserFile /etc/httpd/.htpasswdRequire valid-user</Directory>
systemctl restart httpd ##重启服务
测试:
在浏览器测试访问,会弹出的对话框,让你输入上述你所设置的用户名和密码。
4.https
自定义自签名证书
如果加密的通信非常重要,而经过验证的身份不重要,管理员可以通过生成self-signed certificate来避免与认证机构进行交互所带来的复杂性。使用genkey实用程序(通过crypto-utils软件包分发),生成自签名证书及其关联的私钥。
具体步骤:
yum install crypto-utils -y
##安装crypto-utils软件包
genkey server0.example.com
##为生成的文件指定唯一名称
记录生成的证书(server0.example.com.crt)和关联的私钥(
server0.example.com.key)的位置
选择合适的密钥大小。(默认的2048位密钥为推荐值)
在生成随机数时比较慢,需要敲键盘和移动鼠标
拒绝向认证机构(CA)发送证书请求(CSR)。
拒绝加密私钥
为服务器提供合适的身份。Common Name必须与服务器的主机全名完全匹配
安装证书及其私钥 yum install mod_ssl -y
##专门为apache webserver 提供密码保护,ssl(安全套阶层)和 tls(传输套件层安全)的协议
编辑/etc/httpd/conf.d/ssl.conf, vim /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/pki/tls/certs/server0.example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/server0.example.com.key
##将SSLCertificateFile和SSLCertificateKeyFile指令设置为分别指向证书和密钥文件。
systemctl restart httpd
##重启Web服务器
测试:
在浏览器中输入https://server0.example.com
网页定向重写
编辑文件 vim /etc/httpd/conf.d/https.conf
<VirtualHost *:443> ServerName https.example.com DocumentRoot /var/www/https/ SSLEngine on SSLCertificateFile /etc/pki/tls/certs/https.crt SSLCertificateKeyFile /etc/pki/tls/private/https.key</VirtualHost><Directory "/var/www/https"> Require all granted</Directory><VirtualHost *:80> ServerName https.example.com RewriteEngine on RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]</VirtualHost>
其中上面两个块前面已经说过,最后这个块中RewriteRule表示的含义如下:
^(/.*)$ #客户主机在地址栏中写入的所有字符,但不包含换行符和空格
https:// #定向成为的访问协议
%{HTTP_HOST} #客户请求的主机 $1
#表示^(/.*)$的值
[redirect=301] #临时重定向 302位永久重定向
mkdir /var/www/htpps/ -p ##递归建立目录
vim /var/www/htpps/index.html ##编辑https网址显示的内容
systemctl restart httpd ##重启服务
测试:
在浏览器中输入https.example.com,跳转的实际页面位hhtps://https.example.com
- Apache基本配置指南
- Apache基本配置指南
- apache 基本vhost配置
- apache基本配置
- apache基本配置
- apache 基本vhost配置
- Apache 基本配置
- Apache基本配置-1
- Apache Solr基本配置
- apache+nginx基本配置
- Apache基本配置-2
- Apache基本配置-3
- Apache基本配置
- apache的基本配置
- Apache基本配置
- Apache基本配置
- Apache 简介及基本配置
- Apache 简介及基本配置
- 网路、计算机 术语
- 创建Zigbee Z-Stack 3.0.1 BSP(三)--创建基于UltraIot工程模板
- 神奇的常量池和intern方法
- SSM框架不用实体类,查询单个对象返回Map,多条记录返回list<Map>,及分页
- 通过application实现网页登陆用户数显示的demo
- Apache基本配置
- 安装Apache
- 【JZOJ 5271】神奇的救火现场
- leetcode 110 .Balanced Binary Tree
- Ubuntu17.04系统安装必备软件与存在的问题
- linux 历史命令
- 机器学习算法整理之Logistic Regression (LR) 逻辑斯谛回归 :分类方法(对数线性模型)
- PID算法
- 关于枚举的理解