标签归档:LAMP

Centos 7源代码安装配置 Apache 生产篇

什么是Apache?

Apache是​​互联网上最流行的网络web服务器。全球一半以上网站都是使用Apache作为服务器。其也是一个工业级的 Web服务器。

在本文中我主要介绍如何安装Apache HTTP服务器,你只要按照本教程的步骤操作,一定可以安装成功,需要说明的是,如果你只安装Apache,而不安装php,请在配置文件中,删除 FilesMatch 包含的内容,否则会报错找不到PHP。

如果你需要安装PHP/Mysql数据库,那么本站教程太适合你了,关于php和mysql的安装,请阅读:11、12步骤。

说明:本文教程适用于生产环境,Apache是基于FPM/FastCGI解析的PHP。

系统环境:Centos 7.4 Apache 2.4.29

1.更新系统

$ yum update && yum upgrade -y

2.安装扩展包以及依赖包

$ yum install epel-release -y
$ yum install gcc gcc-c++ openssl openssl-devel libtool expat-devel zlib-devel python-devel -y

3.安装pcre

$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz
$ tar zxvf pcre-8.41.tar.gz
$ cd pcre-8.41
$ ./configure
$ make -j8 && make install

4.安装nghttp2

$ wget //github.com/nghttp2/nghttp2/releases/download/v1.27.0/nghttp2-1.27.0.tar.gz
$ tar zxvf nghttp2-1.27.0.tar.gz
$ cd nghttp2-1.27.0
$ ./configure
$ make -j8 && make install
$ echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
$ ldconfig

5.安装Apache httpd

创建用户与组并下载httpd apr apr-util安装包:

$ groupadd www
$ useradd -g www www
$ cd /tmp
$ wget //mirrors.shuosc.org/apache//httpd/httpd-2.4.29.tar.gz
$ wget //mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.6.3.tar.gz
$ wget //mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.gz

解压相关包:

$ tar xvf httpd-2.4.29.tar.gz
$ tar xvf apr-1.6.3.tar.gz
$ tar xvf apr-util-1.6.1.tar.gz

apr-1.6.3apr-util-1.6.1移动到httpd-2.4.29/srclib目录下。操作如下:

$ cd httpd-2.4.29
$ cp -R ../apr-1.6.3 srclib/apr
$ cp -R ../apr-util-1.6.1 srclib/apr-util

注意:移动不准带版本号,否则编译安装时找不到库。

开始安装:

$ ./configure \
--prefix=/usr/local/apache \
--enable-mods-shared=most \
--enable-headers \
--enable-mime-magic \
--enable-proxy \
--enable-so \
--enable-rewrite \
--with-ssl \
--with-nghttp2 \
--enable-ssl \
--enable-deflate \
--with-pcre \
--with-included-apr \
--with-apr-util \
--enable-mpms-shared=all \
--with-mpm=prefork \
--enable-remoteip \
--enable-http2 \
--enable-dav \
--enable-expires \
--enable-static-support \
--enable-suexec \
--enable-modules=all \
$ make -j8 && make install

安装完成。

6.配置httpd.conf

$ cd /usr/local/apache/conf
$ vim httpd.conf

除默认配置外,请取消以下注释,开启相关模块:

LoadModule ext_filter_module modules/mod_ext_filter.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so
LoadModule remoteip_module modules/mod_remoteip.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_express_module modules/mod_proxy_express.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule http2_module modules/mod_http2.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
LoadModule suexec_module modules/mod_suexec.so
LoadModule rewrite_module modules/mod_rewrite.so

在模块结尾部分添加如下内容:

<IfModule http2_module>
ProtocolsHonorOrder On
Protocols h2 http/1.1
</IfModule>

修改以下参数配置

将用户组设置为:

User www
Group www

默认值:

#ServerName www.example.com:80

修改为:

ServerName 0.0.0.0:80

注意:如果基于端口创建虚拟主机,请换行加端口,然后在虚拟主机配置文件中指定端口即可。

