一、ubuntu

httpd二进制安装

1.安装依赖环境

sudo apt update

sudo apt install build-essential(默认的 Ubuntu 软件源包含了一个软件包组,名称为 "build-essential",它包含了 GNU 编辑器集合,GNU 调试器,和其他编译软件所必需的开发库和工具。这个命令将会安装一系列软件包,包括gcc,g++,和make。)

apt-get install libexpat1-dev

apt-get install libpcre3 libpcre3-dev

sudo apt-get install openssl

sudo apt-get install libssl-dev

下载apr并解压安装

wget https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz

tar -zxvf apr-1.6.5.tar.gz 

进入解压后的文件执行

./configure --prefix=/usr/local/apr(只是安装路径自定义即可)

make&&make install

下载apr-util并解压安装

wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz

tar -zxvf apr-util-1.6.1.tar.gz

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config              //这里with将其关联起来,解决安装的依赖关系

make&&make install

下载安装pcre并解压安装

apt-get install libpcre3 libpcre3-dev(如上执行这句后无需再用源码安装)

wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz/download

tar -zxvf pcre-8.45.tar.gz

./configure --prefix=/usr/local/pcre --with-apr=/usr/local/apr/bin/apr-1-config

安装httpd

下载httpd并解压(wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.53.tar.gz)

tar -zxvf httpd-2.4.53.tar.gz

./configure --prefix=/usr/local/httpd --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util

make&&make install

注意:

报错1: rm: cannot remove 'libtoolT': No such file or directory

解决问题

vim configure

找到RM='$RM'修改为RM='$RM -f'

报错2: expat.h: No such file or directory……的问题

解决问题

apt-get install libexpat1-dev  

后续操作跟下面一样

二、centos

依赖解决

https://centos.pkgs.org/

1.安装依赖

yum install gcc pcre-devel binutils glibc openssl-devel expat-devel

2.获取软件包

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.3.tar.gz

wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.56.tar.gz

3.解压

tar -zxvf apr-1.6.5.tar.gz

tar -zxvf apr-util-1.6.3.tar.gz

tar -zxvf httpd-2.4.54.tar.gz

3.编译安装

apr安装

cd apr-1.6.5.tar.gz

./configure --prefix=/usr/local/apr #配置apr安装目录

make && make install

报错:

rm: cannot remove 'libtoolT': No such file or directory

vim configure

找到RM='$RM'修改为RM='$RM -f'

重新编译即可

```

apr-util安装

```

cd ../apr-util-1.6.3

./configure \

--prefix=/usr/local/apr-util \ #指定apr-util安装目录

--with-apr=/usr/local/apr #指定依赖的apr目录

make && make install

```

httpd安装

```

cd ../httpd-2.4.54

./configure \

--with-apr=/usr/local/apr \ #指定依赖的apr目录

--with-apr-util=/usr/local/apr-util \ #指定依赖的apr-util目录

--prefix=/usr/local/apache \ #指定安装目录

--sysconfdir=/etc/httpd24 \ #指定配置文件路径

--enable-so \ #启动模块动态装卸载

--enable-ssl \ #编译ssl模块

--enable-cgi \ #支持cgi机制

--enable-rewrite \ #支持url重写

--with-zlib \ #支持数据包压缩

--with-pcre \ #支持正则表达式

--with-mpm=prefork \ #指定httpd的工作方式为prefork

--enable-modules=most \ #启用的模块

--enable-mpms-shared=all #支持三种工作模式

```

隐藏版本号(修改自定义)

vim /opt/httpd-2.4.54/include/ap_release.h

![image-1678031687538](https://zmzycc.top/upload/2023/03/image-1678031687538.png)

```

./configure --prefix=/etc/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-cgi --enable-rewrite --enable-ssl --with-mpm=prefork --with-pcre --with-zlib

```

编译安装

```

make && make instal

