编译安装搭建lnmp环境Linux Centos 7.2+Nginx+PHP7+MySQL5.7

免密码登录

本机生成key:ssh-keygen

查看本机key:cat ~/.ssh/id_rsa.pub

复制key到服务器:vim ~/.ssh/authorized_keys

更换CentOS源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache	//清除yum缓存
yum update		//更新

安装依赖

yum install gcc gcc-c++ libmcrypt-devel mcrypt mhash-devel zlib-devel pcre-devel openssl openssl-devel libxml2-devel libpng-devel

安装Nginx

//下载并解压
wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar -zvxf nginx-1.10.2.tar.gz
cd nginx-1.10.2

//配置
./configure --prefix=/usr/local/nginx --modules-path=/usr/local/nginx/modules --with-http_ssl_module --pid-path=/usr/local/nginx/nginx.pid

//编译
make && make install

//www.conf配置文件
location / {
	root   /data/wwwr/public;
	index  index.php index.html index.htm;
	try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
	root           /data/www/public;
	fastcgi_pass   127.0.0.1:9000;
	fastcgi_index  index.php;
	fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	include        fastcgi_params;
}

//启动Nginx
cd /usr/local/nginx
sbin/nginx
//暂停Nginx
sbin/nginx -s stop

安装PHP7

//下载并解压
wget http://cn.php.net/distributions/php-7.0.15.tar.gz
tar -zvxf php-7.0.15.tar.gz
cd php-7.0.15

//配置
./configure --prefix=/usr/local/php7 --exec-prefix=/usr/local/php7 --datadir=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-fpm-user=nginx --with-fpm-group=nginx --with-gd --with-iconv --enable-mbstring --enable-fpm --enable-mysqlnd

//编译
make && make install

//复制php.ini
cp php.ini-development /usr/local/php7/etc/php.ini
//复制php-fpm.conf
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
//复制www.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
//编辑php-fpm.conf,查看include
vim /usr/local/php7/etc/php-fpm.conf

//新建nginx组
groupadd -r nginx
//新建无登录权限的nginx用户
useradd -r -g nginx -s /bin/false nginx
//查看nginx组及用户
id nginx

//启动PHP-FPM
sudo /usr/local/php7/sbin/php-fpm

安装MySQL

//安装CMake
yum install ncurses-devel cmake
//下载并解压
wget http://dev.mysql.com/Downloads/MySQL-5.7/mysql-boost-5.7.14.tar.gz
tar -zvxf mysql-boost-5.7.14.tar.gz
cd mysql-boost-5.7.14

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

//配置
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSCONFDIR=/usr/local/mysql/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=boost -DWITH_SYSTEMD=1

make && make install

在这里特别提醒, 对于mysql5.7.8的make编译, 如果是阿里云centos主机512M内存的, 会在make编译到45%时会报错, 这是内存不足所致。

//
rm -rf CMakeCache.txt
make clean

//注释:of=/home/swap,放置swap的空间; count的大小就是增加的swap空间的大小,64M就是块大小,这里是64MB,所以总共空间就是bs*count=1024MB.这里分配空间的时候需要一点时间,等待执行完毕。
dd if=/dev/zero of=/swapfile bs=1k count=2048000 --获取要增加的2G的SWAP文件块

mkswap /swapfile     -- 创建SWAP文件,把刚才空间格式化成swap各式
swapon /swapfile     -- 激活SWAP文件
swapon -s            -- 查看SWAP信息是否正确
echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab     -- 添加到fstab文件中让系统引导时自动启动

注意, swapfile文件的路径在/var/下,编译完后, 如果不想要交换分区了, 可以删除:

# swapoff /swapfile
# rm -fr /swapfile

安装扩展

yum install -y lrzsz composer git libuuid-devel

安装composer的时候会默认安装php5.6,需要更新成php7

//如果之前已经安装过php的话
yum remove php* php-common

//安装php7的yum源
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

//修改yum源
如果没有remi.repo,就执行rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
vim /etc/yum.repos.d/remi.repo
将[remi]段中的enabled=0改为enabled=1
vim /etc/yum.repos.d/remi-php70.repo
将[remi-php70]段中的enabled=0改为enabled=1

