CentOS6.x 64位下编译安装nginx1.6.x+php5.5.x+mariaDB10.0.x的Shell脚本

以下是CentOS6.x 64位下编译安装nginx1.6.x+php5.5.x+mariaDB10.0.x的Shell脚本,脚本中附带了个人在服务器中常用目录的规划,脚本中所有软件都可以从本站下载,并且已经配置到脚本中。若在安装过程中,发现安装失败,比较大的可能性是系统改成163的源后,用yum安装部分软件没有安装成功(在自动安装过程中,需要多留意是不是因为有部分软件下载失败而导致该软件安装不成功),此时把163源再还原为系统默认的源后,再执行一次脚本就可以了。以下是脚本的内容,在文章尾可以该脚本的下载链接,可以直接下载。本脚本在最小化安装的CentOS6.5系统上测试通过,其它版本未有测试,因为CentOS同一系列区别不大,估计CentOS6.x系列的系统都能安装,具体的大家可以测试下,如有什么问题欢迎回帖反馈。

#!/bin/bash
 
# -------------------------------------------------------------------------------
# Filename:    compile_lnmp.sh
# Revision:    1.3
# Date:        2014-07-14
# Author:      三木
# Email:       linmaogan#gmail.com
# Website:     www.3mu.me
# Description: CentOS6.5+Nginx+PHP+MariaDB及相关扩展安装脚本(编译安装)
# Notes:       需要切换到root运行,版本针对64位系统,操作系统为CentOS6.5
#              各软件版本分别为:Nginx:1.6.0 PHP:5.5.14 MariaDB:10.0.12
# -------------------------------------------------------------------------------
# Copyright:   2014 (c) 三木
# License:     GPL
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# you should have received a copy of the GNU General Public License
# along with this program (or with Nagios);
#
# Credits go to Ethan Galstad for coding Nagios
# If any changes are made to this script, please mail me a copy of the changes
# -------------------------------------------------------------------------------
#Version 1.0
#2014-07-14 三木 初始版本建立
# -------------------------------------------------------------------------------

#变量定义#############################################################################
DOWNLOAD_URL='http://soft.3mu.me' # 下载软件的服务器地址
CPU_NUM=`cat /proc/cpuinfo |grep 'processor'|wc -l` # CPU个数
NGINX_STATUS_URL='3.status'
MYSQL_PORY='61920' #MySQL访问端口
MYSQL_ROOT_PAS='lmg'
DONE="\e[0;32m\033[1mdone\e[m" 
#解锁系统文件#########################################################################
chattr -i /etc/passwd 
chattr -i /etc/group
chattr -i /etc/shadow
chattr -i /etc/gshadow
chattr -i /etc/services
#目录设置#############################################################################
#创建网站相关目录#####################################################################
if [ ! -e /www ]
then
	# data存放除日志文件外的所有数据,data1存放日志文件
	mkdir -pv /home/{data,data1}
	ln -s /home/data /
	ln -s /home/data1 /
	
	mkdir -pv /www
	
	# wwwroot存放网站文件,conf存放软件的配置文件,apps应用程序安装目录(如Nginx、PHP、MySQL等),software存放所下载的安装软件
	# conf/sites-available存放虚拟机的配置文件,conf/sites-enabled存放sites-available中虚拟机的软链接
	# shell存放实现特定功能的shell脚本,tmp用于存放任何临时用的文件
	mkdir -pv /data/{wwwroot,conf/{sites-available,sites-enabled},apps,software,shell,tmp}
	ln -s /data/wwwroot /www/
	
	# web存放网站的源代码,git存放Git版本库文件,fasddfs是FastDFS文件系统存放文件的目录,
	# proxy_cache存放Nginx反向代理缓存文件的目录,mysql存放MySQL数据库目录
	# web/soft存放安装软件,主要给其它机器下载安装文件用
	mkdir -pv /data/wwwroot/{web/soft,git,fasddfs,proxy_cache,mysql}
	
	# old_log_bak存放Nginx每日自动切割的旧日志文件
	mkdir -pv /data1/logs/{nginx/{access,error},mysql,cron,old_log_bak}
	
	mkdir -pv /backup
	ln -s /backup /data/
