CentOS系统初始安装MySQL、PHP和Nginx的脚本

该脚本在CentOS5.5 64bit环境中测试通过,其他环境可以进行对应的修改。信息的配置解释请见 Linux下使用源代码编译方式安装MySQLLinux下源码方式安装Nginx和PHP(FastCGI)

#!/bin/sh
# by Jet Ma from www.javatang.com
# version v0.2 build20121011-1516
# Install Nginx, MySQL and PHP
# CentOS 5.5 64bit

# ----------- 配置信息 -----------
# MySQL安装目录
MYSQL_HOME='/usr/local/mysql'
# MySQL root账号的密码
MYSQL_ROOT_PASSWD='root'
# PHP安装目录
PHP_HOME='/usr/local/php'
# Nginx安装目录
NGINX_HOME='/usr/local/nginx'

# ----------- 检测是否是root账号 ----------- 
if [ $(id -u) != "0" ]; then
    echo -e "\e[1;31m This scripts MUST under root user or group!! \e[0m"
    echo
    exit 2
fi

# 首先打开账号权限文件
chattr -i /etc/passwd
chattr -i /etc/shadow
chattr -i /etc/group
chattr -i /etc/gshadow
# 关闭防火墙
/etc/init.d/iptables stop

# ----------- 安装需要的基础依赖库 ----------- 
echo -e "\e[1;36m 安装需要的基础依赖库 ... \e[0m"
LANG=C
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

# ----------- 安装MySQL ----------- 
echo -e "\e[1;36m 安装MySQL ... \e[0m"
cd ~
mkdir downloads
cd downloads
wget -c http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.65.tar.gz/from/http://cdn.mysql.com/
tar xzvf mysql-5.1.65.tar.gz
cd mysql-5.1.65
./configure --prefix=$MYSQL_HOME --with-charset=utf8 --with-extra-charsets=all --without-debug --enable-thread-safe-client --enable-assembler --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --without-isam --with-plugins=innobase
make && make install
# 设置启动的用户
groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql
cd $MYSQL_HOME
# 设置安装目录权限给mysql用户和用户组
chown -R mysql .
chgrp -R mysql .
# 安装数据库
bin/mysql_install_db --user=mysql
chown -R root .
chown -R mysql var
cp share/mysql/my-medium.cnf /etc/my.cnf
sed -i 's/skip-locking/skip-external-locking/g' /etc/my.cnf
cp share/mysql/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
 /etc/init.d/mysql start
 bin/mysqladmin -uroot password $MYSQL_ROOT_PASSWD
 history -c
 /etc/init.d/mysql restart

# ----------- 安装PHP与PHP-FPM ----------- 
echo -e "\e[1;36m 安装PHP与PHP-FPM ... \e[0m"
# 创建账号
groupadd www
useradd -s /sbin/nologin -M -g www www
# 下载libmcrypt,有两个文件
cd ~/downloads
wget -c "http://downloads.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz?ts=1347243907&use_mirror=ncu"
wget -c "http://downloads.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz?r=&ts=1347244005&use_mirror=nchc"
# 下载libmhash
wget -c "http://downloads.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2?ts=1347245105&use_mirror=ncu"
# 首先安装libmcrypt
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make && make install
ldconfig
cd libltdl/
./configure --enable-ltdl-install
make && make install
# 接下来安装mhash,因为mcrypt需要mhash的依赖
cd ../../
tar jxvf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9
./configure
make && make install
# 最后安装mcrypt
cd ..
tar xzxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
# 将mhash加入ld library路径中
export LD_LIBRARY_PATH=/usr/local/lib
ldconfig
./configure
make && make install
cd ..
# 下载PHP和对应版本的PHP-FPM
wget -c http://www.php.net/get/php-5.2.17.tar.gz/from/this/mirror
# 在http://php-fpm.org/downloads目录下选择与安装php版本一致的PHP-FPM,否则会引起兼容性问题
wget -c http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
# 如果是5.2.x的版本的话,还需要下载PHP Hash冲突的补丁,5.3+不需要下载了
wget -c https://github.com/laruence/laruence.github.com/raw/master/php-5.2-max-input-vars/php-5.2.17-max-input-vars.patch
tar xzvf php-5.2.17.tar.gz
# 将php-fpm以补丁的形式写入到php源码中
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
# 写入hash冲突补丁,php 5.3+不需要
patch -d php-5.2.17 -p1 < php-5.2.17-max-input-vars.patch
cd php-5.2.17
# 这里将php安装到/usr/local/php目录下面,引用的mysql安装目录为/usr/local/mysql,这里需要根据实际情况进行修改
./configure --prefix=$PHP_HOME --with-config-file-path=$PHP_HOME/etc --with-mysql=$MYSQL_HOME --with-mysqli=$MYSQL_HOME/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /usr/local/php/etc/php.ini
cd ..
# 编译安装PHP的扩展模块
cd ~/downloads
# 安装<a href="http://pecl.php.net/package/PDO_MYSQL" target="_blank">pdo_mysql</a>
wget -c http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2
$PHP_HOME/bin/phpize
./configure --with-php-config=$PHP_HOME/bin/php-config --with-pdo-mysql=$MYSQL_HOME
make && make install
cd ..
# 安装eAccelerator
wget -c https://github.com/downloads/eaccelerator/eaccelerator/eaccelerator-0.9.6.1.tar.bz2
tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1
$PHP_HOME/bin/phpize
./configure --enable-shared --with-php-config=$PHP_HOME/bin/php-config
make && make install
cd ..
# 修改php.ini文件来启用安装的第三方模块
sed -i -e "s:^extension_dir.*:extension_dir=\"$PHP_HOME/lib/php/extensions/no-debug-non-zts-20060613/\":g" $PHP_HOME/etc/php.ini
# 增加扩展
cat >> $PHP_HOME/etc/php.ini <<EOF
extension=eaccelerator.so
extension=pdo_mysql.so
[eaccelerator]
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
EOF

