红联Linux门户
Linux帮助

Linux单机部署LNMP环境

发布时间:2015-04-22 17:19:59来源:linux网站作者:avilifans

1. 软件来源

mysql-5.6.14下载地址:http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.14-linux-glibc2.5-i686.tar.gz
nginx-1.5.1下载地址:http://nginx.org/download/nginx-1.5.1.tar.gz
php-5.5.6下载地址:http://us2.php.net/get/php-5.5.6.tar.gz/from/cn2.php.net/mirror


2. 标准库安装

yum -y install wget make vim install gcc gcc-c++ ncurses ncurses-devel 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 curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers pcre pcre-devel  zlip zlip-devel libXpm*


3. php依赖库安装

libxml2 —— 包含库和实用工具用于解析XML文件

wget http://xmlsoft.org/sources/libxml2-2.9.0.tar.gz
tar zxvf libxml2-2.9.0.tar.gz
cd libxml2-2.9.0
./configure
make&&make install

libmcrypt —— 加密算法扩展库(支持DES, 3DES, RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法)

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
tar zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make && make install

默认的php安装后gd不支持jpg,只支持gif、png、bmp。所以首先要安装gd库

wget http://bitbucket.org/libgd/gd-libgd/downloads/libgd-2.1.0.tar.gz
tar zxvf libgd-2.1.0.tar.gz
cd libgd-2.1.0
./configure --prefix=/usr/local/webserver/gd2/
make && make install


4. mysql配置安装

安装

cd /usr/local
mkdir webserver
cd /usr/local/src
tar zxvf mysql-5.6.14-linux-glibc2.5-i686.tar.gz -C /usr/local/webserver
cd /usr/local/webserver
mv mysql-5.6.14-linux-glibc2.5-i686 mysql

配置

groupadd mysql
useradd -r -g mysql mysql
cd mysql
chown -R mysql .
chgrp -R mysql.
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data/
cp support-files/my-default.cnf /etc/my.cnf
bin/mysqld_safe --user=mysql &

配置启动mysql脚本

vim support-files/mysql.server
#将
#basedir=
#datadir=
#改为
#basedir=/usr/local/webserver/mysql
#datadir=/usr/local/webserver/mysql/data
cp support-files/mysql.server /etc/init.d/mysql

启动/停止mysql

#将mysql/bin目录加入系统环境变量中
service mysql start
service mysql stop
service mysql restart


5. nginx安装配置

安装

cd /usr/local/src
tar zxvf nginx-1.5.1.tar.gz
cd nginx-1.5.1
./configure --prefix=/usr/local/webserver/nginx
make && make install

配置启动脚本

每次启动nginx都要找到nginx的启动目录,这样无疑很麻烦,以下是配置nginx在任意目录都可以执行nginx的启动、停止、重启的脚本

执行以下命令,书写脚本

vi /etc/init.d/nginx

注:如果从其他系统拷贝到linux系统时,需要修改文件格式为unix. vi打开后:set ff=unix

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:  - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#  proxy and IMAP/POP3 proxy server
# processname: nginx
# config:  /usr/local/webserver/nginx/conf/nginx.conf
# pidfile:/usr/local/webserver/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="/usr/local/webserver/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/webserver/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
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

修改nginx脚本权限

chmod +x /etc/init.d/nginx

添加到系统服务

/sbin/chkconfig nginx on

启动/停止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


6. php安装配置

安装

tar zxvf php-5.5.6.tar.gz
cd php-5.5.6
./configure --prefix=/usr/local/webserver/php --enable-fpm --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config --with-config-file-path=/usr/local/webserver/php  --with-openssl --enable-mbstring --with-zlib --enable-xml --with-gd=/usr/local/webserver/gd2/ --with-jpeg-dir  --enable-bcmath --with-mcrypt --with-iconv --enable-pcntl --enable-shmop --enable-simplexml --with-xpm-dir=/usr/share/doc/libXpm-3.5.5
make && make install
cp php.ini-development /usr/local/webserver/php/php.ini

配置

php+nginx(/usr/local/webserver/nginx/conf/nginx.conf)

user  www www;
worker_processes  1;
events {
worker_connections  1024;
}
http {
include  mime.types;
default_type  application/octet-stream;
sendfileon;
keepalive_timeout  65;
server {
listen  80;
server_name  192.168.65.144;
location / {
root  /data/www;
index  index.php index.html index.htm;
}
error_page  500 502 503 504  /50x.html;
location = /50x.html {
root  html;
}
location ~ \.php {
root /data/www;
includefastcgi_params;
set $real_script_name $fastcgi_script_name;
  
#设置pathinfo

set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;

fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_pass  127.0.0.1:9000;
fastcgi_index  index.php;
}
}

}

php.ini

date.timezone = prc
enable_dl = On
cgi.force_redirect = 0
cgi.fix_pathinfo=1
fastcgi.impersonate = 1
cgi.rfc2616_headers = 1
allow_url_fopen = On

配置php-fpm启动脚本

编写脚本(vi /etc/init.d/php-fpm )

#! /bin/sh

### BEGIN INIT INFO
# Provides: php-fpm
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php-fpm
# Description: starts the PHP FastCGI Process Manager daemon
### END INIT INFO

prefix=/usr/local/webserver/php

php_fpm_BIN=${prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid

php_opts="--fpm-config $php_fpm_CONF"
php_pid="--pid $php_fpm_PID"

wait_for_pid () {
try=0

while test $try -lt 35 ; do

case "$1" in
'created')
if [ -f "$2" ] ; then
try=''
break
fi
;;

'removed')
if [ ! -f "$2" ] ; then
try=''
break
fi
;;
esac

echo -n .
try=`expr $try + 1`
sleep 1

done

}

case "$1" in
start)
echo -n "Starting php-fpm "

$php_fpm_BIN $php_opts $php_pid

if [ "$?" != 0 ] ; then
echo " failed"
exit 1
fi

wait_for_pid created $php_fpm_PID

if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;

stop)
echo -n "Gracefully shutting down php-fpm "

if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi

kill -QUIT `cat $php_fpm_PID`

wait_for_pid removed $php_fpm_PID

if [ -n "$try" ] ; then
echo " failed. Use force-exit"
exit 1
else
echo " done"
fi
;;

force-quit)
echo -n "Terminating php-fpm "

if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi

kill -TERM `cat $php_fpm_PID`

wait_for_pid removed $php_fpm_PID

if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;

restart)
$0 stop
$0 start
;;

reload)

echo -n "Reload service php-fpm "

if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi

kill -USR2 `cat $php_fpm_PID`

echo " done"
;;

*)
echo "Usage: $0 {start|stop|force-quit|restart|reload}"
exit 1
;;

esac

赋予脚本执行权限

cd /usr/local/webserver/php/etc
cp php-fpm.conf.default php-fpm.conf
chmod +x /etc/init.d/php-fpm

设置开机启动

/sbin/chkconfig php-fpm on

开启/重启php

service php-fpm start
service php-fpm stop
service php-fpm restart