源码编译LNMP

源码编译安装LNMP架构环境

先下载nmp三件套

wget http://nginx.org/download/nginx-1.12.0.tar.gz

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz

wget http://tw1.php.net/get/php-5.6.30.tar.gz/from/this/mirror -O php-5.6.30.tar.gz

编译安装Nginx

1、建立Nginx的程序目录

1
2
3
[root@CentOS7 ~]# mkdir -pv /opt/nginx
mkdir: 已创建目录 "/opt/nginx"
[root@CentOS7 ~]#

2、 安装依赖包

1
[root@CentOS7 opt]# yum -y install openssl-devel pcre-devel zlib-devel

3、创建nginx用户和组

1
[root@CentOS7 opt]# useradd -U nginx -s /sbin/nologin

前戏做足,接下来开始编译了,啊啊啊~~~~~

4、进入到nginx源码目录

1
2
[root@CentOS7 opt]# cd nginx-1.12.0/
[root@CentOS7 nginx-1.12.0]#

输入以下编译命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
./configure --prefix=/opt/nginx \
--with-select_module \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--with-stream \
--with-stream_ssl_module \

敲两下回车

如下输出:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
checking for OS
+ Linux 3.10.0-514.16.1.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... not found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for sched_setaffinity() ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_RECVDSTADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for kqueue AIO support ... not found
checking for Linux AIO support ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
+ using threads
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library

nginx path prefix: "/opt/nginx"
nginx binary file: "/opt/nginx/sbin/nginx"
nginx modules path: "/opt/nginx/modules"
nginx configuration prefix: "/opt/nginx/conf"
nginx configuration file: "/opt/nginx/conf/nginx.conf"
nginx pid file: "/opt/nginx/logs/nginx.pid"
nginx error log file: "/opt/nginx/logs/error.log"
nginx http access log file: "/opt/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

[root@CentOS7 nginx-1.12.0]# make -j4 && make install

好,接下来/opt/nginx 目录下已经有文件集目录生成了,运行./sbin/nginx -V看看输出啥,如下:

1
2
3
4
5
6
7
[root@CentOS7 sbin]# ./nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --with-select_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-stream --with-stream_ssl_module
[root@CentOS7 sbin]#

嗦嘎,编译成功!

运行试试,记得配置防火墙规则,如果你要关掉防火墙也可以

[root@CentOS7 html]# systemctl stop firewalld.service
[root@CentOS7 html]# systemctl disable firewalld.service

上边两条命令拿去不谢。

开机ngixn还要启动啊,init脚本得做好咧,如下:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
# nginx This shell script takes care of starting and stopping
# nginx
#
# chkconfig: - 13 68
# description: nginx is a web server
### BEGIN INIT INFO
# Provides: $named
# Short-Description: start|stop|status|restart|configtest
### END INIT INFO
#variables
NGINX_BIN="/opt/nginx/sbin/nginx"
NGINX_CONF="/opt/nginx/conf/nginx.conf"
NGINX_PID="/opt/nginx/logs/nginx.pid"
NETSTAT="/bin/netstat"
alter=$1
prog=nginx
#load system function
. /etc/rc.d/init.d/functions
#function:echo ok or error
function if_no {
if [ $2 == 0 ]; then
echo -n $"$1 ${prog}:" && success && echo
else
echo -n $"$1 ${prog}:" && failure && echo
fi
}
#start nginx
function start {
if [ -s ${NGINX_PID} ]; then
echo "nginx already running"
else
if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
${NGINX_BIN} -c ${NGINX_CONF}
if_no start $?
else
${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1 > ${NGINX_PID}
if_no start $?
fi
fi
}
#stop nginx
function stop {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -QUIT
if_no stop $?
else
if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
if_no stop 0
else
rm -f ${NGINX_PID} 2>/dev/null
kill `${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1`
if_no stop $?
fi
fi
}
function restart {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -HUP
if_no restart $?
else
stop
sleep 1
start
fi
}
function status {
${NETSTAT} -tnpl | grep nginx | grep LISTEN
[ $? == 0 ] && echo "nginx is running" || echo "nginx is not running"
}
function configtest {
${NGINX_BIN} -t
}
case $alter in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
configtest)
configtest
;;
*)
echo "use:${NGINX} {start|stop|restart|status|configtest}"
;;
esac

