CentOS7编译安装LNMP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7编译安装LNMP相关的知识,希望对你有一定的参考价值。

  • 安装编译工具和依赖包
    
    [[email protected] 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

[[email protected] src]# tar zxvf cmake-3.7.2.tar.gz
[[email protected] src]# cd cmake-3.7.2
[[email protected] cmake-3.7.2]# ./configure
[[email protected] cmake-3.7.2]# make && make install


2、安装MySQL

[[email protected] cmake-3.7.2]# cd /usr/local/src/
[[email protected] src]# mkdir -p /usr/local/boost
[[email protected] src]# cp boost_1_59_0.tar.gz /usr/local/boost/
[[email protected] src]# groupadd mysql
[[email protected] src]# useradd -g mysql mysql -s /bin/false
[[email protected] src]# mkdir -p /data/mysql
[[email protected] src]# chown -R mysql:mysql /data/mysql/
[[email protected] src]# mkdir -p /usr/local/mysql
[[email protected] src]# tar zxvf mysql-5.7.16.tar.gz
[[email protected] src]# cd mysql-5.7.16
[[email protected] 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".
[[email protected] 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

[[email protected] mysql-5.7.16]# make
[[email protected] mysql-5.7.16]# make install
[[email protected] mysql-5.7.16]# rm -rf /etc/my.cnf
[[email protected] mysql-5.7.16]# cd /usr/local/mysql/
[[email protected] 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 [email protected]: D!Pd0Mokriji
[[email protected] mysql]#

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

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

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

2、安装openssl

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

3、安装zlib

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

4、安装Nginx

[[email protected] zlib-1.2.11]# cd /usr/local/src/
[[email protected] src]# groupadd www
[[email protected] src]# useradd -g www www -s /bin/false
[[email protected] src]# tar nginx-1.10.3.tar.gz
[[email protected] src]# cd nginx-1.10.3
[[email protected] 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
[[email protected] 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指向的是源码包解压的路径,而不是安装的路径,否则会报错

[[email protected] ~]# 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
[[email protected] init.d]# chmod 755 /etc/rc.d/init.d/nginx
[[email protected] init.d]# chkconfig nginx on
[[email protected] init.d]# systemctl daemon-reload
[[email protected] init.d]# systemctl restart nginx

*安装php
1、安装yasm

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

2、安装libmcrypt

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

3、安装libvpx

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

4、安装tiff

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

5、安装libpng

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

6、安装freetype

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

7、安装jpeg

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

8、安装libgd

[[email protected] jpeg-9b]# cd /usr/local/src
[[email protected] src]# tar zxvf libgd-2.1.1.tar.gz
[[email protected] src]# cd libgd-2.1.1
[[email protected] 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
[[email protected] libgd-2.1.1]# make && make install

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

9、安装t1lib

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

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

[[email protected] t1lib-5.1.2]# cd /usr/local/src/
[[email protected] src]# cp -frp /usr/lib64/libltdl.so /usr/lib/
[[email protected] src]# cp -frp /usr/lib64/libXpm.so
/usr/lib/
[[email protected] src]# tar zxvf php-7.1.2.tar.gz
[[email protected] src]# cd php-7.1.2
[[email protected] php-7.1.2]# export LD_LIBRARY_PATH=/usr/local/libgd/lib
[[email protected] 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
[[email protected] php-7.1.2]#

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

[[email protected] 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
[[email protected] php-7.1.2]# make && make install
[[email protected] php-7.1.2]# cp php.ini-production /usr/local/php/etc/php.ini
[[email protected] php-7.1.2]# rm -rf /etc/php.ini
[[email protected] php-7.1.2]# ln -s /usr/local/php/etc/php.ini /etc/php.ini
[[email protected] php-7.1.2]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[[email protected] php-7.1.2]# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
[[email protected] php-7.1.2]# vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid #取消注释
[[email protected] php-7.1.2]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[[email protected] php-7.1.2]# vi /usr/local/php/etc/php-fpm.d/www.conf
user = www
group = www
[[email protected] 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
[[email protected] php-7.1.2]# chmod +x !$
chmod +x /etc/rc.d/init.d/php-fpm
[[email protected] php-7.1.2]# chkconfig php-fpm on
[[email protected] 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缓存功能
[[email protected] 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;
    }

[[email protected] php-7.1.2]# systemctl restart nginx
[[email protected] php-7.1.2]# systemctl restart php-fpm

验证测试

[[email protected] php-7.1.2]# cd /usr/local/nginx/html/
[[email protected] html]# vim index.php

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

浏览器验证

技术分享图片

以上是关于CentOS7编译安装LNMP的主要内容,如果未能解决你的问题,请参考以下文章

CentOS7一键编译安装LNMP环境

centos7编译安装lnmp环境

CentOS7.2编译安装LNMP

Centos7构建LNMP平台

centos7.4 编译安装php5.6 (LNMP)

阿里云-centos7.2-LNMP-编译安装-记录