fi

#更新yum源#############################################################################
yum -y install wget
#将默认源换为163源,一般在系统初始化中已经做了修改,此处修改主要是为了方便在新的测试机上进行测试
if cat /etc/yum.repos.d/CentOS-Base.repo |awk -F: '{print $1}'|grep '163.com'  2>&1 >/dev/null 
then 
	echo -e "163 Yum has been \e[0;32m\033[1madded\e[m." 
else 
	mv /etc/yum.repos.d/CentOS-Base.repo{,.bak}
	wget http://mirrors.163.com/.help/CentOS6-Base-163.repo -O /etc/yum.repos.d/CentOS-Base.repo
	echo -e "Install 163 source ${DONE}." 
fi 

#获取相关开源程序,利用CentOS Linux系统自带的yum命令安装、升级所需的程序库############
#yum install pcre
#yum install openssl*
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 krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make
yum -y install gd gd2 gd-devel gd2-devel

#下载程序源码包#######################################################################
cd /data/software
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/nginx/nginx-1.6.0.tar.gz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/php/php-5.5.14.tar.gz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/mariadb/mariadb-10.0.12.tar.gz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/cmake/cmake-3.0.0.tar.gz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/libmcrypt/libmcrypt-2.5.8.tar.gz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/memcache/memcache-2.2.7.tgz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/pcre/pcre-8.35.tar.gz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/jpegsrc/jpegsrc.v9.tar.gz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/libpng/libpng-1.6.2.tar.gz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/freetype/freetype-2.4.12.tar.gz
wget -c ${DOWNLOAD_URL}/soft/linux/nginx_php/autoconf/autoconf-latest.tar.gz

#如果已安装Apache和PHP,则卸载########################################################
yum -y remove httpd* nginx php*  mysql
#安装Nginx############################################################################
# 创建运行Nginx的用户
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www -s /bin/false

# 安装Nginx所需的pcre库
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure --prefix=/data/apps/pcre
make && make install
cd ../
# 安装Nginx
tar zxvf nginx-1.6.0.tar.gz
cd nginx-1.6.0
./configure --user=www --group=www --prefix=/data/apps/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/data/software/pcre-8.35 --with-http_realip_module --with-http_image_filter_module
make
make install
cd ../
# 创建Nginx日志目录
# mkdir -p /data1/logs/nginx
chmod +w /data1/logs/nginx
chown -R www:www /data1/logs/nginx
# 创建Nginx配置文件
# 在/data/apps/nginx/conf/目录中创建nginx.conf文件:
rm -f /data/apps/nginx/conf/nginx.conf

# 生成nginx主配置文件
cat << EOF > /data/apps/nginx/conf/nginx.conf
user  www www;
worker_processes 8;
error_log  /data1/logs/nginx/nginx_error.log  crit;
pid        /data/apps/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process. 
worker_rlimit_nofile 65535;

events 
{
  use epoll;
  worker_connections 65535;
}

http 
{
  include       mime.types;
  default_type  application/octet-stream;

  #charset  gb2312;
      
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
      
  sendfile on;
  tcp_nopush     on;

  keepalive_timeout 60;

  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

  #limit_zone  crawler  $binary_remote_addr  10m;
  server_tokens off;
  log_format '$remote_addr - $remote_user [$time_local] "$request" '
	              '$status $body_bytes_sent "$http_referer" '
	              '"$http_user_agent" "$http_x_forwarded_for"';
  include /data/conf/sites-enabled/nginx_*;

  server
  {
    listen  80;
    server_name  $NGINX_STATUS_URL;

    location / {
    stub_status on;
    access_log   off;
    }
  }
}
EOF

# 修改配置文件中的CPU核数
sed -i "s/worker_processes 8/worker_processes ${CPU_NUM}/g"  /data/apps/nginx/conf/nginx.conf