脚本根据自己的Nginx安装路径设置好,加上可执行权限,放到/etc/init.d/目录下,之后使用下面两行命令开机启动:

1
2
[root@CentOS7 init.d]# chkconfig --add nginx 
[root@CentOS7 init.d]# chkconfig nginx on

Nginx的安装配置就到这里啦

源码编译安装MySQL

参考资料

Installing MySQL from Source

https://dev.mysql.com/doc/refman/5.7/en/source-installation.html

How to Get MySQL

https://dev.mysql.com/doc/refman/5.7/en/getting-mysql.html

Building MySQL Server with CMake

https://dev.mysql.com/doc/internals/en/cmake.html

港真,看官方文档很重要,会给你指点迷津,少走弯路~~~~~

开始啦,之前已经下载好源码包mysql-boost-5.7.18.tar.gz ,开始脱(jie)衣(ya)服(suo),开搞!啊啊啊~~~

不过要注意哦,官网文档有这么一段“温馨提示”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Source Installation System Requirements

Installation of MySQL from source requires several development tools. Some of these tools are needed no matter whether you use a standard source distribution or a development source tree. Other tool requirements depend on which installation method you use.

To install MySQL from source, the following system requirements must be satisfied, regardless of installation method:

CMake, which is used as the build framework on all platforms. CMake can be downloaded from http://www.cmake.org.

A good make program. Although some platforms come with their own make implementations, it is highly recommended that you use GNU make 3.75 or higher. It may already be available on your system as gmake. GNU make is available from http://www.gnu.org/software/make/.

A working ANSI C++ compiler. See the description of the FORCE_UNSUPPORTED_COMPILER. option for some guidelines.

The Boost C++ libraries are required to build MySQL (but not to use it). Boost 1.59.0 must be installed. To obtain Boost and its installation instructions, visit the official site. After Boost is installed, tell the build system where the Boost files are located by defining the WITH_BOOST option when you invoke CMake. For example:

shell> cmake . -DWITH_BOOST=/usr/local/boost_1_59_0

Adjust the path as necessary to match your installation.

Sufficient free memory. If you encounter problems such as “internal compiler error” when compiling large source files, it may be that you have too little memory. If compiling on a virtual machine, try increasing the memory allocation.

Perl is needed if you intend to run test scripts. Most Unix-like systems include Perl. On Windows, you can use a version such as ActiveState Perl.

为了照顾英文不好的同学,我简单说几句

1、必须安装cmake,新版的MySQL采用cmak来编译。

2、linux系统中的make版本最少在3.75及以上

3、linux必须有C++编译器

4、必须安装Boost 1.59.0,指定的1.59.0版本,其他的高版本、低版本都不行,就这一个版本

下载boost1.59

[root@CentOS7 opt]# yum remove boost-1.53.0-26.el7.x86_64 #删除系统自带的低版本boost
[root@CentOS7 opt]# wget https://dl.bintray.com/boostorg/release/1.59.0/source/boost_1_59_0.tar.gz #下载新版本的boost

创建mysql用户、用户组及目录

