源码安装httpd
安装关联包: yum -y install gcc gcc-c++ apr apr-devel cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr-util-devel apr-util pcre-devel pcre lrzsz
安装
解包:tar zxf httpd-2.4.46.tar.gz -C /usr/local/
进入:cd /usr/local/httpd-2.4.46
指定路径:./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi
–prefix:指定将httpd服务程序安装到哪个目录下
–enable-so:启用动态加载模块支持,使httpd具备进一步扩展功能的能力
–enable-rewrite:启用网页地址重写功能,用于网站优化及目录迁移维护
–enable-charset-lite:启用字符集支持,以便支持使用各种字符集编码的网页
–enable-cgi:启用CGI脚本程序支持,便于扩展网站的应用访问能力
编译、安装:make && make install
确认安装结果
配置目录:ls /usr/local/httpd
/usr/local/httpd/bin:存放httpd服务的各种执行程序文件,包括主程序httpd,服务控制工具apachectl等
/usr/local/httpd/cgi-bin:存放和动态网页有关(各种CGI程序)文件
/usr/local/httpd/logs:存放httpd服务的日志文件(错误日志error_log和访问日志access_log)
/usr/local/httpd/conf:存放httpd服务的各种配置文件,包括主配置文件httpd.conf、增强配置子目录extra等(extra目录下是额外配置文件)
/usr/local/httpd/htdocs:存放网页文档,包括默认首页文件index.html等。(注:以rpm包方式安装的httpd,默认存放网页文档的目录是/var/www/html/)
/usr/local/httpd/modules:存放httpd服务的各种模块文件
优化执行路径
方法一:建立软连接
ln -s /usr/local/httpd/bin/* /usr/local/bin/
方法二:修改PATH变量
配置文件:vim /etc/profile
PATH=$PATH:/usr/local/httpd/bin
执行生效:source /etc/profile
查看程序版本
httpd -v
添加httpd系统服务
使用chkconfig添加系统服务
复制:cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
配置文件:vim /etc/init.d/httpd
在#/bin/sh 下添加
#chkconfig: 35 85 21
#description:httpd servier
执行chkconfig命令将httpd添加为系统服务:chkconfig --add httpd
建立.service配置文件
vim /lib/systemd/system/httpd.service
[Unit]
Description=the apache http server
After=network.target
[Service]
Type=forking
PIDfile=/usr/local/httpd/logs/httpd.pid
ExecStart=/usr/local/httpd/bin/apachectl $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
重载service文件:systemctl daemon-reload
开启启动:systemctl enable httpd.service
查看是否启动:systemctl is-enabled httpd.service
启动服务:systemctl start httpd.service
查看状态:systemctl status httpd.service
访问控制
客户机地址访问控制
限制哪些ip能访问,哪些ip不能访问
配置文件:vim /usr/local/httpd/conf/httpd.conf
多个不带not的Require配置语句之间是或的关系
Require ip 192.168.1.220
Require ip 192.168.1.225
只允许“192.168.1.220”和"192.168.1.225"访问
不带not的Require配置语句,又出现了带not的Require配置语句,则语句之间是与的关系。如果出现了带not语句,要用</ RequireAll >包裹
Require ip 192.168.1.0/24
Require not ip 192.168.1.230
</ RequireAll >
表示在192.168.1.0网段中,除了禁止192.168.1.230访问,其他的ip都允许访问
用户授权限制
认证:验证用户名和密码。用户名和密码输入正确表示认证成功,这个用户能不能访问还要看有没有权限
授权:授予访问某个目录的权限
方式:
摘要认证:有些浏览器不支持,使用摘要认证需要在编译httpd之前添加“—enable-auth-digest”选项
基本认证:所有浏览器都支持。不需要添加模块
基本认证设置步骤:
创建用户认证数据文件:/usr/local/httpd/bin/htpasswd -c /usr/local/httpd/con/.awspwd webadmin
添加用户:/usr/local/httpd/bin/htpasswd /usr/local/httpd/con/.awspwd user1
查看:cat /usr/local/httpd/conf/.awspwd
<Directory “/usr/local/httpd/htdocs”>
…………
AuthName “welcome” #提示信息
AuthType Basic #认证类型是基本认证
AuthUserFile /usr/local/httpd/conf/.awspwd #认证文件
Require valid-user #设置只有文件中用户才能访问
基于域名
配置:vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
主配置文件引用httpd-vhosts.conf文件
配置文件:vim /usr/local/httpd/conf/httpd.conf
取消注释:Include conf/extra/httpd-vhosts.conf
注释主配置文件的ServerName:ServerName www.test.com
检测语法:httpd -t
重启服务:systemctl restart httpd
windows改hosts文件:C:\Windows\System32\drivers\etc\hosts
添加:
192.168.1.220 www.test.com
192.168.1.220 www.abc.com
192.168.1.220 test.com
192.168.1.220 abc.com
基于端口
改httpd-vhosts.conf文件:
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 192.168.1.220:801>
ServerAdmin webmaster@test.com
DocumentRoot “/var/www/test/html”
ServerName www.test.com
ServerAlias test.com
ErrorLog “logs/www.test.com-error_log”
CustomLog “logs/www.test.com-access_log” common
<Directory “/var/www/test/html”>
Require all granted
<VirtualHost 192.168.1.220:802>
ServerAdmin webmaster@abc.com
DocumentRoot “/var/www/abc/html”
ServerName www.abc.com
ServerAlias abc.com
ErrorLog “logs/www.abc.com-error_log”
CustomLog “logs/www.abc.com-access_log” common
<Directory “/var/www/abc/html”>
Require all granted
该主配置文件,添加监听端口
vim /usr/local/httpd/conf/httpd.conf
添加:
Listen 801
Listen 802
重启:systemctl restart httpd
查看监听端口:netstat -anput |grep httpd
基于ip
添加ip:
vim /etc/sysconfig/network-scripts/ifcfg-ens33
添加:
IPADDR1=192.168.1.220
PREFIX1=24
IPADDR2=192.168.1.210
PREFIX2=24
重启网卡:systemctl restart network
如果需要配置端口:vim /usr/local/httpd/conf/httpd.conf
编辑httpd-vhosts.conf文件:
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 192.168.1.220:80>
ServerAdmin webmaster@test.com
DocumentRoot “/var/www/test/html”
ServerName www.test.com
ServerAlias test.com
ErrorLog “logs/www.test.com-error_log”
CustomLog “logs/www.test.com-access_log” common
<Directory “/var/www/test/html”>
Require all granted
<VirtualHost 192.168.1.210:80>
ServerAdmin webmaster@abc.com
DocumentRoot “/var/www/abc/html”
ServerName www.abc.com
ServerAlias abc.com
ErrorLog “logs/www.abc.com-error_log”
CustomLog “logs/www.abc.com-access_log” common
<Directory “/var/www/abc/html”>
Require all granted
重启httpd:systemctl restart httpd
验证:浏览器验证