web-apache安装配置

ref

centos7 升级到最新版

先确认openssl是否是最新版,需要支持https。apache现在最新版是2.4.33版本

卸载当前版本

sudo yum remove httpd -y

下载源码

sudo yum install -y epel-release

安装编译依赖
sudo yum install autoconf expat-devel libtool libnghttp2-devel pcre-devel -y

下载源码
Apache httpd - https://github.com/apache/httpd/releases
APR - https://github.com/apache/apr/releases
APR-util https://github.com/apache/apr-util/releases

curl -O -L https://github.com/apache/httpd/archive/2.4.33.tar.gz
curl -O -L https://github.com/apache/apr/archive/1.6.3.tar.gz
curl -O -L https://github.com/apache/apr-util/archive/1.6.1.tar.gz

tar -zxvf 2.4.29.tar.gz
tar -zxvf 1.6.3.tar.gz
tar -zxvf 1.6.1.tar.gz

编译源码

httpd编译的时候需要apr和apr-utils,所以要放到指定的目录下
cp -r apr-1.6.3 httpd-2.4.33/srclib/apr
cp -r apr-util-1.6.1 httpd-2.4.33/srclib/apr-util
注意目标目录不要改动。

编译源码
cd httpd-2.4.33
./buildconf
./configure --enable-ssl --enable-so --enable-http2 --with-mpm=event --with-included-apr --with-ssl=/usr/local/openssl --prefix=/usr/local/apache2
make

configure 参数说明
--enable-ssl will build Apache with SSL support, so you can enable HTTPS on your websites.
--enable-so will enable dynamically loaded modules. So you can enable and disable modules without recompilation (I will describe modules in configuration part)
--enable-http2 will enable HTTP/2 support.
--with-mpm will set multiprocessing modules for Apache. I'm using event, but you can use worker or prefork instead. event works best for me and I think that it is mpm that will give you most performance.
--with-included-apr It will use APR library that you copied to srclib directory
--with-ssl will point compiler to newer version of OpenSSL. Make sure that you compiled it first!
--prefix is the installation path for Apache httpd compiled package

编译需要一段时间,看你服务器的处理器速度了。

安装
sudo make install

清理安装包和源码目录。

设置

添加到环境变量
sudo nano /etc/profile.d/httpd.sh
添加如下内容:
pathmunge /usr/local/apache2/bin

登出后在登入,检查版本
httpd -v

增加系统入口使用systemctl命令
sudo nano /etc/systemd/system/httpd.service

加入下面内容:
[Unit]
Description=The Apache HTTP Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl -k start
ExecReload=/usr/local/apache2/bin/apachectl -k graceful
ExecStop=/usr/local/apache2/bin/apachectl -k graceful-stop
PIDFile=/usr/local/apache2/logs/httpd.pid
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重新载入
sudo systemctl daemon-reload

sudo systemctl start httpd

设置代理用户

创建代理用户
sudo groupadd www
sudo useradd httpd -g www --no-create-home --shell /sbin/nologin

配置参数

修改默认端口为8080
找到apache的配置文件目录

sudo nano /usr/local/apache2/conf/httpd.conf

Listen 127.0.0.1:8080

# Set ServerName to prevent warning on Apache start
ServerName localhost

# Set user and group
User httpd
Group www

# Configure entry file for your application. If you plan to use PHP make sure that it's as first possible file
DirectoryIndex index.php index.html

# If you are using PHP with PHP-FPM which I highly suggest enable proxy modules
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

# Enable pretty links and mod_rewrite that is highly used in all frameworks and CMSes
LoadModule rewrite_module modules/mod_rewrite.so

# Useful for WordPress sites - enables Require for setting up access to given resources.
LoadModule access_compat_module modules/mod_access_compat.so

# One more useful thing for WordPress and Let's Encrypt - enables Alias. If you are using composer and wpackagist it's a must, otherwise if you don't plan to use aliases, leave that disabled
LoadModule alias_module modules/mod_alias.so

# Enable gzip extension for compressing static files
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so

# Enable expires header for caching assets on browser side
LoadModule expires_module modules/mod_expires.so

# Enable SSL
LoadModule http2_module modules/mod_http2.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so

# Enable status module for monitoring Apache. If you don't plan to use that, leave that commented out
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule status_module modules/mod_status.so

LoadModule cgid_module modules/mod_cgid.so

Setup GZIP compression

sudo nano /usr/local/apache2/conf/extra/httpd-deflate.conf

写入如下内容:

<IfModule mod_deflate.c>
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/ecmascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
</IfModule>
</IfModule>

在配置文件最下面的include位置添加,保存后重启

Include conf/extra/httpd-deflate.conf

Setup assets caching with expires headers

sudo nano /usr/local/apache2/conf/extra/httpd-expires.conf

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On

# Default directive for types not specified below
ExpiresDefault "access plus 1 week"

# Expirations for given mime type
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/ico "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
</IfModule>

Include conf/extra/httpd-expires.conf

网站虚拟目录

centOs应该在/var/www

确保权限

sudo chmod 755 /var/www
sudo chown httpd:www /var/www

创建网站目录,域名为study.com

sudo mkdir /var/www/study.com
放网页文件
sudo mkdir /var/www/study.com/htdocs
放日志
sudo mkdir /var/www/study.com/logs

设置用户组

sudo chown root:root /var/www/study.com
sudo chown httpd:www /var/www/study.com/htdocs
sudo chown httpd:www /var/www/study.com/logs

设置权限

sudo chmod 755 /var/www/study.com
sudo chmod 2775 /var/www/study.com/htdocs
sudo chmod 2775 /var/www/study.com/logs

前面的2表示新创建的子目录用户组和父目录一致(httpd:www)

创建虚拟主机配置

sudo nano /usr/local/apache2/conf/extra/vhost-study.com.conf

内容如下,根据实际情况调整:

<VirtualHost *:80>

ServerName study.com
# 可以指定多个域名
ServerAlias www.study.com www.gd.com
# Directory settings
DocumentRoot /var/www/study.com/htdocs
<Directory /var/www/study.com/htdocs>
AllowOverride All
Require all granted
Options +FollowSymLinks -Indexes -Includes
</Directory>

# Logging
ErrorLog "/var/www/study.com/logs/httpd-error.log"
CustomLog "/var/www/study.com/logs/httpd-access.log" common

</VirtualHost>

写入主配置文件
sudo nano /usr/local/apache2/conf/httpd.conf
Include conf/extra/vhost-study.com.conf

支持PHP

找到
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
后面添加
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .php7

这个没有找到
//LoadModule php7_module modules/libphp7.so

问题

查看fcgid模块是否支持
httpd -M | grep fcgid
fcgid_module (shared)

查看使用的php-fpm模式
httpd -V

Server MPM:event

MPM(Multi -Processing Modules,多重处理模块)是Apache的核心组件之一,Apache通过MPM来使用操作系统的资源,对进程和线程池进行管理。

启动、自启动

sudo systemctl start httpd
自启动
sudo systemctl enable httpd

重启

sudo systemctl restart httpd

停止

sudo systemctl stop httpd
systemctl restart nginx.service

查看状态

sudo systemctl status httpd

centos7 安装

sudo yum clean all
sudo yum -y update
sudo yum -y install httpd

防火墙

sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --reload

查看版本

httpd -v