CentOS7源码编译安装lamp,lnmp及其相关扩展

教程简介

本教程内容如下:

1.两个流行的Web服务器

apache 2.4.23

nginx 1.13.5

2.一个开源数据库

mysql 5.7.15

3.两个版本的PHP

PHP 5.6.24

PHP 7.1.9

4. 安装相应扩展

PHP 5.6.24以安装Redis扩展为例

PHP 7.1.9以找回PHP 7废弃的mysql扩展为例

当然不推荐使用php-mysql扩展,本教程只是演示

安装

准备安装包

apache Web服务器

httpd-2.4.23.tar.bz2

nginx Web服务器

nginx-1.13.5.tar.gz

mysql 开源数据库

mysql-5.7.15.tar.gz

php php网站源码解析器

php-5.6.24.tar.gz

cmake c语言自动化编译工具,本教程使用cmake编译mysql(高版本的mysql都是用cmake编译,以前低版本的用make就行)

cmake-3.6.2.tar.gz

libmcrypt PHP libmcrypt模块,php中用于加密的模块

libmcrypt-2.5.8.tar.gz

libvpx PHP libvpx模块,php中的vp9视频编译器

libvpx-1.6.0.zip

libpng PHP libpng模块,php中用于处理png的扩展

libpng-1.2.31.tar.gz

jpegsrc PHP jpeg模块,php中用于处理jpeg的扩展

jpegsrc.v9a.tar.gz

apr Apache库文件,Apache兼容多平台的底层支持接口库

apr-1.5.2.tar.bz2

apr-util Apache库文件,apr的工具库

apr-util-1.5.4.tar.gz

boost C++准标准库,使用cmake安装mysql时需要使用,低版本的不需要boost

boost_1_59_0.tar.gz

mysql 5.7对应的Boost版本为1.59.0,其他版本的不行(包括高版本的)。

下载地址:https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

安装基础开发环境

移除系统自带的低版本软件包

yum -y remove apr-util-devel apr apr-util-mysql apr-docs apr-devel apr-util apr-util-docs

安装编译器,链接器,构建工具,依赖的基础库,补丁工具等

yum -y install make apr autoconf automake gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm freetype libjpeg libpng php-common php-gd ncurses libtool libxml2 libxml2 libvpx libjpeg libpng libXpm patch nsd yasm

出现以下内容,不用管,继续下面的步骤安装。

 --> 处理 compat-qpid-cpp-client-0.24-19.el7.x86_64 与 qpid-cpp-client 的冲突
 --> 解决依赖关系完成
 错误:compat-qpid-cpp-client conflicts with qpid-cpp-client-1.36.0-1.el7.x86_64
 您可以尝试添加 --skip-broken 选项来解决该问题
 您可以尝试执行:rpm -Va --nofiles --nodigest

安装libmycrpt

1
2
3
4
cd libmycrypt-2.5.8
./configure
make -j
make -j install

安装libvpx

1
2
cd ../libvpx-1.6.0
./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9

不加–enable-shared在PHP编译时会遇到以下错误:

1
2
/usr/bin/ld: /usr/local/libvpx/lib/libvpx.a(vpx_codec.c.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/local/libvpx/lib/libvpx.a: could not read symbols: Bad value
1
2
make -j
make -j install

安装jpeg

1
2
3
4
cd ../jpeg-9a
./configure --prefix=/usr/local/jpeg
make -j
make -j install

##

安装libpng

1
2
3
4
cd ../libpng-1.2.31
./configure --prefix=/usr/local/libpng
make -j
make -j install

安装cmake

cd ../cmake-3.6.2
1
2
3
./configure 
make -j #这一步加-j参数极有可能出错
make -j install</pre>

make -j如果出错

 rm -f CMakeCache.txt

安装apr

1
2
3
4
cd ../apr-1.5.2
./configure --prefix=/usr/local/apr
make -j
make -j install

安装apr-util

1
2
3
4
cd ../apr-util-1.5.4
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
make -j
make -j install

安装nginx

1
2
3
4
cd ../nginx-1.13.5/
./configure --prefix=/usr/local/nginx
make -j
make -j install

安装mysql

1
2
3
groupadd mysql #添加mysql组
useradd -g mysql mysql -s /bin/false #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
mkdir -p /usr/local/mysql/ #创建MySQL安装目录

安装前将boot_1_59_0.tar.gz压缩包放在/usr/local/boost_1_59_0/目录下

1
2
3
4
[root@local boost_1_59_0]# pwd
/usr/local/boost_1_59_0
[root@local boost_1_59_0]# ls
boost_1_59_0.tar.gz
1
2
3
cd /usr/local/src/mysql-5.7.15
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFIGDIR=/usr/local/mysql/ -DWITH_BOOST=/usr/local/boost_1_59_0
make -j 7

编译mysql需要消耗大量时间,cc1plus会占用大量CPU和内存,主要是CPU,内存会占用的少一点,加上-j参数会快很多,但是配置低的话加上-j会出现CPU,内存不足等而失败。

我的用-j在55%的时候出现这个:

c++: 编译器内部错误:已杀死(程序 cc1plus)

编译的时候时候非常慢,可以看看都生成了哪些二进制可执行文件,听说Gento Linux编译需要几天。

48.1MB的.tar.gz编译完成后的文件用du -sh .统计了一下,5.0G,如果用tar打包成.tar.gz应该有600多MB,mysql官网已经编译好的Linux通用的mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz有610MB。

make -j install

安装后/usr/local/mysql大小1.9G。

配置mysql

1
2
3
4
5
6
7
cd /usr/local/mysql
mkdir data #创建MySQL数据库存放目录
chown -R mysql:mysql data #设置MySQL数据库目录权限
cp support-files/my-default.cnf my.cnf #复制Mysql配置文件
cp support-files/mysql.server /etc/rc.d/init.d/mysqld #将mysql服务器复制到相应目录方便使用系统命令管理
chmod 755 /etc/init.d/mysqld #增加执行权限
chkconfig mysqld on #加入开机启动
vim my.cnf
1
2
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
vim /etc/init.d/mysqld
1
2
3
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
user=mysql
初始化mysql
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

如果出现这个

2017-09-16T16:00:04.136525Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.

执行

1
2
rm -rf /usr/local/mysql/data/*
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

 

1
2
cd lib
ln -s libmysqlclient.so.20.3.2 libmysqlclient_r.so

解决PHP 检查环境时以下错误

1
2
configure: error: Cannot find libmysqlclient_r under /usr/local/mysql.
Note that the MySQL client library is not bundled anymore!

启动mysql及检查状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@local mysql]# systemctl start mysqld.service #启动mysql
[root@local mysql]# systemctl status mysqld.service #查看mysql状态
mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld)
Active: active (running) since 六 2017-09-16 18:02:27 CEST; 25s ago
Process: 27064 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mysqld.service
├─27080 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/local.pid
└─27276 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/maria...

9月 16 18:02:26 local systemd[1]: Starting LSB: start and stop MySQL...
9月 16 18:02:26 local mysqld[27064]: /etc/rc.d/init.d/mysqld: Zeile 48: log-error=/usr/local/mysql/data/error.log: Datei oder Verzeichnis nicht gefunden
9月 16 18:02:26 local mysqld[27064]: /etc/rc.d/init.d/mysqld: Zeile 49: pid-file=/usr/local/mysql/data/mysql.pid: Datei oder Verzeichnis nicht gefunden
9月 16 18:02:27 local mysqld[27064]: Starting MySQL. SUCCESS!
9月 16 18:02:27 local systemd[1]: Started LSB: start and stop MySQL.

安装apache

1
2
3
4
cd /usr/local/src/httpd-2.4.23
./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-ssl --enable-ssl --enable-module=so --enable-rewrite --enable-cgid --enable-cgi
make -j #编译
make -j install #安装

 

1
2
3
/usr/local/apache2/bin/apachectl -k start #启动apache
systemctl stop firewalld.service #关闭防火墙
setenforce 0 #关闭SELiux

打开浏览器输入ip地址,将看到以下内容

It works!

如果出错,检查一下哪里的权限有问题。

配置apache

1
2
cd /usr/local/apache2
vim conf/httpd.conf

找到AllowOverride None

修改为:AllowOverride All #开启apache支持伪静态,有两处都做修改

找到:Options Indexes FollowSymLinks

修改为:Options FollowSymLinks #不显示目录结构

找到:DirectoryIndex index.html

修改为:DirectoryIndex index.html index.php

<Files “.ht*”>

Require all denied

</Files>

修改为

<Files “.ht*”>

Require all granted

</Files>

LoadModule rewrite_module modules/mod_rewrite.so #取消前面的#号,开启apache支持伪静态

ServerName 0.0.0.0:80

解决启动/关闭/重启时的警告

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1\. Set the 'ServerName' directive globally to suppress this message

 

1
2
3
4
cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd #将apache服务器管理程序复制到相应目录方便使用系统命令管理 
chmod 700 /usr/local/apache2/htdocs -R #更改apache网站目录权限
chown daemon.daemon -R /usr/local/apache2/htdocs #更改目录所有者
vim /etc/rc.d/rc.local #设置apache开机自启动

 

/usr/local/apache2/bin/apachectl  start

 

chmod +x /etc/rc.d/rc.local

 

安装PHP5.6.24

cd /usr/local/src/php-5.6.24

./configure –prefix=/usr/local/php5 –with-config-file-path=/usr/local/php5/etc –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql=/usr/local/mysql –with-mysqli=/usr/local/mysql/bin/mysql_config –with-mysql-sock=/tmp/mysql.sock –with-gd –with-iconv –with-freetype –with-jpeg –with-zlib –with-libxml –enable-xml –enable-discard-path –enable-magic-quotes –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curlwappers –enable-mbregex –enable-fastcgi –enable-force-cgi-redirect –enable-mbstring –enable-ftp –enable-gd-native-ttf –with-openssl –enable-pcntl –enable-sockets –with-xmlrpc –enable-zip –enable-soap –enable-pear –with-pear –with-gettext –with-mine-magic –enable-suhosin –enable-session –with-mcrypt –enable-fpm –with-jpeg –with-vpx-dir=/usr/local/libvpx –with-jpeg-dir=/usr/local/jpeg –with-png –with-png-dir=/usr/local/libpng

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[root@local php-5.6.24]# ./configure --prefix=/usr/local/php5 \
> --with-config-file-path=/usr/local/php5/etc \
> --with-apxs2=/usr/local/apache2/bin/apxs \
> --with-mysql=/usr/local/mysql \
> --with-mysqli=/usr/local/mysql/bin/mysql_config \
> --with-mysql-sock=/tmp/mysql.sock \
> --with-gd \
> --with-iconv \
> --with-freetype \
> --with-jpeg \
> --with-png \
> --with-zlib \
> --with-libxml \
> --enable-xml \
> --enable-discard-path \
> --enable-magic-quotes \
> --enable-safe-mode \
> --enable-bcmath \
> --enable-shmop \
> --enable-sysvsem \
> --enable-inline-optimization \
> --with-curlwappers \
> --enable-mbregex \
> --enable-fastcgi \
> --enable-force-cgi-redirect \
> --enable-mbstring \
> --enable-ftp \
> --enable-gd-native-ttf \
> --with-openssl \
> --enable-pcntl \
> --enable-sockets \
> --with-xmlrpc \
> --enable-zip \
> --enable-soap \
> --enable-pear \
> --with-pear \
> --with-gettext \
> --with-mine-magic \
> --enable-suhosin \
> --enable-session \
> --with-mcrypt \
> --enable-fpm \
> --with-vpx-dir=/usr/local/libvpx \
> --with-jpeg-dir=/usr/local/jpeg \
> --with-jpeg-dir=/usr/local/libpng

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
configure: WARNING: unrecognized options: --with-freetype, --with-jpeg, --with-libxml, --enable-discard-path, --enable-magic-quotes, --enable-safe-mode, --with-curlwappers, --enable-fastcgi, --enable-force-cgi-redirect, --enable-pear, --with-mine-magic, --enable-suhosin, --with-jpeg, --with-png

最后有一个警告,不用管它,生成了Makefile文件就行。

make -j 
make -j test #这一步可以省略
=====================================================================
WARNED TEST SUMMARY
---------------------------------------------------------------------
zend multibyte (2) [ext/mbstring/tests/zend_multibyte-02.phpt] (warn: XFAIL section but test passes)
zend multibyte (6) [ext/mbstring/tests/zend_multibyte-06.phpt] (warn: XFAIL section but test passes)
zend multibyte (8) [ext/mbstring/tests/zend_multibyte-08.phpt] (warn: XFAIL section but test passes)
zend multibyte (10) [ext/mbstring/tests/zend_multibyte-10.phpt] (warn: XFAIL section but test passes)
zend multibyte (11) [ext/mbstring/tests/zend_multibyte-11.phpt] (warn: XFAIL section but test passes)
Phar: bug #69958: Segfault in Phar::convertToData on invalid file [ext/phar/tests/bug69958.phpt] (warn: XFAIL section but test passes)
Bug #69111 Crash in SessionHandler::read() [ext/session/tests/bug69111.phpt] (warn: XFAIL section but test passes)
Bug #70172 - Use After Free Vulnerability in unserialize() [ext/standard/tests/serialize/bug70172.phpt] (warn: XFAIL section but test passes)
=====================================================================

You may have found a problem in PHP.
This report can be automatically sent to the PHP QA team at
http://qa.php.net/reports and http://news.php.net/php.qa.reports
This gives us a better understanding of PHP's behavior.
If you don't want to send the report immediately you can choose
option "s" to save it. You can then email it to qa-reports@lists.php.net later.
Do you want to send this report now? [Yns]: Y

这里我给PHP提交了一个bug,这个不影响,继续安装

make -j install

 

1
2
[root@local php-5.6.24]# du -sh . #这个目录417M
417M .

配置PHP

1
2
3
cp php.ini-production /usr/local/php5/etc/php.ini #复制php配置文件到安装目录
cd /usr/local/php5
vim /etc/php.ini

找到:;date.timezone =

修改为:date.timezone = PRC #设置时区

1
2
cp etc/php-fpm.conf.default etc/php-fpm.conf #拷贝模板文件为php-fpm配置文件
vim etc/php-fpm.conf

user = www #设置php-fpm运行账号为www

group = www #设置php-fpm运行组为www

pid = run/php-fpm.pid #取消前面的分号