LNMP 环境搭建

2018-04-22  本文已影响0人  joker_牧羊人
(CentOS 6.8 + Nginx 1.10.3 + Mysql 5.7.18 + PHP 5.6.31)

一、安装Nginx

1、安装依赖

# yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

2、下载 Nginx
# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.10.3.tar.gz
3、解压

# tar -zxvf nginx-1.10.3.tar.gz

4、编译
# cd nginx-1.10.3
# ./configure \
  --prefix=/usr \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre
# make && make install
5、创建启动脚本

# vim /etc/init.d/nginx

5-1、脚本内容
#!/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="/etc/nginx/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
6、修改脚本权限

# chmod 755 /etc/init.d/nginx

7、设置开机启动
# chkconfig --add nginx
# service nginx reload
# chkconfig nginx on
8、启动 Nginx

# service nginx start

二、安装 Mysql

1、安装依赖

# yum -y install gcc gcc-c++ gcc-g77 make cmake bison ncurses-devel autoconf automake zlib* fiex* libxml* libmcrypt* libtool-ltdl-devel* libaio libaio-devel bzr libtool ncurses5-devel imake libxml2-devel expat-devel

2、安装 boost_1_59_0 (必须是该版本)
2-1、下载 boost_1_59_0
# cd /usr/local/src
# wget https://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
2-2、解压

# tar -zxvf boost_1_59_0.tar.gz

2-3、配置
# cd boost_1_59_0
# ./bootstrap.sh
2-4、编译

# ./b2

2-5、安装

# ./b2 install

3、安装 cmake (最新版本)
3-1、下载 cmake
# cd /usr/local/src
# wget https://cmake.org/files/v3.8/cmake-3.8.0.tar.gz
3-2、解压

# tar -xzvf cmake-3.8.0.tar.gz

3-3、编译安装
# cd cmake-3.8.0
# ./bootstrap
# gmake
# gmake install
4、安装 Mysql 5.7.18
4-1、添加mysql用户和所属组
# /usr/sbin/groupadd mysql
# /usr/sbin/useradd -g mysql mysql
4-2、创建mysql安装目录和数据目录
# mkdir /usr/local/mysql
# mkdir /usr/local/mysql/data
4-3、修改mysql目录所有者

# chown -R mysql:mysql /usr/local/mysql

4-4、下载mysql源码包
# cd /usr/local/src
# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18.tar.gz
4-5、解压

# tar -xzvf mysql-5.7.18.tar.gz

4-6、cmake编译配置
# cd mysql-5.7.18
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
  -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
  -DMYSQL_DATADIR=/usr/local/mysql/mydata \
  -DSYSCONFDIR=/etc \
  -DEXTRA_CHARSETS=all \
  -DDEFAULT_CHARSET=utf8 \
  -DDEFAULT_COLLATION=utf8_general_ci \
  -DWITH_MYISAM_STORAGE_ENGINE=1 \
  -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  -DWITH_MEMORY_STORAGE_ENGINE=1 \
  -DWITH_READLINE=1 \
  -DMYSQL_TCP_PORT=3306 \
  -DENABLED_LOCAL_INFILE=1 \
  -DWITH_PARTITION_STORAGE_ENGINE=1 \
  -DWITH_SSL=yes \
  -DWITH_BOOST=/usr/local/src/boost_1_59_0 \
  -DMYSQL_USER=mysql
4-7、编译安装
# make && make install
 【
     注意:这里可能会报错(如果在阿里云内存不足编译失败)

     错误代码信息:
     c++: Internal error: Killed (program cc1plus)
     Please submit a full bug report.
     See < http://bugzilla.redhat.com/bugzilla > for instructions.
     make[2]: *** [sql/CMakeFiles/sql.dir/item_geofunc.cc.o] Error 1
     make[1]: *** [sql/CMakeFiles/sql.dir/all] Error 2
     make: *** [all] Error 2

     处理方法:用2g分区交换,运行下面
     # dd if=/dev/zero of=/swapfile bs=1k count=2048000 --获取要增加的2G的SWAP文件块
     # mkswap /swapfile     -- 创建SWAP文件
     # swapon /swapfile     -- 激活SWAP文件
     # swapon -s            -- 查看SWAP信息是否正确
     # echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab   -- 添加到fstab文件中让系统引导时自动启动

     然后需要删除CMakeCache.txt文件,再重新cmake预编译
 】