# 生成nginx虚拟机配置文件示例
cat << "EOF" > /data/conf/sites-available/nginx_main.conf
server
{
	listen       80;
	server_name  www.3mu.me;
	index index.html index.htm index.php;
	root  /www/wwwroot/web/3mu.me;

	#limit_conn   crawler  20;    
						 
	location ~ .*\.(php|php5)?$
	{      
		#fastcgi_pass  unix:/tmp/php-cgi.sock;
		fastcgi_pass  127.0.0.1:9000;
		fastcgi_index index.php;
		include fcgi.conf;
	}

	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
	{
		expires      30d;
	}

	location ~ .*\.(js|css)?$
	{
		expires      1h;
	}    

	access_log  /data1/logs/nginx/3mu.me.log;
}

server
{
	listen       80;
	server_name  soft.3mu.me;
	index index.html index.htm index.php;
	root  /www/wwwroot/web/soft;

	location ~ .*\.(php|php5)?$
	{      
		#fastcgi_pass  unix:/tmp/php-cgi.sock;
		fastcgi_pass  127.0.0.1:9000;
		fastcgi_index index.php;
		include fcgi.conf;
	}

	access_log  /data1/logs/nginx/soft.3mu.me.log;
}
EOF

# 生成配置文件软链接
ln -s /data/conf/sites-available/nginx_main.conf /data/conf/sites-enabled/


# 在/data/apps/nginx/conf/目录中创建fcgi.conf文件
cat << "EOF" > /data/apps/nginx/conf/fcgi.conf
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
EOF

# 添加nginx的service启动脚本
cat << "EOF" > /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
#
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /Data/apps/nginx/conf/nginx.conf
nginxd=/data/apps/nginx/sbin/nginx
nginx_config=/data/apps/nginx/conf/nginx.conf
nginx_pid=/data/apps/nginx/nginx.pid

RETVAL=0
prog="nginx"

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0


# Start nginx daemons functions.
start() {

if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi

echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL

}


# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}


# reload nginx service functions.
reload() {

echo -n $"Reloading $prog: "
$nginxd -s reload
#if your nginx version is below 0.8, please use this command: "kill -HUP `cat ${nginx_pid}`"
RETVAL=$?
echo

}

# See how we were called.
case "$1" in
start)
start
;;

stop)
stop
;;

reload)
reload
;;

restart)
stop
start
;;

status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac

exit $RETVAL
EOF

# 添加可执行权限
chmod +x /etc/init.d/nginx

# 启动Nginx
ulimit -SHn 65535
service nginx start
#/data/apps/nginx/sbin/nginx
# 测试虚拟机文件语法是否正常
# /data/apps/nginx/sbin/nginx -t

#编译安装mariadb-10.0.12##############################################################
cd /data/software

# 安装需要的开发包和cmake
yum -y groupinstall 'Server Platform Development'
yum -y groupinstall  'Development tools'
yum -y install readline-devel
yum -y install cmake
#yum install -y gunzip tar gcc g++ make libtool bison libncurses zlib-dev automake autoconf cmake
# 安装编译器cmake
#tar zxvf cmake-3.0.0.tar.gz
#cd cmake-3.0.0
#./configure
#make && make install  
#cd ../

tar zxvf mariadb-10.0.12.tar.gz
cd mariadb-10.0.12

# 要是重复运行,先清理此前的编译所生成的文件
make clean
rm CMakeCache.txt

# 建立目录
mkdir -pv /data/apps/mysql/tmp
mkdir -pv /data/wwwroot/mysql
#mkdir -pv /data/apps/mysql/etc

# 创建用户和组
/usr/sbin/groupadd -r mysql
/usr/sbin/useradd -g mysql -r -s /bin/false mysql

# 配置
cmake \
-DCMAKE_INSTALL_PREFIX=/data/apps/mysql \
-DMYSQL_DATADIR=/data/wwwroot/mysql/ \
-DSYSCONFDIR=/etc \
-DMYSQL_UNIX_ADDR=/data/apps/mysql/tmp/mysqld.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_TCP_PORT=$MYSQL_PORY \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_ARIA_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_FEDERATEDX_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DWITH_SPHINX_STORAGE_ENGINE=1 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=all \
-DEXTRA_CHARSETS=all \

