ubuntu 安装 apache2 + php5

本文对应简短文字版查看:https://www.cnblogs.com/amnotgcs/p/14018744.html

手动编译安装步骤
https://blog.csdn.net/u014261408/article/details/90084446

compile php apr/pcre error
https://www.cnblogs.com/yuzhaokai0523/p/4382974.html

compile php mcrypt error:
https://blog.csdn.net/tanga842428/article/details/76861462

md,编译安装的apache需要 sudo apachectl start 启动
使用 systemctl start apache2 启动的是什么鬼????是另一个apache2????
注意!!!!再说一遍!!
编译安装的apache在/usr/lib/apache/bin/中使用 sudo ./apachectl start 启动,
这时与该apache2绑定的php才会生效

编译安装

一、编译安装 apache2

官方文档:apache2.4安装文档(en)
官方总体流程:

Download	Download the latest release from http://httpd.apache.org/download.cgi
Extract	$ gzip -d httpd-NN.tar.gz
$ tar xvf httpd-NN.tar
$ cd httpd-NN
Configure	$ ./configure --prefix=PREFIX
Compile	$ make
Install	$ make install
Customize	$ vi PREFIX/conf/httpd.conf
Test	$ PREFIX/bin/apachectl -k start

1.1 下载 apache2

你可以从https://httpd.apache.org/download.cgi选择一个比较快的镜像点来下载。

1.2 解压源码包

1.3 ./configure

这里我们发现configure失败了。阅读官方文档得知有一些东西在编译之前是需要先存在的。

这里我的环境还没这些包,我需要安装apr,需要到apr.apache.org去下载安装。
如何安装apr参见f1.1

再次configure,发现没有apr-util,如何安装参见f1.4

configure 结果:

1.4 make && make install

su
make && make install

如果在安装的时候出现了 xml 相关错误则需要先安装 libxml2-dev (见f1.7),然后重新编译安装 apr-util 再继续安装 apache2
安装完成如下图:

1.5 启动测试

cd /usr/local/apache2
bin/apachectl -k start

如果出现了 libpcre.so 的问题,执行 ldconfig,再重新启动
如果出现了 set ServerName 的问题,将 /usr/local/apache2/conf/httpd.conf 中的 #ServerName www.example.com:80 改成 ServerName localhost:80即可(不改也可以)

wget localhost
cat index.html

使用上述命令访问主页,查看结果:

出现 It's works!即成功访问。
通过命令 ifconfig 查看自己的ip地址,在其他电脑上访问,如果正常即可。
如果ifconfig不可用,参见 f1.8 安装 net-tools
访问成功如下图:

附录:其他安装

f1.1 安装 apr

wget https://mirror.bit.edu.cn/apache//apr/apr-1.7.0.tar.gz
tar zxvf apr-1.7.0.tar.gz | tail


解压apr:

编译安装apr,发现我没有编译器:

如何安装编译器参见 f1.2

再次confugure apr:

然后 sudo make && make install (没有make的参见f1.3安装make)
如果 sudo make && make install 失败的可以尝试先su切换到root用户再执行 make && make install

再次configure 发现 pcre not found

如何安装 pcre 见f1.5

f1.2 安装 gcc

# 下载安装gcc编译器:
sudo apt install gcc

f1.3 安装 make

# 下载安装 make 程序
sudo apt install make

f1.4 安装apr-util

下载,解压

wget https://mirror.bit.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
tar zxvf apr-util-1.6.1.tar.gz


configure && make && make install

cd apr-util-1.6.1
./configure --with-apr=/usr/local/apr
sudo make && make install

如果在安装过程中报 expat not found:
centos 系统使用 sudo yum install expat-devel来安装
debain 系统使用 sudo apt install libexpat-dev来安装

f1.5 安装 pcre

可以在pcre官网 pcre.org找到下载链接

wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.44/pcre-8.44.tar.gz
tar zxvf pcre-8.44.tar.gz | tail
./configure

configure的时候发现我没有c++ compiler 如何安装 参见f.16
configure 结束:

su
make && make install

开始编译:

安装结果:

f1.6 安装 g++

sudo apt install g++ -y

f1.7 安装 libxml2-dev

当编译安装apache2的时候报如下错误:

...
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_StopParser'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_Parse'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ErrorString'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetElementHandler'
collect2: error: ld returned 1 exit status
make[2]: *** [htpasswd] 错误 1
make[2]: 离开目录“/usr/local/src/httpd-2.4.28/support”
make[1]: *** [all-recursive] 错误 1
make[1]: 离开目录“/usr/local/src/httpd-2.4.28/support”
make: *** [all-recursive] 错误 1

则需要安装 libxml2-dev
而且在安装之后需要重新编译 apr-util 才可生效以使apache2编译成功。

sudo apt install libxml2-dev

f1.8 安装 net-tools (为了使用ifconfig)

sudo apt install net-tools

二、编译安装php

参考链接:configure 参数

2.1 下载 php

搜狐镜像站http://mirrors.sohu.com/php/

2.2 编译安装

wget http://mirrors.sohu.com/php/php-5.6.40.tar.bz2
tar jxvf php-5.6.40.tar.bz2
cd php5.6.40
su
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs  #这是最基本的编译参数,如果需要数据库还需要加参数
make
make install

2.3 在apache2中添加php解析

在conf/httpd.conf中第389行左右,找到AddType添加:

AddType application/x-httpd-php .php

重启之后就可以解析php了。
可以写一个测试文件:

<?php
    date_default_timezone_set("Asia/Shanghai");
    phpinfo();
?>
![](https://img2020.cnblogs.com/blog/1604857/202011/1604857-20201122213622218-924064483.png)
原文地址:https://www.cnblogs.com/amnotgcs/p/14011543.html