4-8、初始化数据库【注意:运行后最后一句[note] 生成了一个mysql默认密码,复制到一个地方,保存下来。】
# cd /usr/local/mysql/bin
# ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data/ --basedir=/usr/local/mysql --socket=/usr/local/mysql/mysql.sock
4-9、添加mysql服务
# cd /usr/local/src/mysql-5.7.18
# cd support-files
# cp -a mysql.server /etc/init.d/mysql
4-10、编辑mysql配置文件
# cd /etc
# sudo vi my.cnf
【修改配置文件中的
1)、datadir=/usr/local/mysql/data
2)、socket=/usr/local/mysql/mysql.sock
】
4-11、创建文件夹并给定用户和权限
# mkdir /var/lib/mysql
# chown mysql:mysql /var/lib/mysql
# chmod 777 /var/lib/mysql
4-12、启动mysql

# service mysql start

4-13、设置开机启动

# chkconfig mysql on

4-14、添加环境变量
# sudo vi /etc/profile
在文件末尾添加一行: export PATH=$PATH:/usr/local/mysql/bin
# :wq
# source /etc/profile   (使修改的profile文件生效)
4-15、登录mysql
# mysql -u root -p
【注意:输入 4-8 中保存的密码后 回车】
4-16、修改root密码

# SET PASSWORD = PASSWORD('yourpassword');

4-17、刷新mysql的系统权限相关表

# flush privileges;

4-18、退出,并用新密码重新登录即可

# quit;

三、安装php

1、安装依赖

# yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel openssl openssl-devel openldap openldap-devel openldap-clients openldap-servers php-mcrypt libmcrypt libmcrypt-devel

2、下载 php
# cd /usr/local/src
# wget http://cn2.php.net/distributions/php-5.6.31.tar.gz
3、解压

# tar -zxvf php-5.6.31.tar.gz

4、PHP 编译配置
# cd php-5.6.31
# ./configure \
  --prefix=/usr/local/php \
  --with-fpm-user=www --with-fpm-group=www \
  --with-config-file-path=/usr/local/php/etc \
  --with-mhash --with-mcrypt --enable-bcmath \
  --enable-mysqlnd --with-mysql --with-mysqli --with-pdo-mysql \
  --with-gd --enable-gd-native-ttf --with-jpeg-dir --with-png-dir --with-freetype-dir \
  --enable-fpm \
  --enable-mbstring \
  --enable-pcntl \
  --enable-sockets \
  --enable-opcache \
  --with-openssl \
  --with-zlib \
  --with-curl \
  --with-libxml-dir \
  --with-iconv-dir
5、编译安装
# make &amp;&amp; make install
 【
     注意:
     此处可能会出现一个error:make: *** [sapi/cli/php] Error 1
     解决办法:编辑文件 Makefile
     # vim Makefile
     # /EXTRA_LIBS (查找EXTRA_LIBS的位置)
     然后在末尾添加 -liconv
     然后保存,再重新编译
 】
6、复制生成php-fpm配置文件

# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

7、复制生成一份php配置文件

# cp php.ini-production /usr/local/php/etc/php.ini

8、将php-fpm加入系统服务

# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

9、赋予执行权限

# chmod +x /etc/init.d/php-fpm

10、设置开机启动

# chkconfig php-fpm on

11、创建www用户

# groupadd www &amp;&amp; useradd -d /home/www -g www www

12、启动php-fpm

# service php-fpm start

13、添加环境变量
# sudo vi /etc/profile
修改行: export PATH=$PATH:/usr/local/mysql/bin:/usr/local/php/bin
# :wq
# source /etc/profile   (使修改的profile文件生效)

四、配置nginx支持php-fpm(修改nginx.conf配置文件)

1、编辑nginx配置文件,具体路径根据实际的nginx.conf配置文件位置编辑,下面主要修改nginx的server {}配置块中的内容,修改location块,追加index.php让nginx服务器默认支持index.php为首页
# sudo vi /etc/nginx/nginx.conf
location / {
    root   html;
    index  index.html index.htm index.php;
}
2、然后配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,此时去掉注释并修改为以下内容,这里面很多都是默认的,root是配置php程序放置的根目录,主要修改的就是fastcgi_param中的/scripts为$document_root
location ~ \.php\$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  
    \$document_root$fastcgi_script_name;
    include        fastcgi_params;
}
3、保存退出并重启nginx服务
# :wq
# service nginx restart

至此,LNMP环境已搭建完毕。

【如若文档有错误,欢迎大家不吝赐教。本文档是集网上各位大神的资源进行整合的,具体资源来源已经忘记了,如果发现有侵权等行为,请联系我,我将对应处理,谢谢~~~】

上一篇下一篇

猜你喜欢

热点阅读