[root@CentOS7 /]# mkdir -pv /opt/mysql5.7/{conf,data}
mkdir: created directory /opt/mysql5.7' mkdir: created directory/opt/mysql5.7/conf’
mkdir: created directory `/opt/mysql5.7/data’

[root@CentOS7 /]# groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql #增加mysql用户及用户组

安装依赖包保平安

yum install zlib-devel ncurses ncurses-devel bison

进入源码目录编译安装

[root@CentOS7 opt]# cd mysql-5.7.18/
[root@CentOS7 mysql-5.7.18]#

输入以下编译参数:

cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql5.7 -DMYSQL_DATADIR=/opt/mysql5.7/data -DSYSCONFDIR=/opt/mysql5.7/conf -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/opt/mysql5.7/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLED_LOCAL_INFILE=1 -DWITH_BOOST=/opt/tools -DENABLE_DOWNLOADS=1 -DDOWNLOAD_BOOST=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNODB_MEMCACHED=on

敲回车

等一会儿,看到最后一句 – Build files have been written to: /opt/mysql-5.7.18 ,哈哈,OK。

PS:这里有个坑,官方文档说的不是很清楚,boost1.59的压缩包下载下来后,-DWITH_BOOST的设置为压缩包所在的目录就行,也不用解压,例如我的boost1.59的压缩包放在/opt路径下,我这里就设置为-DWITH_BOOST=/opt

下一步就make -j 8 && make install

嘿嘿,我用的是Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz

无惊无险,编译完成,接下来就是要做初始化啦

官方MySQL5.7文档里面有这么一段话:

After installing MySQL, you must initialize the data directory, including the tables in the mysql system database.
在安装MySQL,您必须初始化数据目录,包括MySQL系统数据库中的表。

As of MySQL 5.7.6, use the server to initialize the data directory:
自MySQL 5.7.6起,使用MySQL服务器初始化数据目录:

命令例子
shell> bin/mysqld –initialize –user=mysql

Before MySQL 5.7.6, use mysql_install_db:
在MySQL 5.7.6之前,使用mysql_install_db:

命令例子
shell> bin/mysql_install_db –user=mysql

OK,我这里采用的是mysqld –initialize来做初始化,哈哈哈哈哈哈哈~~~~~~~

进入MySQL应用目录
[root@CentOS7 ~]# cd /opt/mysql5.7/
[root@CentOS7 mysql5.7]#

初始化MySQL

1
2
3
4
5
6
7
8
9
10
[root@CentOS7 mysql5.7]# ./bin/mysqld --initialize --user=mysql --basedir=/opt/mysql5.7 --datadir=/opt/mysql5.7/data
2017-06-12T05:41:26.221750Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-06-12T05:41:26.350566Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-06-12T05:41:26.375260Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-06-12T05:41:26.429865Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: c6bc1e39-4f31-11e7-ac7a-000c29bf2761.
2017-06-12T05:41:26.430714Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-06-12T05:41:26.431659Z 1 [Note] A temporary password is generated for root@localhost: kTsP_fwzx9zY
[root@CentOS7 mysql5.7]# date
2017年 06月 12日 星期一 13:41:36 CST
[root@CentOS7 mysql5.7]#

会自动完成初始化,并生成一个临时的密码,如上。

上边有个报警 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).

我是乖乖仔,照做,删除掉data目录下初始化生成的文件,再来一波:

1
2
3
4
5
6
7
8
9
10
[root@CentOS7 mysql5.7]# rm -rf data/*
[root@CentOS7 mysql5.7]# ll data/
总用量 0
[root@CentOS7 mysql5.7]# ./bin/mysqld --initialize --user=mysql --basedir=/opt/mysql5.7 --datadir=/opt/mysql5.7/data --explicit_defaults_for_timestamp
2017-06-12T07:04:15.856329Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-06-12T07:04:15.877492Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-06-12T07:04:15.931186Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 58c9d7b2-4f3d-11e7-a336-000c29bf2761.
2017-06-12T07:04:15.931749Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-06-12T07:04:15.932352Z 1 [Note] A temporary password is generated for root@localhost: l>58J50te/vw
[root@CentOS7 mysql5.7]#

报警没有了,嘿嘿。

编辑配置文件,添加以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[client]
port=3306
socket=/tmp/mysql.sock
default-character-set=utf8

[mysqld]
basedir =/usr/local/mysql
datadir =/mydata/data
port =3306
server_id =1
socket =/tmp/mysql.sock
pid-file=/mydata/data/mysql.pid

bind-address=localhost
#skip-grant-tables

拷贝配置文件
cp support-files/my-default.cnf /opt/mysql5.7/conf/my.cnf

拷贝启动脚本
cp support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

编辑启动脚本
主要编辑basedir和datadir这两项

chkconfig –add mysqld
chkconfig mysqld on

[root@CentOS7 opt]# service mysqld start
Starting MySQL. [ 确定 ]

[root@CentOS7 opt]# service mysqld status
MySQL running (104746) [ 确定 ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@CentOS7 opt]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 14334/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 14074/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 14071/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 14300/master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 98390/sshd: root@pt
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 103809/sshd: root@p
tcp6 0 0 :::3306 :::* LISTEN 104746/mysqld
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 14074/sshd
tcp6 0 0 ::1:631 :::* LISTEN 14071/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 14300/master
tcp6 0 0 ::1:6010 :::* LISTEN 98390/sshd: root@pt
tcp6 0 0 ::1:6011 :::* LISTEN 103809/sshd: root@p

爽爽爽~~~~~

全局变量
为了直接使用,加到环境变量里,修改/etc/profile文件,在文件末尾添加:
export PATH=/opt/mysql5.7/bin:$PATH

source /etc/profile

设置root用户可以远程访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@CentOS7 data]#  mysql -u root -p 
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('123456');

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

关闭CentOS7的防火墙,再用第三方数据库管理工具连接测试,OK啦。

MySQL5.7 编译安装完毕

源码编译安装PHP5

安装依赖库保平安

yum -y install gd zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel libticonv.x86_64 libticonv-devel.x86_64

具备第三方源的前提下的平安符
yum -y install gd zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel libticonv.x86_64 libticonv-devel.x86_64 php-mcrypt libmcrypt libmcrypt-devel mhash mhash-devel libevent libevent-devel libxml2 libxml2-devel bzip2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel

依赖包的一些点滴

扩展支持(mcrypt、mhash扩展和libevent)

如果想让编译的php支持mcrypt、mhash扩展和libevent,需要安装以下包
libmcrypt
libmcrypt-devel
mhash
mhash-devel

说明:
mcrypt扩展库可以实现加密解密功能,就是既能将明文加密,也可以密文还原。
mhash是基于离散数学原理的不可逆向的php加密方式扩展库,其在默认情况下不开启。
mhash的可以用于创建校验数值,消息摘要,消息认证码,以及无需原文的关键信息保存(如密码)等。

centos源不能安装libmcrypt-devel,由于版权的原因没有自带mcrypt的包,不过我在centos7中安装了epel源,哈哈哈。

使用yum命令安装

# yum install php-mcrypt libmcrypt libmcrypt-devel mhash mhash-devel

libevent相关包
libevent是一个异步事件通知库文件,其API提供了在某文件描述上发生某事件时或其超时时执行回调函数的机制
它主要用来替换事件驱动的网络服务器上的event loop机制。目前来说, libevent支持/dev/poll、kqueue、select、poll、epoll及Solaris的event ports。可以根据需要安装libevent,系统一般会自带libevent,但版本有些低。

使用yum命令安装

yum install libevent libevent-devel

支持xml的相关包
bzip2 是一个基于Burrows-Wheeler 变换的无损压缩软件能够高效的完成文件数据的压缩
libcurl主要功能就是用不同的协议连接和沟通不同的服务器,也就是相当封装了的sockPHP
libcurl允许你用不同的协议连接和沟通不同的服务器

yum install libxml2 libxml2-devel bzip2-devel libcurl-devel

图形相关的rpm包
通常对应的错误提示:JIS-mapped Japanese font support in GD

yum install libjpeg-devel libpng-devel freetype-devel

来个一条龙
yum -y install gd zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel libticonv.x86_64 libticonv-devel.x86_64 php-mcrypt libmcrypt libmcrypt-devel mhash mhash-devel libevent libevent-devel libxml2 libxml2-devel bzip2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel

前戏做足,好戏开始,各位同学往下面看,下面哦。。。。。。

[root@CentOS7 php-5.6.30]# tar zxvf php-5.6.30.tar.gz #解压源码包

[root@CentOS7 tools]# cd php-5.6.30/ #进入PHP源码目录

[root@CentOS7 php-5.6.30]#

输下编译命令:

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
./configure --prefix=/opt/php \
--sysconfdir=/opt/php/etc \
--enable-fpm \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--enable-shared \
--enable-mbstring \
--enable-xml \
--enable-opcache \
--enable-bcmath \
--enable-soap \
--enable-zip \
--with-mysqli=/opt/mysql5.7/bin/mysql_config \
--with-mysql=/opt/mysql5.7 \
--with-openssl \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--with-iconv \
--with-mhash \
--with-mcrypt \
--with-config-file-path=/opt/php/etc \
--with-config-file-scan-dir=/opt/php/etc/php.d \
--with-bz2 \
--with-curl \
--with-gettext \
--with-gd \

–with-mysqli=/opt/mysql5.7/bin/mysql_config 和–with-mysql=/opt/mysql5.7 这两个参数,我指向了自己编译安装的MySQL5.7.17 目录

最后看到下面这一段话:

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
Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

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: main/php_config.h is unchanged
config.status: executing default commands
[root@CentOS7 php-5.6.30]#

然后就是 make -j 8 && make install

就这么完成了编译安装php5.6.30

为php-fpm提供Sysv init脚本,并将其添加至服务列表

1
2
3
4
[root@CentOS7 ~]# cp /opt/tools/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@CentOS7 ~]# chmod +x /etc/rc.d/init.d/php-fpm
[root@CentOS7 ~]# chkconfig --add php-fpm && chkconfig php-fpm on
[root@CentOS7 ~]#

为php-fpm提供配置文件

1
2
3
4
5
6
7
8
9
10
[root@CentOS7 ~]# cd /opt/php/etc/
[root@CentOS7 etc]# ls
pear.conf php-fpm.conf.default
[root@CentOS7 etc]# cp php-fpm.conf.default php-fpm.conf
[root@CentOS7 etc]# ll
总用量 52
-rw-r--r-- 1 root root 1167 6月 13 15:51 pear.conf
-rw-r--r-- 1 root root 22752 6月 13 16:05 php-fpm.conf
-rw-r--r-- 1 root root 22752 6月 13 15:51 php-fpm.conf.default
[root@CentOS7 etc]#

编辑php-fpm的配置文件

1
2
3
4
5
6
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = run/php-fpm.pid

接下来就可以启动php-fpm了

service php-fpm start

[root@CentOS7 etc]# ps axuf |grep php

1
2
3
4
5
6
7
8
9
10
11
12
[root@CentOS7 etc]# ps axuf |grep php
root 74719 0.0 0.0 112664 972 pts/1 S+ 16:21 0:00 \_ grep --color=auto php
root 74700 0.0 0.1 125980 5032 ? Ss 16:21 0:00 php-fpm: master process (/opt/php/etc/php-fpm.conf)
nobody 74701 0.0 0.1 128064 5108 ? S 16:21 0:00 \_ php-fpm: pool www
nobody 74702 0.0 0.1 128064 5108 ? S 16:21 0:00 \_ php-fpm: pool www
nobody 74703 0.0 0.1 128064 5108 ? S 16:21 0:00 \_ php-fpm: pool www
nobody 74704 0.0 0.1 128064 5112 ? S 16:21 0:00 \_ php-fpm: pool www
nobody 74705 0.0 0.1 128064 5112 ? S 16:21 0:00 \_ php-fpm: pool www
nobody 74706 0.0 0.1 128064 5112 ? S 16:21 0:00 \_ php-fpm: pool www
nobody 74707 0.0 0.1 128064 5112 ? S 16:21 0:00 \_ php-fpm: pool www
nobody 74708 0.0 0.1 128064 5112 ? S 16:21 0:00 \_ php-fpm: pool www
[root@CentOS7 etc]#

整合nginx和php5

1、编辑 /opt/nginx/conf/nginx.conf,启用如下选项:

1
2
3
4
5
6
7
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}

2、vim /opt/nginx/conf/fastcgi_params,将其内容更改为如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

并在所支持的主页面格式中添加php格式的主页,类似如下:

1
2
3
4
location / {
root html;
index index.php index.html index.htm;
}

而后重新载入nginx的配置文件:

service nginx reload

3、在/opt/nginx/html新建index.php的测试页面,测试php是否能正常工作:

1
2
3
<?php
phpinfo();
?>

接着就可以通过浏览器访问此测试页面了。

此处有个坑 浏览器访问,不能解析,显示“ File not found.”

fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

这一句有问题, /scripts是一个目录名,也就是你站点的根目录,比如说,你访问/index.php这个文件,其实就是访问操作系统中/scripts/index.php这个文件,这个文件是否存在?/scripts 改成 $document_root 就好了,呵呵。

一键搭建LNMP源码编译环境

请移步
https://github.com/zhusas/lnmp

0%