• 安装编译工具和依赖包
    
    [root@localhost src]# yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libXaw-devel libXmu-devel libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel

* 安装MySQL
1、安装cmake

[root@localhost src]# tar zxvf cmake-3.7.2.tar.gz
[root@localhost src]# cd cmake-3.7.2
[root@localhost cmake-3.7.2]# ./configure
[root@localhost cmake-3.7.2]# make && make install


2、安装MySQL

[root@localhost cmake-3.7.2]# cd /usr/local/src/
[root@localhost src]# mkdir -p /usr/local/boost
[root@localhost src]# cp boost_1_59_0.tar.gz /usr/local/boost/
[root@localhost src]# groupadd mysql
[root@localhost src]# useradd -g mysql mysql -s /bin/false
[root@localhost src]# mkdir -p /data/mysql
[root@localhost src]# chown -R mysql:mysql /data/mysql/
[root@localhost src]# mkdir -p /usr/local/mysql
[root@localhost src]# tar zxvf mysql-5.7.16.tar.gz
[root@localhost src]# cd mysql-5.7.16
[root@localhost mysql-5.7.16]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EMBEDDED_SERVER=OFF -DWITH_BOOST=/usr/local/boost
-- cd /usr/local/boost; tar xfz /usr/local/boost/boost_1_59_0.tar.gz
CMake Error: Problem with archive_read_next_header(): Truncated input file (needed 33792 bytes, only 0 available)
CMake Error: Problem extracting tar: /usr/local/boost/boost_1_59_0.tar.gz
-- WITH_BOOST /usr/local/boost.
-- Failed to extract files.
Please try downloading and extracting yourself.
The url is: http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
CMake Error at cmake/boost.cmake:217 (MESSAGE):
Giving up.
Call Stack (most recent call first):
CMakeLists.txt:455 (INCLUDE)

-- Configuring incomplete, errors occurred!
See also "/usr/local/src/mysql-5.7.16/CMakeFiles/CMakeOutput.log".
See also "/usr/local/src/mysql-5.7.16/CMakeFiles/CMakeError.log".
[root@localhost mysql-5.7.16]#

我这里是boost_1_59_0.tar.gz包有问题,重新下载完整的包即可,或者可以使用-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost参数在线安装boost软件包,需要服务器联网,容易下载失败。
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITH_EMBEDDED_SERVER=OFF -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
编译出错, 重新编译前要删除编译失败的文件,重新编译时,需要清除旧的对象文件和缓存信息。
make clean
rm -f CMakeCache.txt

[root@localhost mysql-5.7.16]# make
[root@localhost mysql-5.7.16]# make install
[root@localhost mysql-5.7.16]# rm -rf /etc/my.cnf
[root@localhost mysql-5.7.16]# cd /usr/local/mysql/
[root@localhost mysql]# ./bin/mysqld --user=mysql --initialize --basedir=/usr/local/mysql --datadir=/data/mysql
2018-01-29T19:37:06.836533Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-01-29T19:37:09.794126Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-01-29T19:37:10.074405Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-01-29T19:37:10.100352Z 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: cc1d463a-052b-11e8-94a0-000c29bd8d97.
2018-01-29T19:37:10.101845Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-01-29T19:37:10.102898Z 1 [Note] A temporary password is generated for root@localhost: D!Pd0Mokriji
[root@localhost mysql]#

--initialize表示默认生成密码, --initialize-insecure 表示不生成密码, 密码为空。最后一行的D!Pd0Mokriji为默认生成的密码