make && make install

# 复制配置
cp support-files/my-huge.cnf  /etc/my.cnf
# 设置my.cnf的软链接,不知道什么原因,指定my.cnf的地址无效,只能放在系统默认的/etc目录下
#ln -s /data/apps/mysql/etc/my.cnf /etc/
# 赋权
chown -R mysql:mysql /data/apps/mysql

# 开机启动
cp support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql
chkconfig mysql on

# 修改my.cnf
sed '/mysqld]/a\pid-file = /data/wwwroot/mysql/mysql.pid' -i /etc/my.cnf
sed '/mysqld]/a\datadir = /data/wwwroot/mysql' -i /etc/my.cnf
sed '/mysqld]/a\basedir = /data/apps/mysql' -i /etc/my.cnf
sed '/mysqld]/a\user = mysql' -i /etc/my.cnf

sed -e '/#innodb_lock_wait_timeout = 50/a\
\
# name-resolve \
skip-name-resolve \
skip-host-cache \
\
# LOG \
log_error = /data/wwwroot/mysql/mysql.err\
long_query_time = 1 \
slow_query_log \
slow_query_log_file = /data1/logs/mysql/mysql-slow.log \
\
# Oher \
max_connections = 1000 \
open_files_limit = 65535' -i /etc/my.cnf

# 查看配置
cat /etc/my.cnf|grep -v '^#'|grep -v '^$'
# 初始化数据库
/data/apps/mysql/scripts/mysql_install_db --user=mysql --basedir=/data/apps/mysql --datadir=/data/wwwroot/mysql
chown -R :root /data/wwwroot/mysql
# 日志目录修改用户组
chown -R mysql:mysql /home/data1/logs/mysql

# 启动mariadb
/etc/init.d/mysql start
# 添加环境变量
export PATH=$PATH:/data/apps/mysql/bin
echo 'export PATH=$PATH:/data/apps/mysql/bin' >> /etc/profile
source /etc/profile

sed -i "s#/usr/local/mysql#/data/apps/mysql#g" /data/apps/mysql/bin/mysqld_safe

# 初始化密码
/data/apps/mysql/bin/mysqladmin -u root password 123456
# 修改root密码 / 删除初始非 root 的用户 / 删除空密码的root
# 删除test测试库 / 禁止root远程连接权限 / 刷新权限
cat >> /data/tmp/mysql_sec_script << DEB
use mysql;
update user set password=password('${MYSQL_ROOT_PAS}') where user='root';
delete from user where not (user='root') ;
delete from user where user='root' and password=''; 
drop database test;
DROP USER ''@'%';
flush privileges;
DEB
/data/apps/mysql/bin/mysql -u root -p123456 -h localhost < /data/tmp/mysql_sec_script
rm -f /data/tmp/mysql_sec_script

# 最后打包上传
#tar czf - /data/apps | ssh root@ip:port tar xzf - -C /data/apps

#安装PHP依赖库########################################################################
mkdir -p /data/apps/libs/
tar zxvf jpegsrc.v9.tar.gz
cd jpeg-9/
./configure --prefix=/data/apps/libs --enable-shared --enable-static --prefix=/data/apps/libs
make
make install
cd ../

tar zxvf libpng-1.6.2.tar.gz
cd libpng-1.6.2/
./configure --prefix=/data/apps/libs
make
make install
cd ../

tar zxvf freetype-2.4.12.tar.gz
cd freetype-2.4.12/
./configure --prefix=/data/apps/libs
make
make install
cd ../

tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure --prefix=/data/apps/libs
make
make install
cd libltdl/
./configure --prefix=/data/apps/libs --enable-ltdl-install
make
make install
cd ../../

tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure --prefix=/data/apps/libs
make
make install
cd ../