//如果执行下列命令看到是php7.*版本,说明安装正确
yum list php
//安装php7
yum install php php-fpm php-cli php-pdo php-mysql php-gd php-bcmath php-xml php-mbstring php-mcrypt php-redis
//也可以yum install php70

安装uuid

yum install libuuid-devel
wget http://pecl.php.net/get/uuid-1.0.4.tgz
tar -zxvf uuid-1.0.4.tgz
cd uuid-1.0.4

/usr/local/php7/bin/phpize
./configure --with-php-config=/usr/local/php7/bin/php-config

make && make install

/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/uuid.so

将上边的添加到php.ini的Dynamic Extensions
vim /usr/local/php7/etc/php.ini
extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/uuid.so


如果出现Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

yum install autoconf

安装Redis

yum install -y redis
//启动
redis-server &
service redis start
chkconfig redis on

//或者编译安装
https://github.com/phpredis/phpredis 

wget https://github.com/phpredis/phpredis/archive/3.1.0.tar.gz
tar -zxvf 3.1.0.tar.gz
cd 3.1.0

/usr/local/php7/bin/phpize
./configure --with-php-config=/usr/local/php7/bin/php-config
make && make install

vim /usr/local/php7/etc/php.ini
extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/redis.so

部署网站

//新建目录
mkdir wwwroot
//文件夹权限
chmod 777 -R storage
chmod 777 -R vendor

启动脚本

Nginx启动脚本/etc/init.d/nginx

https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

设置完成后可以用以下命令管理Nginx

service nginx start
service nginx stop
service nginx restart
service nginx reload
 
/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart
/etc/init.d/nginx reload

vim /etc/init.d/nginx

#!/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:      /etc/sysconfig/nginx
# pidfile:     /var/run/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="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      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
    fi
}

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
chmod +x /etc/init.d/nginx
/sbin/chkconfig nginx on
# 检查一下
/sbin/chkconfig --list nginx
nginx          	0:关	1:关	2:开	3:开	4:开	5:开	6:关

PHP-FPM启动脚本

service php-fpm start
service php-fpm stop
service php-fpm restart
service php-fpm reload
 
/etc/init.d/php-fpm start
/etc/init.d/php-fpm stop
/etc/init.d/php-fpm restart
/etc/init.d/php-fpm reload
vim /etc/init.d/php-fpm
#!/bin/bash
#
# Startup script for the PHP-FPM server.
#
# chkconfig: 345 85 15
# description: PHP is an HTML-embedded scripting language
# processname: php-fpm
# config: /usr/local/php7/etc/php.ini
 
# Source function library.
. /etc/rc.d/init.d/functions
 
PHP_PATH=/usr/local
DESC="php-fpm daemon"
NAME=php-fpm
# php-fpm路径
DAEMON=$PHP_PATH/php7/sbin/$NAME
# 配置文件路径
CONFIGFILE=$PHP_PATH/php7/etc/php-fpm.conf
# PID文件路径(在php-fpm.conf设置)
PIDFILE=$PHP_PATH/php7/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
 
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
 
rh_start() {
  $DAEMON -y $CONFIGFILE || echo -n " already running"
}
 
rh_stop() {
  kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
 
rh_reload() {
  kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
 
case "$1" in
  start)
        echo -n "Starting $DESC: $NAME"
        rh_start
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC: $NAME"
        rh_stop
        echo "."
        ;;
  reload)
        echo -n "Reloading $DESC configuration..."
        rh_reload
        echo "reloaded."
  ;;
  restart)
        echo -n "Restarting $DESC: $NAME"
        rh_stop
        sleep 1
        rh_start
        echo "."
        ;;
  *)
         echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
         exit 3
        ;;
esac
exit 0
chmod +x /etc/init.d/php-fpm
/sbin/chkconfig php-fpm on
# 检查一下
/sbin/chkconfig --list php-fpm
php-fpm           0:off   1:off   2:on    3:on    4:on    5:on    6:off

参考资料

CentOS 7.2mini版本下编译安装php7.0.10+MySQL5.7.14+Nginx1.10.1

centos6.6 从源码编译安装mysql5.7.5

g++: 内部错误:Killed (程序 cc1plus)

CentOS6.5 下升级 PHP7、MySQL5.7