# 创建eaccelerator的临时目录
mkdir /tmp/eaccelerator
chmod 777 /tmp/eaccelerator

# ----------- 安装Nginx ----------- 
echo -e "\e[1;36m 安装Nginx ... \e[0m"
# 下载安装最新的pcre库
cd ~/downloads
wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.tar.gz
tar xzvf pcre-8.31.tar.gz
cd pcre-8.31
./configure
make && make install
cd ..
# 安装TCMalloc提升Nginx性能
# 如果是64位系统的话首先需要安装libunwind库,32位系统不需要安装。
wget -c http://download.savannah.gnu.org/releases/libunwind/libunwind-1.0.1.tar.gz
tar xzvf libunwind-1.0.1.tar.gz
cd libunwind-1.0.1
CFLAGS=-fPIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
cd ..
# 安装google-perftools
wget -c https://gperftools.googlecode.com/files/gperftools-2.0.tar.gz
tar xzvf gperftools-2.0.tar.gz
cd gperftools-2.0
./configure
make && make install
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig
cd ..
# 安装Nginx
wget -c http://nginx.org/download/nginx-1.2.3.tar.gz
tar xzvf nginx-1.2.3.tar.gz
cd nginx-1.2.3
./configure --prefix=$NGINX_HOME --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-google_perftools_module
make && make install
# 64位增加库的链接
ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1
ln -s /usr/local/lib/libprofiler.so /lib64/libprofiler.so.0
ln -s /usr/local/lib/libunwind.so /lib64/libunwind.so.8
# 启用TCMalloc
mkdir /tmp/tcmalloc
chmod 777 /tmp/tcmalloc
# 修改nginx.conf配置文件,在pid这行下面添加配置如下信息
sed -i -e 's:^#pid.*:&\ngoogle_perftools_profiles /tmp/tcmalloc;:g' $NGINX_HOME/conf/nginx.conf
# 增加日志自动切割脚本
echo '#!/bin/bash' > ${NGINX_HOME}/sbin/cutlogs.sh
echo "mkdir -p ${NGINX_HOME}/logs/\$(date +%Y)/\$(date +%m)
mv ${NGINX_HOME}/logs/access.log ${NGINX_HOME}/logs/\$(date +%Y)/\$(date +%m)/access.\$(date +%Y%m%d).log
mv ${NGINX_HOME}/logs/error.log ${NGINX_HOME}/logs/\$(date +%Y)/\$(date +%m)/error.\$(date +%Y%m%d).log

kill -USR1 \`cat ${NGINX_HOME}/logs/nginx.pid\`" >> ${NGINX_HOME}/sbin/cutlogs.sh
# 增加执行权限并添加到任务中
chmod +x ${NGINX_HOME}/sbin/cutlogs.sh
crontab -l > tmpcrontab
echo "00 00 * * * /bin/bash ${NGINX_HOME}/sbin/cutlogs.sh" >> tmpcrontab
crontab tmpcrontab
rm -f tmpcrontab

# 最后关闭账号权限文件
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/group
chattr +i /etc/gshadow
# 打开防火墙
/etc/init.d/iptables start

# ----------- 设置为开机启动 ----------- 
echo -e "\e[1;36m 将MySQL、PHP-FPM和Nginx设置为开机启动 ... \e[0m"
cat >> /etc/rc.local <<EOF
/etc/init.d/mysql start
ulimit -SHn 65535
$PHP_HOME/sbin/php-fpm start
$NGINX_HOME/sbin/nginx
EOF

cat << EOF
+--------------------------------------------------------------+
    已经安装完成MySQL、PHP和Nginx
    请使用 $PHP_HOME/bin/php -version 查看php是否成功运行并包含扩展
    请使用 $NGINX_HOME/sbin/nginx -t 查看nginx配置信息是否成功
    最后还需要对mysql、php-fpm和nginx配置文件进行优化处理!
+--------------------------------------------------------------+
EOF

参考资料:
用sed替换文本中环境变量的问题,Bash中可行,请高手指点在Csh中的使用方法
Linux下Sed命令的用法
linux下sed命令对文件执行文本替换

发表评论

电子邮件地址不会被公开。 必填项已用*标注