echo '/data/apps/libs/lib' >> /etc/ld.so.conf
ldconfig

tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
export LDFLAGS="-L/data/apps/libs/lib -L/usr/lib"
export CFLAGS="-I/data/apps/libs/include -I/usr/include"
touch malloc.h
./configure --prefix=/data/apps/libs --with-libmcrypt-prefix=/data/apps/libs
make
make install
cd ../
#编译安装PHP 5.5######################################################################
tar zxvf php-5.5.14.tar.gz
cd php-5.5.14/
export LIBS="-lm -ltermcap -lresolv"
export DYLD_LIBRARY_PATH="/data/apps/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"
export LD_LIBRARY_PATH="/data/apps/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"
./configure --prefix=/data/apps/php --with-config-file-path=/data/apps/php/etc --with-mysql=/data/apps/mysql --with-mysqli=/data/apps/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir=/data/apps/libs --with-jpeg-dir=/data/apps/libs --with-png-dir=/data/apps/libs --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt=/data/apps/libs --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-opcache --with-pdo-mysql --enable-maintainer-zts
make
make install
cp php.ini-production /data/apps/php/etc/php.ini
cd ../


ln -s /data/apps/mysql/lib/libmysqlclient.so.18.0.0 /usr/lib64/libmysqlclient.so.18
mv /data/apps/php/etc/php-fpm.conf.default /data/apps/php/etc/php-fpm.conf

# 打开pid = run/php-fpm.pid前的注释,这样fpm的进程就会被写入这个文件:/usr/local/php5/var/run/php-fpm.pid
sed -i 's#;pid = run/php-fpm.pid#pid = /data/apps/php/var/run/php-fpm.pid#' /data/apps/php/etc/php-fpm.conf

# 添加php5.5的service启动脚本
cat << "EOF" > /etc/init.d/php-fpm
#!/bin/bash
exec=/data/apps/php/sbin/php-fpm
config=/data/apps/php/etc/php-fpm.conf
pidfile=/data/apps/php/var/run/php-fpm.pid
start(){
	$exec -y $config || echo -n "start $exec -y $config failed."
}

stop(){
	kill -QUIT `cat $pidfile` || echo "stop $exec failed."
}

reload(){
	kill -HUP `cat $pidfile` || echo "reload $exec failed"
}

test(){
	$exec -t -y $config
}

status(){
	if [ -f $pidfile ] ; then
		if [ -f /proc/`cat $pidfile` ] ; then
			echo "$exec -y $config is running ..."
		else
			echo "$exec -y $config is stopped ..."
		fi
	fi
	echo "$exec -y $config is stopped ..."
}

case "$1" in
start)
echo "starting $exec ok"
start
;;
stop)
echo "stopping $exec ok"
stop
;;
reload)
test
reload
;;
restart)
echo "restarting $exec ok"
stop
start
;;
test)
test
;;
*)
echo "usage: $0 {start|stop|restart|reload|status|test}"
exit 3
;;
esac
exit 0
EOF

chmod +x /etc/init.d/php-fpm
service php-fpm start

#编译安装PHP扩展######################################################################
tar zxvf autoconf-latest.tar.gz
cd autoconf-2.69/
./configure --prefix=/data/apps/libs
make
make install
cd ../

tar zxvf memcache-2.2.7.tgz
cd memcache-2.2.7/
export PHP_AUTOCONF="/data/apps/libs/bin/autoconf"
export PHP_AUTOHEADER="/data/apps/libs/bin/autoheader"
/data/apps/php/bin/phpize
./configure --with-php-config=/data/apps/php/bin/php-config
make
make install
cd ../

# 修改php.ini文件,加入memcache.so扩展
sed -i 's#extension_dir = "./"#extension_dir = "ext/"\nextension = "memcache.so"\n#' /data/apps/php/etc/php.ini

# 启动PHP
service php-fpm restart

#配置开机自动启动Nginx + PHP##########################################################
#vi /etc/rc.local
echo 'ulimit -SHn 65535
service php-fpm start
/data/apps/nginx/sbin/nginx' >> /etc/rc.local

