最近重新整理下LNMP环境搭建的步骤:
1.1路径说明:
1、安装包存放目录: /app/tool/
2、软件安装目录:/app/soft/
3、项目存放目录:/app/www/
4、提示:管理用户是www
5、启动路径:

1) mysql:/etc/init.d/mysqld {start|stop|restart}
2) php:/app/soft/php5.6.16/sbin/php-fpm
3) nginx:/app/soft/nginx/sbin/nginx -t && /app/soft/nginx/sbin/nginx -s reload

设置开机自动启动:
编辑:

vim /etc/rc.local

添加:

#开机启动mysql服务
/etc/init.d/mysqld start
#开机启动php服务
/app/soft/php5.6.16/sbin/php-fpm
#开机启动Nginx服务
/app/soft/nginx/sbin/nginx
#关闭防火墙
systemctl stop firewalld.service

mysql的安装(版本:mysql-5.5.25):
1、上传cmake-2.8.8.tar.gz mysql-5.5.25.tar.gz
2、安装编译MySQL过程中需要的依赖包:

yum install ncurses-devel libaio-devel -y
yum install gcc-c++ -y

3、安装编译安装需要的软件:

cd /app/tool/
tar zxvf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure 
gmake
gmake install

4、开始安装MySQL,在主服务器上搭建MySQL:

useradd mysql -s /sbin/nologin -M
tar zxf mysql-5.5.25.tar.gz 
cd mysql-5.5.25
cmake . -DCMAKE_INSTALL_PREFIX=/app/soft/mysql-5.5.25 \
-DMYSQL_DATADIR=/app/soft/mysql-5.5.25/data \
-DMYSQL_UNIX_ADDR=/app/soft/mysql-5.5.25/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
make   
make install
ln -s mysql-5.5.25/ /app/soft/mysql     //创建MySQL软连接
echo 'PATH="/app/soft/mysql/bin/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"'>>/etc/profile
. /etc/profile

5、配置MySQL初始数据,并启动:

chmod 755 /app/soft/mysql/scripts/mysql_install_db 
/app/soft/mysql/scripts/mysql_install_db --basedir=/app/soft/mysql/ --datadir=/app/soft/mysql/data/ --user=mysql
##如果在这个环节出现了/app/soft/mysql/scripts/mysql_install_db 不存在的报错时,重新进行编译安装。
cp /app/soft/mysql/support-files/my-small.cnf /etc/my.cnf
cp /app/soft/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld   //授予执行的权限
mysql_secure_installation #设置Mysql密码
/etc/init.d/mysqld start  //重新启动

至此,MySQL编译安装完成。

Nginx安装(版本:nginx-1.9.9):
nginx使用的是一键安装脚本:安装的是nginx-1.9.9版本(你可以创建一个.sh,把下面脚本代码拷贝进去直接执行)

#!/bin/sh
####################################################
#This is a shell scripts to one-key install for lnmp  
#######################start########################
cd /app/tool &&\
[ -f nginx-1.9.9.tar.gz ] || wget http://nginx.org/download/nginx-1.9.9.tar.gz
[ -f pcre-8.37.tar.gz ] || wget http://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gz/download
#####################install libpcre################
tar zxf pcre-8.37.tar.gz
cd ./pcre-8.37 &&\
./configure
make && make install
cd .. &&\
rm -rf ./pcre-8.37
######################install nginx#################
cd /app/tool && tar zxf nginx-1.9.9.tar.gz
cd ./nginx-1.9.9
mkdir /app/soft/nginx-1.9.9
useradd nginx -s /sbin/nologin -M
yum install openssl* gd-devel -y
./configure --user=nginx --group=nginx --prefix=/app/soft/nginx-1.9.9 --with-http_image_filter_module --with-http_stub_status_module --with-http_ssl_module
make && make install
#######################test install#################
ln -s /app/soft/nginx-1.9.9/ /app/soft/nginx
cd /app/soft
if [ $? -eq 0 ];then
  echo "/usr/local/lib" >> /etc/ld.so.conf
  ldconfig
  /app/soft/nginx-1.9.9/sbin/nginx
  if [ $? -eq 0 ];then
     echo "service nginx start"
     echo  `lsof -i :80`
  fi
fi

PHP安装步骤:
php使用的是编译安装,安装的是php-5.6.16版本
###安装依赖环境包

yum -y install curl-devel
yum -y install libxslt-deve
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libevent libevent-devel
yum install zlib libxml libjpeg freetype libpng gd curl libiconv mysql-devel zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel libxslt* -y
yum -y install libxslt-deve
cd /app/tool/

下载libmcrypt(PHPlibmcrypt模块)
安装libmcrypt

cd /usr/local/src
tar zxvf libmcrypt-2.5.7.tar.gz   #解压
cd libmcrypt-2.5.7      #进入目录
./configure      #配置
make    #编译
make install #安装

下载mhash并安装:

wget http://jaist.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make && make install