[root@localhost mysql]# cp /usr/local/mysql/support-files/my-default.cnf /usr/local/mysql/my.cnf
[root@localhost mysql]# ln -s /usr/local/mysql/my.cnf /etc/my.cnf
[root@localhost mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chmod 755 /etc/init.d/mysqld
[root@localhost mysql]# chkconfig mysqld on
[root@localhost mysql]# vi /etc/rc.d/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
[root@localhost mysql]# systemctl daemon-reload
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# vi /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost mysql]# source /etc/profile
[root@localhost mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
[root@localhost mysql]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
[root@localhost mysql]# mkdir /var/lib/mysql
[root@localhost mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
[root@localhost mysql]# mysql_secure_installation
[root@localhost mysql]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: #输入上面生成的密码

The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new password:

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y #是否安装密码安全插件?选择y

There are three levels of password validation policy: #有以下几种密码强度选择
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 #选择0,只要8位数字即可,选1要有大写,小写,特殊字符等


* 安装Nginx
1、安装pcre

[root@localhost src]# tar zxvf pcre-8.40.tar.gz
[root@localhost src]# cd pcre-8.40
[root@localhost pcre-8.40]# ./configure --prefix=/usr/local/pcre
[root@localhost pcre-8.40]# make && make install

2、安装openssl

[root@localhost src]# tar zxvf openssl-1.1.0e.tar.gz
[root@localhost src]# mkdir /usr/local/openssl
[root@localhost src]# cd openssl-1.1.0e
[root@localhost openssl-1.1.0e]# ./config --prefix=/usr/local/openssl
[root@localhost openssl-1.1.0e]# make && make install
[root@localhost openssl-1.1.0e]# vi /etc/profile
export PATH=$PATH:/usr/local/openssl/bin
[root@localhost openssl-1.1.0e]# source /etc/profile

3、安装zlib

[root@localhost openssl-1.1.0e]# cd /usr/local/src/
[root@localhost src]# tar zxvf zlib-1.2.11.tar.gz
[root@localhost src]# cd zlib-1.2.11
[root@localhost zlib-1.2.11]# ./configure --prefix=/usr/local/zlib
[root@localhost zlib-1.2.11]# make && make install

4、安装Nginx

[root@localhost zlib-1.2.11]# cd /usr/local/src/
[root@localhost src]# groupadd www
[root@localhost src]# useradd -g www www -s /bin/false
[root@localhost src]# tar nginx-1.10.3.tar.gz
[root@localhost src]# cd nginx-1.10.3
[root@localhost nginx-1.10.3]# ./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.1.0e --with-zlib=/usr/local/src/zlib-1.2.11 --with-pcre=/usr/local/src/pcre-8.40
[root@localhost nginx-1.10.3]# make && make install

--with-openssl=/usr/local/src/openssl-1.1.0e --with-zlib=/usr/local/src/zlib-1.2.11 --with-pcre=/usr/local/src/pcre-8.40指向的是源码包解压的路径,而不是安装的路径,否则会报错

[root@localhost ~]# vim /etc/rc.d/init.d/nginx #将ngin配置成服务
#!/bin/sh
#

nginx - this script starts and stops the nginx daemon

#

chkconfig: - 85 15

description: Nginx is an HTTP(S) server, HTTP(S) reverse \

proxy and IMAP/POP3 proxy server

processname: nginx

config: /etc/nginx/nginx.conf

config: /usr/local/nginx/conf/nginx.conf

pidfile: /usr/local/nginx/logs/nginx.pid

Source function library.

. /etc/rc.d/init.d/functions

Source networking configuration.

. /etc/sysconfig/network

Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0
NGINX_PATH="/usr/local/nginx"
nginx="$NGINX_PATH/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="$NGINX_PATH/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {

make required directories

user=$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -
if [ -z "grep $user /etc/passwd" ]; then
useradd -M -s /bin/nologin $user
fi
options=$nginx -V 2>&1 | grep 'configure arguments:'
for opt in $options; do
if [ echo $opt | grep '.*-temp-path' ]; then
value=echo $opt | cut -d "=" -f 2
if [ ! -d "$value" ]; then

echo "creating" $value

mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
sleep 1
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
[root@localhost init.d]# chmod 755 /etc/rc.d/init.d/nginx
[root@localhost init.d]# chkconfig nginx on
[root@localhost init.d]# systemctl daemon-reload
[root@localhost init.d]# systemctl restart nginx

*安装php
1、安装yasm

[root@localhost init.d]# cd /usr/local/src/
[root@localhost src]# tar zxvf yasm-1.3.0.tar.gz
[root@localhost src]# cd yasm-1.3.0
[root@localhost yasm-1.3.0]# ./configure && make && make install

2、安装libmcrypt

[root@localhost yasm-1.3.0]# cd /usr/local/src/
[root@localhost src]# tar zxvf libmcrypt-2.5.8.tar.gz
[root@localhost src]# cd libmcrypt-2.5.8
[root@localhost libmcrypt-2.5.8]# ./configure && make && make install

3、安装libvpx

[root@localhost libmcrypt-2.5.8]# cd /usr/local/src/
[root@localhost src]# tar zxvf libvpx-1.3.0.tar.gz
[root@localhost src]# cd libvpx-1.3.0
[root@localhost libvpx-1.3.0]# ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9 && make && make install

4、安装tiff

[root@localhost libvpx-1.3.0]# cd /usr/local/src/
[root@localhost src]# tar zxvf tiff-4.0.7.tar.gz
[root@localhost src]# cd tiff-4.0.7
[root@localhost tiff-4.0.7]# ./configure --prefix=/usr/local/tiff --enable-shared && make && make install

5、安装libpng

[root@localhost tiff-4.0.7]# cd /usr/local/src
[root@localhost src]# tar zxvf libpng-1.6.32.tar.gz
[root@localhost src]# cd libpng-1.6.32
[root@localhost libpng-1.6.32]# ./configure --prefix=/usr/local/libpng --enable-shared
[root@localhost libpng-1.6.32]# make && make install

6、安装freetype

[root@localhost libpng-1.6.32]# cd /usr/local/src
[root@localhost src]# tar zxvf freetype-2.7.1.tar.gz
[root@localhost src]# cd freetype-2.7.1
[root@localhost freetype-2.7.1]# ./configure --prefix=/usr/local/freetype --enable-shared && make && make install

7、安装jpeg

[root@localhost freetype-2.7.1]# cd /usr/local/src
[root@localhost src]# tar zxvf jpegsrc.v9b.tar.gz
[root@localhost src]# cd jpeg-9b/
[root@localhost jpeg-9b]# ./configure --prefix=/usr/local/jpeg --enable-shared && make && make install

8、安装libgd

[root@localhost jpeg-9b]# cd /usr/local/src
[root@localhost src]# tar zxvf libgd-2.1.1.tar.gz
[root@localhost src]# cd libgd-2.1.1
[root@localhost libgd-2.1.1]# ./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/lib64 --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx
[root@localhost libgd-2.1.1]# make && make install

说明:如果libgd编译失败,可以先跳过,直接使用系统默认的2.1.0版本,在编译php的时候把参数--with-gd=/usr/local/libgd修改为--with-gd即可。

9、安装t1lib

[root@localhost libgd-2.1.1]# cd /usr/local/src
[root@localhost src]# tar zxvf t1lib-5.1.2.tar.gz
[root@localhost src]# cd t1lib-5.1.2
[root@localhost t1lib-5.1.2]# ./configure --prefix=/usr/local/t1lib --enable-shared && make without_doc && make install

10、安装php
如果系统是64位,请执行以下两条命令,否则安装php会出错

[root@localhost t1lib-5.1.2]# cd /usr/local/src/
[root@localhost src]# cp -frp /usr/lib64/libltdl.so /usr/lib/
[root@localhost src]# cp -frp /usr/lib64/libXpm.so
/usr/lib/
[root@localhost src]# tar zxvf php-7.1.2.tar.gz
[root@localhost src]# cd php-7.1.2
[root@localhost php-7.1.2]# export LD_LIBRARY_PATH=/usr/local/libgd/lib
[root@localhost php-7.1.2]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd=/usr/local/libgd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/lib64 --with-zlib-dir=/usr/local/zlib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype --enable-mysqlnd
checking for XpmFreeXpmImage in -lXpm... (cached) yes
checking for gdSetErrorMethod in -lgd... no
configure: error: Unable to find libgd.(a|so) >= 2.1.0 anywhere under /usr/local/libgd
[root@localhost php-7.1.2]#

如果提示libgd版本错误,把php编译参数--with-gd=/usr/local/libgd修改为--with-gd即可

[root@localhost php-7.1.2]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/lib64 --with-zlib-dir=/usr/local/zlib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype --enable-mysqlnd
[root@localhost php-7.1.2]# make && make install
[root@localhost php-7.1.2]# cp php.ini-production /usr/local/php/etc/php.ini
[root@localhost php-7.1.2]# rm -rf /etc/php.ini
[root@localhost php-7.1.2]# ln -s /usr/local/php/etc/php.ini /etc/php.ini
[root@localhost php-7.1.2]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost php-7.1.2]# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
[root@localhost php-7.1.2]# vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid #取消注释
[root@localhost php-7.1.2]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@localhost php-7.1.2]# vi /usr/local/php/etc/php-fpm.d/www.conf
user = www
group = www
[root@localhost php-7.1.2]# cp /usr/local/src/php-7.1.2/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@localhost php-7.1.2]# chmod +x !$
chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-7.1.2]# chkconfig php-fpm on
[root@localhost php-7.1.2]# vi /usr/local/php/etc/php.ini
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname #列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
date.timezone = PRC #设置时区
expose_php = Off #禁止显示php版本的信息
short_open_tag = ON #支持php短标签
opcache.enable=1 #php支持opcode缓存
opcache.enable_cli=0 #php支持opcode缓存
zend_extension=opcache.so #开启opcode缓存功能
[root@localhost php-7.1.2]# vi /usr/local/nginx/conf/nginx.conf #配置nginx支持php
user www; #必须与/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
location / {
root html;
index index.html index.htm index.php; #添加index.php
}

pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

取消FastCGI server以下location的注释

    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;     #fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
        include        fastcgi_params;
    }

[root@localhost php-7.1.2]# systemctl restart nginx
[root@localhost php-7.1.2]# systemctl restart php-fpm

验证测试

[root@localhost php-7.1.2]# cd /usr/local/nginx/html/
[root@localhost html]# vim index.php

<?php
phpinfo();
?>
[root@localhost html]# chown -R www:www /usr/local/nginx/html/
[root@localhost html]# chmod -R 700 /usr/local/nginx/html/

浏览器验证

CentOS7编译安装LNMP