下载的源码包 http-2.425.tar.gz为例。(所有镜像都在主页)
首先配置IP
vim /etc/sysconfig/network-scripts/ifcfg-ens33 (以下为编辑内容)
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4 FAILURE FATAL=no
IPV6INIT=yes
IPV6 AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6 PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6 FAILURE FATAL=no
IPV6 ADDR GEN
MODE=stable-privacy
NAME=ens33
UUID=ed98e80b-99da-4e79-9f31-3fd47ccc7ee9
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.1.10 (IP地址)
NETMASK=255.255.255.0 (子网掩码)
GATEWAY:=192.168.1.1 (网关)
重启网络服务
systemctl restart network
为避免端口冲突、程序冲突等现象,建议先卸载使用rpm方式安装http。
rpm -e http --nodeps
挂载光盘(以1611为例)
eject (弹出光盘)
插入1611光盘
mount /dev/cdrom /media (指定挂载路径)
cd /media/Packages (进入安装目录)
1、Apache安装软件及开发包
rpm -ivh apr-1.4.8-3.el7.x86_64.rpm
rpm -ivh apr-devel-1.4.8-3.el7.x86_64.rpm
rpm -ivh cyrus-sasl-devel-2.1.26-20.el7_2.x86_64.rpm
rpm -ivh expat-devel-2.1.0-8.el7.x86_64.rpm
rpm -ivh libdb-devel-5.3.21-19.el7.x86_64.rpm
rpm -ivh openldap-devel-2.4.40-13.el7.x86_64.rpm
rpm -ivh apr-util-devel-1.5.2-6.el7.x86_64.rpm
rpm -ivh apr-util-1.5.2-6.el7.x86_64.rpm
rpm -ivh pcre-devel-8.32-15.el7_2.1.x86_64.rpm
rpm -ivh pcre-8.32-15.el7_2.1.x86_64.rpm
2、源码编译和安装
插入web光盘(参考挂载光盘)
(1)解包;
将http源码包解压并释放到/usr/src 目录下
tar zxf httpd-2.4.25.tar.gz -C /usr/src
cd /usr/src/httpd-2.4.25/
(2)配置
./configure --prefix=/usr/local/httpd --enable-os --enable-rewrite --enable-charset-lite --enable-cgi
prefix:指定将httpd服务程序安装到哪个目录
--enable--so:启用动态加载模块支持,使httpd具备进一步扩展功能的能力
--enable--rewrite:启用网页地址重写功能,用于网站优化及目录迁移维护。
--enable--charset--lite:启动字符集支持,以便支持使用各种字符集编码的网页。
--enable--cgi:启用CGl脚本程序支持,便于扩展网站的应用访问能力
(3)编译及安装
make && make install
(4)重启Apache服务
/usr/local/httpd/bin/apachectl restart (重启)
(5)查看端口
netstat -anpt | grep httpd
3、确认安装结果
ls /usr/local/httpd (下两行为http下目录)
bin cgi-bin error icons logs manual
build conf htdocs include man modules
/usr/local/httpd/bin:存放httpd服务的各种执行程序文件,包括主程序httpd、服务控制工具apachectl等。
/usr/八ocal/httpd/cgi-bin:存放各种CGl程序文件。
/usr/local/httpd/Iogs:存放httpd服务的日志文件。
/usr/local/httpd,/conf:存放httpd服务的各种配置文件,包括主配置文件httpd.conf、增强配置子目录extra等。
/usr/local,/httpd,/htdocs:存放网页文档,包括默认首页文件index.html等。
usr/local/,httpd,/modules:存放httpd服务的各种模块文件。
4、优化执行路径
通过源码编译安装的ttpd服务,程序路径并不在默认的搜索路径中,为了使该服务在使用时更加方便,可以为相关程序添加符号链接。
ln -s /usr/local/httpd/bin/* /usr/local/bin
ls -l /usr/local/bin/httpd /usr/local/bin/apachectl
再执行相关命令时就不用输入冗长的路径了。例如,当执行"httpd-v”命令(用于查看程序版本)时,即相当于执行“/usr/local/httpd/bin/httpd-v”命令。
httpd -v
5、添加httpd系统服务
使用chkconfig添加系统服务若希望将httpd添加为系统服务,以便通过chkconfig进行管理,需要建立可控的服务脚本。例如,可将apachectl脚本复制为/etc/init.d/httpd,并在文件开头添加chkconfig识别配置,然后将其添加为标准的Linux系统服务。
cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
vim /etc/init.d/httpd
(下列为编写内用)
#!/bin/bash
#chkconfig: 35 85 21
#description: Startup script for the Apache HTTP Server
6、建立【service】.service配置文件在/Iib/systemd,/system,/目录下,建立一个以.service结尾的单元(unit)配置文件,用于控制由systemd控制或监控的httpd服务。配置文件主要分为三个部分,控制单元Unit】的定义、服务【Service】
chkconfig --add httpd
7、 将httpd添加为系统服务 :
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/bin/apachect1 $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy-=graphical.target
Description:设置服务单元描述字符串。
After:设置服务单元的启动先后顺序。
network.target”表示启动时先启动network.target,后启动httpd.service,关闭时顺序相反。
与After字段相对应的还有一个Before字段,如httpd.service单元文件中出现
"Before-=network.target'”表示启动时先启动httpd.service,后启动network,target。
Type:设置服务进程的启动类型。需要注意的是,当启动类型为forking时需要同时设置PIDFile配置项,以帮助systemd准确定位到服务的主进程。
PIDFile:设置服务的守护进程的PID文件。
ExecStart:设置服务启动时执行的命令。
ExecReload:设置服务重新加载时执行的命令。
KillMode:
设置在单元停止时,杀死进程的方法。process表示仅杀死主进程。
Restart:设置服务进程正常退出、异常退出、被杀死,超时的时候,是否重启该服务。on-failure表示仅在进程异常退出时重启。
RestartSec:设置在重启服务前暂停多长时间,默认值是100毫秒(100ms)。
WantedBy:设置该服务所在的Target(运行目标)。WantedBy-=graphical.target
(图形界面多用户系统)是指,httpd所在的Target是graphical.target。这个设置非常重要,因执行"systemct enable httpd,service”命令时,httpd.service的符号链接就会放在/etc/systemd,/system目录下的graphical.target,wants子目录中。在系统进入graphical,target运行目标时,该目录下的所有服务均开机启动。该配置项常用的Target除了graphical.target之外,还有muti-user,target.
8、httpd服务开机启动:
systemctl enable httpd.service
9、查看httpd服务的自启动状态
systemctl is-enabled httpd.service
10、启动Apache服务
/usr/local/httpd/bin/apachectl start (启动)
/usr/local/httpd/bin/apachectl restart (重启)
到此就可以用IP访问http服务器了,但是如果用域名访问http服务器怎么办呢?参考以下。
DNS域名解析
1、安装dns包(挂载1611)
rpm -ivh bind-9.9.4-37.el7.x86_64.rpm bind-chroot-9.9.4-37.el7.x86_64.rpm
2、主DNS配置
vim /etc/named.conf (进入编辑)
options {
listen-on port 53 {127.0.0.1;}; (此行删除)
listen-on-v6 port 53 {::1;}
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query {localhost;}; (此行删除)
3、添加benet.com区域
zone "benet.com" in {
type master;
file "benet.com.zone";
};
4、 区域数据文件(正向)
vim /var/named/benet.com.zone (进入编辑)
$ttl 86400
@ in soa benet.com. admin.benet.com. (
20220423
3h
15m
1w
1d
)
in ns ns1.benet.com.
ns1 in a 192.168.1.10
www in a 192.168.1.10
5、重启服务:
systemctl restart named
排错检查/etc/named.conf文件:
named-checkconf -z /etc/named.conf
web站点部署
配置httpd服务
vim /usr/local/httpd/conf/httpd.conf
(以下为输入内容)
ServerName www.benet.com
如果没有语法错误,将会显示“Syntax OK
apachectl -t
(以下为显示内容)
Syntax OK
启动Apache服务
/usr/local/httpd/bin/apachectl start (启动)
/usr/local/httpd/bin/apachectl restart (重启)
启动http服务、默认将监听TCP协议的8O端口
systemctl start httpd
netstat -anpt | grep httpd
(以下为查看内容)
tcp6 0 0 :::80 :::* LISTEN 68130/httpd
httpd服务器默认已提供了一个名为index,html的测试网页(可显示字串“It works!”),作为访问网站时的默认首页。
cat /usr/local/httpd/htdocs/index.html
(以下为显示内容)
<html><body><h1>It works!</h1></body></html>