将配置文件中的所有:

AllowOverride None

修改为:

AllowOverride All

在以下内容位置:

AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz

添加:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

取消以下注释并添加apache支持python:

AddHandler cgi-script .cgi .py

取消以下注释:

Include conf/extra/httpd-mpm.conf
Include conf/extra/httpd-vhosts.conf
Include conf/extra/httpd-default.conf

以下内容在文件尾部添加

开启GZIP:

<IfModule mod_headers.c>
 AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript
 <FilesMatch "\.(js|css|html|htm|png|jpg|swf|pdf|shtml|xml|flv|gif|ico|jpeg)$">
 RequestHeader edit "If-None-Match" "^(.*)-gzip(.*)$" "$1$2"
 Header edit "ETag" "^(.*)-gzip(.*)$" "$1$2"
 </FilesMatch>
 DeflateCompressionLevel 6
 SetOutputFilter DEFLATE
</IfModule>

设置apache安全:

ProtocolsHonorOrder On
PidFile /usr/local/apache/logs/httpd.pid
ServerTokens ProductOnly
ServerSignature Off

引入虚拟主机配置文件:

IncludeOptional conf/vhost/*.conf

7.创建目录及虚拟主机配置文件

$ mkdir -p /usr/local/apache/conf/vhost
$ mkdir -p /data/apps/web/renwolecom
$ vim /usr/local/apache/conf/vhost/renwolecom.conf

插入以下内容:

<VirtualHost *:80>
 ServerAdmin webmaster@example.com
 DocumentRoot "/data/apps/web/renwolecom"
 ServerName www.renwole.com
 ServerAlias renwole.com
 errorDocument 404 /404.html
 ErrorLog "/usr/local/apache/logs/renwolecom-error_log"
 CustomLog "/usr/local/apache/logs/renwolecom-access_log" combined

<FilesMatch \.php$>
 SetHandler "proxy:unix:/tmp/php-cgi.sock|fcgi://localhost"
 </FilesMatch>

<Directory "/data/apps/web/renwolecom">
 SetOutputFilter DEFLATE
 Options FollowSymLinks
 AllowOverride All
 Require all granted
 DirectoryIndex index.php index.html index.htm default.php default.html default.htm
 </Directory>
</VirtualHost>

以上内容请根据自己的网站实际路径修改。

说明:如果没有域名,请将域名绑定部分修改为127.0.0.1,在http.conf中添加其他端口即可实现IP+端口访问,要记得防火墙放行端口。

注意:建议清空httpd-vhosts.conf中的内容,或者不开启引用,否则重启httpd服务时会有警告,但不影响使用,主要原因是找到默认配置中的网站目录路径。

8.创建system系统单元启动文件

$ vim /usr/lib/systemd/system/httpd.service

添加以下内容:

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/usr/local/apache/bin/apachectl
Description=LSB: starts Apache Web Server
Before=runlevel2.target
Before=runlevel3.target
Before=runlevel4.target
Before=runlevel5.target
Before=shutdown.target
After=all.target
After=network-online.target
Conflicts=shutdown.target

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop

9.加入开机自启/启动/停止/重启

$ systemctl enable httpd
$ systemctl start httpd
$ systemctl stop httpd
$ systemctl restart httpd

10.设置Firewalld防火墙

$ firewall-cmd --permanent --zone=public --add-service=http
$ firewall-cmd --permanent --zone=public --add-service=https
$ firewall-cmd --reload

接下来你就可以使用域名或IP访问你的网站了。

11.部署PHP篇

请参阅《Centos 7源码编译安装 php7.1 生产篇

12.部署Mysql篇

请参阅《Centos 7二进制安装配置 MariaDB(MySQL)数据库

Apache HTTP服务器的安装与配置至此已经结束,如果在安装配置过程中出现任何报错,请查看/usr/local/apache/logs/下的错误日志,以便你能快速解决所遇到的问题。

若还有其他指教,欢迎你的留言。