```

三、配置apache

vim /etc/appche/conf/httpd.conf

找到ServerName将注释去掉,并 ServerName改为:localhost:80然后更改保存退出

vim /etc/systemd/system/httpd.service

```

[Unit]

Description=Apache 2.5

After=network.target

After=syslog.target

[Service]

Type=forking

ExecStart=/etc/apache/bin/apachectl -k start

ExecStop=/etc/apache/bin/apachectl -k stop

ExecReload=/etc/apache/bin/apachectl -k restart

[Install]

WantedBy=multi-user.target

Alias=http.service

```

打开apache服务后访问IP查看配置是否正确:

/usr/local/apache/bin/apachectl start

apachectl -t --检查配置文件是否正确Syntax OK

apachectl restart --重新启动apache检查虚拟主机是否运行

systemctl start httpd.service

systemctl enable httpd.service

修改主配置文件httpd.conf

```

去掉#LoadModule ssl_module modules/mod_ssl.so 前的#号

去掉#Include conf/extra/httpd-ssl.conf 前的#号

去掉#LoadModule rewrite_module modules/mod_rewrite.so 前面的#

去掉#LoadModule socache_shmcb_module

modules/mod_socache_shmcb.so 前面的#

去掉#Include conf/extra/httpd-vhosts.conf 前面的#

去掉#LoadModule proxy_module modules/mod_proxy.so 前面的#

去掉#LoadModule proxy_http_module modules/mod_proxy_http.so 前面的#

```

第二种方式通过fcgi调用php

去掉#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

前面的#

虚拟主机这样配置

```

<VirtualHost *:2000>

DocumentRoot "/opt"

ServerName 192.168.0.135

ErrorLog "logs/pma.com-error_log"

CustomLog "logs/pma.com-access_log" combined

ProxyRequests Off

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/opt

<Directory "opt">

Options None

AllowOverride None

Require all granted

</Directory>

</VirtualHost>

搜索AddType,并在配置文件底部添加下列内容。

vim /etc/httpd.conf

AddType application/x-httpd-php .php

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

```

虚拟主机的端口也必须在httpd里面配置监听端口,否则不生效。比如上述2000端口就的在httpd.conf添加

```

Listen 2000

```

httpd-vhosts.conf 配置多台虚拟机

```

<VirtualHost *:80>

DocumentRoot "/opt/"

ServerName like.zmzycc.com

DirectoryIndex index.php index.htm index.html

<Directory /opt/>

Options Includes ExecCGI FollowSymLinks

Options FollowSymLinks

RewriteEngine on

RewriteCond %{SERVER_PORT} !^443$

RewriteRule ^(.*)?$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

AllowOverride All

Require all granted

</Directory>

ErrorLog "logs/dummy-host2.like.zmzycc.com-error_log"

CustomLog "logs/dummy-host2.like.zmzycc.com-access_log" common

</VirtualHost>

```

httpd-ssl.conf 配置ssl

```

<VirtualHost *:443>

ServerName like.zmzycc.com:443

DocumentRoot "/opt/"

SSLEngine on

SSLCertificateFile "/etc/apache/ssl/cert.pem"

SSLCertificateKeyFile "/etc/apache/ssl/key.pem"

SSLCertificateChainFile "/etc/apache/ssl/fullchain.pem"

</VirtualHost>

```

第一种通过php编译完成,apache引进php模块

```

去掉#LoadModule actions_module modules/mod_actions.so

```

搜索AddType,并在配置文件底部添加下列内容。

vim /etc/httpd.conf

```

AddType application/x-httpd-php .php

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

```

隐藏版本

vim httpd-default.conf

```

ServerTokens Prod

ServerSignature off

```

开启gzip压缩

```

去掉#LoadModule deflate_module modules/mod_deflate.so 前面的#

去掉#LoadModule headers_module modules/mod_headers.so 前面的#

```

vim httpd.conf

```

<Ifmodule mod_deflate.c>

DeflateCompressionLevel 9

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php

AddOutputFilter DEFLATE js css

</Ifmodule>

```