Apache

Apache

伍陆柒 386 2022-05-15

编译安装

解决依赖问题

apr

wget https://dlcdn.apache.org//apr/apr-1.7.0.tar.gz
tar -zxvf apr-1.7.0.tar.gz
cd apr-1.7.0
./configure --prefix=/usr/local/apr/apr
make 
sudo make install

apr-util

wget https://dlcdn.apache.org//apr/apr-util-1.6.1.tar.gz
tar -zxvf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1
./configure --prefix=/usr/local/apr/util --with-apr=/usr/local/apr/apr
make
sudo make install

pcre

wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.44/pcre-8.44.tar.gz
tar -zxvf pcre-8.44.tar.gz
cd pcre-8.44
./configure #直接使用默认安装地址,我这边好像指定位置会报找不到
make
sudo make install

下载Apache

wget https://dlcdn.apache.org/httpd/httpd-2.4.53.tar.gz
tar -zxvf httpd-2.4.53.tar.gz
cd httpd-2.4.53

编译安装

./configure --prefix=/usr/local/apache \ #指定安装目录
--with-apr=/usr/local/apr/apr \ #指定apr位置
--with-apr-util=/usr/local/apr/util #指定apr-util位置
#若出现pcre找不到再加上:--with-pcre=/usr/local/pcre
make #编译
make install #安装
/usr/local/apache/bin/apachectl start #启动,可以将命令ln -s 到/usr/bin

在线安装

yum -y install httpd
rpm  -qa  |grep  httpd #检查是否安装成功

配置文件

主要是在httpd.conf中,在线安装的自行百度

  • vim /usr/local/apache/conf/httpd.conf
  • 默认监听端口和默认网站目录
#默认监听端口和默认网站目录
Define HTTP_PORT "80"
Define SSL_PORT "443"
Define SRVROOT "D:/Apache"
Define DOCROOT "${SRVROOT}/htdocs"

#新增监听端口
#Listen 1234
  • 打开反向代理模块
#打开反向代理模块,按需打开
#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_http2_module modules/mod_proxy_http2.so
  • 修改运行用户,及用户组
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.  
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.

User apache
Group apache
#修改后记得去创建真实的用户和组,不然报错

自定义Web配置

  • conf/下新建配置文件夹myweb
cd /usr/local/apache/conf
mkdir myweb && cd myweb
  • apache默认配置文件来加载自定义配置
vim /usr/local/apache/conf/httpd.conf

#末尾追加
#可以使用绝对路径,也可以使用相对路径
#如果使用相对路径,则它相对于ServerRoot
IncludeOptional conf/myweb/*.conf
  • 新建配置touch xxx.conf
    <VirtualHost *:80>
        ServerAdmin webmaster@example.com 
        DocumentRoot "F:/Code_Blog"
        ServerName 4ee7040b.blog.ethreal.cn
        ServerAlias blog.ethreal.cn

        #反向代理规则,需要开启proxy相关模块
        <IfModule mod_proxy.c>
            ProxyRequests Off
            SSLProxyEngine On
            ProxyPass / http://127.0.0.1:9901/
            ProxyPassReverse / http://127.0.0.1:9901/
        </IfModule>

        #PATH
        <Directory "F:/Code_Blog">
            SetOutputFilter DEFLATE
            Options FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>

配置解释

  • VirtualHost *:80:入口端口
  • ServerAdmin:网站管理者邮箱
  • DocumentRoot:网站根目录
  • ServerName:唯一标识一个网站
  • ServerAlias:网站别名
  • Directory:标识目录下的权限,注意是绝对路径

剩下的用到再说:httpd配置文件httpd.conf规则说明

开启SSL配置

  • 打开模块,编辑安装路径conf目录下httpd.conf文件
#去掉则前面的注释,如果没找到则通过yum命令安装
yum -y install mod_ssl
#LoadModule ssl_module modules/mod_ssl.so
LoadModule ssl_module modules/mod_ssl.so
  • 检查上面文件中是否有以下字段
# Secure (SSL/TLS) connections
Include conf/extra/httpd-ssl.conf
#
# Note: The following must must be present to support
#       starting without SSL on platforms with no /dev/random equivalent
#       but a statically compiled-in mod_ssl.
#
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
  • 编辑网站conf配置文件,添加上证书位置
#路径后面的注释记得去掉!!
SSLCertificateFile   /etc/httpd/ssl/www.***.cn.crt        #填写证书文件路径
SSLCertificateKeyFile   /etc/httpd/ssl/www.***.cn.key     #填写私钥文件路径
SSLCertificateChainFile   /etc/httpd/ssl/root_bundle.crt   #填写证书链文件路径