#####################################################################################
#####################################################################################
#配置文件目录设置######################################################################
#移动nginx主配置文件
if [ -s /data/conf/nginx.conf ]; then  
  echo "nginx.conf already move"  
else  
	cp -p /data/apps/nginx/conf/nginx.conf  /data/apps/nginx/conf/nginx.conf.bak
	mv /data/apps/nginx/conf/nginx.conf /data/conf/
	ln -s /data/conf/nginx.conf /data/apps/nginx/conf/
	echo "nginx.conf move success"  
fi

#移动nginx虚拟机配置文件
/data/conf/sites-available/nginx_main.conf

#移动mysql配置文件
if [ -s /data/conf/my.cnf ]; then  
  echo "my.cnf already move"  
else  
	cp -p /etc/my.cnf /etc/my.cnf.bak
	mv /etc/my.cnf /data/conf/
	ln -s /data/conf/my.cnf /etc/
	echo "my.cnf move success"  
fi

#移动php配置文件
if [ -s /data/conf/php.ini ]; then  
  echo "php.ini already move"  
else  
  cp -p /data/apps/php/etc/php.ini /data/apps/php/etc/php.ini.bak
  mv /data/apps/php/etc/php.ini /data/conf/
  ln -s /data/conf/php.ini /data/apps/php/etc/
  echo "php.ini move success"  
fi


#设置目录权限##########################################################################
chown -R www:www /data/wwwroot/web
  
#php配置################################################################################
#修改时区
#禁用的函数
#禁止显示php版本的信息
#禁止php脚本访问指定目录以外的目录
#禁止nginx与php5.4以下版本的安全漏洞
#不指定session路径
if cat /data/conf/php.ini |awk -F: '{print $1}'|grep 'date.timezone = PRC'  2>&1 >/dev/null 
then 
	echo -e "php.ini has been \e[0;32m\033[1madded\e[m." 
else 
	sed -i 's/;date.timezone \=/date.timezone \= PRC/
s/disable_functions \=/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/
s/expose_php = On/expose_php = Off/
s/;open_basedir =/open_basedir = \/tmp\/:\/var\/tmp\/:\/data\/wwwroot\/web\//
s/;cgi.fix_pathinfo=1 /cgi.fix_pathinfo = 0/
s/upload_max_filesize = 2M/upload_max_filesize = 5M/
s/max_input_time = 60/max_input_time = 30/
s/memory_limit = 128M/memory_limit = 64M/
s/error_reporting = E_ALL/error_reporting = E_ERROR/
s/session.save_path/;session.save_path/
s/display_errors = On/display_errors = Off/
s/log_errors = Off/log_errors = On/' /data/conf/php.ini
fi

#开启防火墙
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 443 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport $MYSQL_PORY -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables restart

#重启所有服务器
service nginx restart
service php-fpm restart
/etc/init.d/mysql restart
#/etc/init.d/memcached restart

#系统文件加锁
chattr +i /etc/passwd        
chattr +i /etc/shadow
chattr +i /etc/gshadow
chattr +i /etc/group
chattr +i /etc/services

脚本下载:compile_lnmp.rar

参考资料:
CentOS6.4 64位下编译安装nginx1.5.11+php5.5.10+mariaDB5.5.36:http://blog.sina.com.cn/s/blog_7b0d38140101kyxc.html
CentOS下编译安装PHP5.5:http://cuelog.com/archives/27.html
Linux平滑编译升级php至5.5.0:http://levi.cg.am/archives/2987
linux下nginx连接PHP5.5:http://hanqunfeng.iteye.com/blog/1983259
Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版):http://blog.zyan.cc/nginx_php_v6
Nginx 1.5.2 + PHP 5.5.1 + MySQL 5.6.10 在 CentOS 下的编译安装:http://zyan.cc/nginx_php_v7/

发表评论?

1 条评论。

  1. 这篇文章不错,写得很详细,我在CentOS6.5上运行一次通过,可以用于生产环境的安装部署。

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据