Apache
1 前言
笔者平素不喜欢简单的编译安装,因为很多系统管理员为了省事,编译安装的东西没有rpm包安装那种容易管理的感觉,所以,笔者的编译安装是尽量接近rpm安装的。
2 最佳实践
2.1 环境配置
2.1.1 环境配置
IP Address = 10.168.0.90
OS Type = CentOS 7.x-x86_64, RHEL-7.x x86_64, RHEL-8.x-x86_64
2.1.2 安装编译环境
yum -y install gcc gcc-c++ make expat-devel
2.1.3 安装常用工具
yum install -y wget net-tools vim bzip2 openssh-clients
2.1.4 下载apache软件包
cd ~ wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.6.5.tar.gz wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz wget http://archive.apache.org/dist/httpd/httpd-2.2.34.tar.bz2
2.1.5 防火墙配置
firewall-cmd --permanent --add-service http firewall-cmd --reload firewall-cmd --list-all
2.1.6 关闭SELinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0
2.2 编译部署APR
2.2.1 清理旧的apr包
yum remove -y arp apr-util apr-util-ldap
2.2.2 解压软件包
cd ~ tar -xf apr-1.6.5.tar.gz tar -xf apr-util-1.6.1.tar.gz
2.2.3 预编译APR
cd ~/apr-1.6.5 ./configure --prefix=/usr \ --libdir=/usr/lib64 make make install
2.2.4 预编译APR-Util
cd ~/apr-util-1.6.1 ./configure --prefix=/usr \ --with-apr=/usr \ --libdir=/usr/lib64 make make install
2.3 编译部署Apache HTTPD
2.3.1 解压软件包
cd ~ tar -xf httpd-2.2.34.tar.bz2
2.3.2 预编译软件包
cd ~/httpd-2.2.34 ./configure --prefix=/var/www \ --bindir=/usr/bin \ --sbindir=/usr/sbin \ --libexecdir=/usr/libexec \ --sysconfdir=/etc/httpd \ --localstatedir=/var \ --libdir=/usr/lib64 \ --includedir=/usr/include \ --datarootdir=/usr/share \ --infodir=/usr/share/info \ --localedir=/usr/share/locale \ --mandir=/usr/share/man \ --docdir=/usr/share/doc/httpd \ --enable-so
如果遇到如下错误提示,
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
则需要安装以下包解决依赖关系,
yum install -y pcre-devel
或者,
yum install -y pcre2-devel
2.3.3 预编和安装软件包
make make install
2.3.4 创建运行用户
groupadd -g 48 apache useradd -u 48 -g 48 -d /usr/share/httpd -s /sbin/nologin apache
2.3.5 编辑配置文件
可以使用如下命令修改如下参数,
vim /etc/httpd/httpd.conf
参数定义如下,
User apache Group apache ErrorLog "/var/log/httpd/error.log" CustomLog "/var/log/httpd/access.log" common ServerName www.example.com:80 PidFile "/var/run/httpd.pid" Include /etc/httpd/conf.d/*.conf
另外,也可以使用如下快速指令修改,
sed -i "s/User daemon/User apache/g" /etc/httpd/httpd.conf sed -i "s/Group daemon/Group apache/g" /etc/httpd/httpd.conf sed -i "s/ErrorLog \"\/var\/logs\/error_log\"/ErrorLog \"\/var\/log\/httpd\/error.log\"/g" /etc/httpd/httpd.conf sed -i "s/CustomLog \"\/var\/logs\/access_log\" common/CustomLog \"\/var\/log\/httpd\/access.log\" common/g" /etc/httpd/httpd.conf sed -i "s/#ServerName www.example.com:80/ServerName www.example.com:80/g" /etc/httpd/httpd.conf echo 'PidFile "/var/run/httpd.pid"' >> /etc/httpd/httpd.conf echo 'Include /etc/httpd/conf.d/*.conf' >> /etc/httpd/httpd.conf
根据配置文件要求,修改或增加以下目录,
mv /var/logs /var/log/httpd mkdir /etc/httpd/conf.d
修改完成后,建议使用如下指令检查配置文件的语法,
httpd -t
2.3.6 测试服务启动与关闭
apachectl -f /etc/httpd/httpd.conf -k start
另外,启动服务后可以使用如下命令确认服务是否启动,
netstat -antp | grep httpd ps -ef | grep httpd
确认服务无异常后,建议使用如下命令先停止此服务以利于后面的配置,
apachectl -f /etc/httpd/httpd.conf -k stop
2.4 安装优化与定制
2.4.1 配置服务
vim /etc/systemd/system/httpd.service
增加如下配置:
[Unit] Description=httpd service After=syslog.target network.target [Service] Type=forking PIDFile=/var/run/httpd.pid ExecStart=/bin/sh -c '/usr/sbin/apachectl -f /etc/httpd/httpd.conf -k start' ExecStop=/bin/sh -c '/usr/sbin/apachectl -f /etc/httpd/httpd.conf -k stop' ExecReload=/bin/sh -c '/usr/sbin/apachectl -f /etc/httpd/httpd.conf -k graceful' [Install] WantedBy=multi-user.target
重载使配置生效:
systemctl daemon-reload
2.4.2 启动服务并开机启动
systemctl start httpd.service systemctl enable httpd.service
启动后,可以尝试使用浏览器测试此服务的访问,
http://10.168.0.90/
2.4.3 配置日志切割
vim /etc/logrotate.d/httpd
加入如下配置,
/var/log/httpd/*log { missingok notifempty sharedscripts delaycompress postrotate /usr/sbin/apachectl -k graceful > /dev/null 2>/dev/null || true endscript }
配置完成后,你可以使用如下命令测试日志切割,
logrotate /etc/logrotate.d/httpd
没有评论