下载mcrypt并安装:

wget http://jaist.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make && make install

下载libiconv并安装:

wget http://ftp.gnu.org/gnu/libiconv/libiconv-1.13.tar.gz
tar zxf libiconv-1.13.tar.gz
cd libiconv-1.13
./configure --prefix=/usr/local/libiconv
make && make install

echo "/usr/local/lib" >>/etc/ld.so.conf
rm -f /usr/lib/libmcrypt.*
rm -f /usr/lib/libmhash*
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

###安装PHP:

cd /app/tool/
wget http://cn2.php.net/distributions/php-5.6.16.tar.gz
tar zxf php-5.6.16.tar.gz
cd php-5.6.16
./configure --prefix=/app/soft/php5.6.16 --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=www --with-fpm-group=www --enable-ftp --enable-opcache=no
make
make install
cp php.ini-development /app/soft/php5.6.16/lib/php.ini
cp /app/soft/php5.6.16/etc/php-fpm.conf.default /app/soft/php5.6.16/etc/php-fpm.conf
cp -R ./sapi/fpm/php-fpm /app/soft/php5.6.16/sbin/php-fpm
/app/soft/php5.6.16/sbin/php-fpm
lsof-i :9000
yum install -y vim lrzsz netstat
最后关闭SELinux(SELinux是一种安全子系统它能控制程序只能访问特定文件)
vim /etc/selinux/config
修改为:SELINUX=disabled

redis安装
在安装Redis之前首要先做的是安装Unix的Tcl工具,如果不安装的话后期将无法对Redis进行测试。在后期执行make test的时候返回如下错误信息:You need tcl 8.xuyao de5 or newer in order to run the Redis test,具体的流程为:

cd /usr/local/src
wget http://downloads.sourceforge.net/tcl/tcl8.6.3-src.tar.gz
tar -zxvf tcl8.6.3-src.tar.gz
cd ​tcl8.6.3/unix/
./configure
make
make install

redis步骤:
先下载redis,下载地址:http://redis.io/download

cd /app/soft/
wget http://download.redis.io/releases/redis-3.0.7.tar.gz
tar -zxvf redis-3.0.7.tar.gz
cd redis-3.0.7
make
make PREFIX=/app/soft/redis-3.0.7 install

redis配置文档
1、复制并修改配置文档
cp ./redis.conf /app/soft/redis-3.0.7/
vim /app/soft/redis-3.0.7/redis.conf ##修改配置文件
把 daemonize no 改为 daemonize yes (#redis将以守护进程的方式运行,默认为no会暂用你的终端)
把timeout 0 改为timeout 300? (#当 客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能)
设置自动启动脚本

#!/bin/sh
#
# redis        Startup script for Redis Server
#
# chkconfig: - 80 12
# description: Redis is an open source, advanced key-value store.
#
# processname: redis-server
# config: /etc/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
        if [ -e $PIDFILE ];then
             echo "$desc already running...."
             exit 1
        fi
        echo -n $"Starting $desc: "
        daemon $BIN/$prog $CONFIG
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
        return $RETVAL
}
stop() {
        echo -n $"Stop $desc: "
        killproc $prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
        return $RETVAL
}
restart() {
        stop
        start
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  condrestart)
        [ -e /var/lock/subsys/$prog ] && restart
        RETVAL=$?
        ;;
  status)
        status $prog
        RETVAL=$?
        ;;
   *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        RETVAL=1
esac
exit $RETVAL

a)、启动redis:/app/soft/redis3.0.7/bin/redis-server
b)、停止redis: /app/soft/redis3.0.7/bin/redis-cli shutdown 或者 pkill redis-server
启动之后最好重新打开个窗口运行redis-cli进入控制台
使用redis:/app/soft/redis3.0.7/bin/redis-cli
更改默认端口打开vim /app/tool/redis-3.0.7/redis.conf 把 prot = 6379 更改成你自己端口(为了安全)
/app/soft/redis-3.0.7/bin/redis-server /app/soft/redis-3.0.7/redis.conf
/app/soft/redis-3.0.7/bin/redis-cli -p 6380
启动:/app/soft/redis-3.0.7/bin/redis-server /app/soft/redis-3.0.7/redis.conf

phpredis安装和配置步骤如下:
首先下载phpredis包
解压:unzip phpredis-develop.zip

cd phpredis-develop
执行 /app/soft/php5.6.16/bin/phpize 会在phpredis-develop目录下生成一个configure
./configure --with-php-config=/app/soft/php5.6.16/bin/php-config
make
make install
执行 cd /app/soft/php5.6.16/lib/php/extensions/no-debug-non-zts-20131226/ 目录下会有redis.so
vim /app/soft/php5.6.16/lib/php.ini  在php.ini添加redis.so扩展

至此LNMP环境已经完美搭建,现在可以开启你的LNMP之旅了~
转载请注明转自:运达's blog 原文地址:http://www.yunda51.com/1802.html