#!/bin/bash

#关闭SElinux

setenforce 0

sed -i 's/SELINUX=Enforcing/SELINUX=disabled/' /etc/sysconfig/selinux




#安装软件环境以及依赖包

yum -y install make apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch bison libxml2-static zlib-static


#我的.tar.gz都放在/opt/下面:cmake-2.8.8.tar.gz,libmcrypt-2.5.8.tar.gz,mysql-5.5.25.tar.gz,pcre-8.31.tar.gz,php-5.4.5.tar.gz

#安装cmake

cd /opt/

tar -zxvf cmake-2.8.8.tar.gz

cd cmake-2.8.8

./configure

make && make install


#安装mysql

groupadd mysql

useradd -g mysql mysql -s /sbin/nologin

mkdir -p /data/mysql

chown -R mysql:mysql /data/mysql

mkdir -p /usr/local/mysql


cd /opt/

tar -zxvf mysql-5.5.25.tar.gz

cd /opt/mysql-5.5.25

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc

make && make install

cd /opt/mysql-5.5.25

cp support-files/my-medium.cnf /etc/my.cnf

sed -i 's/\[mysqld]/& \n\datadir\=\/data\/mysql/g' /etc/my.cnf

chmod 777 ./scripts/mysql_install_db

scripts/mysql_install_db  --user=mysql  --basedir=/usr/local/mysql --datadir=/data/mysql/

\cp support-files/mysql.server /etc/init.d/mysqld

chmod 755 /etc/rc.d/init.d/mysqld

chkconfig mysqld on

service mysqld start





#将mysql服务加入到系统环境变量

echo 'export PATH=$PATH:/usr/local/mysql/bin' >>/etc/profile

source /etc/profile


ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql

ln -s /usr/local/mysql/include/mysql /usr/include/mysql





cd /opt/

tar -zxvf pcre-8.31.tar.gz

cd pcre-8.31

./configure --prefix=/usr/local/pcre

make && make install


cd /opt/

groupadd www

useradd -g www -s /sbin/nologin www

tar -zxvf nginx-1.2.2.tar.gz

cd nginx-1.2.2

./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/opt/pcre-8.31

make && make install


cd /opt/

mv -f nginx.bak /etc/rc.d/init.d/nginx

chmod 755 /etc/rc.d/init.d/nginx

chkconfig nginx on

/etc/rc.d/init.d/nginx restart



cd /opt/

tar -zxvf libmcrypt-2.5.8.tar.gz

cd libmcrypt-2.5.8

./configure && make && make install



cd /opt/

tar -zxvf php-5.4.5.tar.gz

cd php-5.4.5

./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-gd --with-iconv --with-zlib --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl


./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql=mysqlnd --with-gd --with-iconv --with-zlib --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl


make && make install

\cp php.ini-production /usr/local/php5/etc/php.ini

rm -rf /etc/php.ini

ln -s /usr/local/php5/etc/php.ini /etc/php.ini

\cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf


#设置php-fpm运行账号与组为www

sed -i 's/user = nobody/user = www/' /usr/local/php5/etc/php-fpm.conf

sed -i 's/group = nobody/group = www/' /usr/local/php5/etc/php-fpm.conf

sed -i 's/;pid = run\/php-fpm.pid/& \n\pid = run\/php-fpm.pid/g' /usr/local/php5/etc/php-fpm.conf



\cp /opt/php-5.4.5/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

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

chkconfig php-fpm on

service php-fpm start


sed -i 's/disable_functions =/disable_function = 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/' /usr/local/php5/etc/php.ini

sed -i 's/;date.timezone =/date.timezone = PRC/' /usr/local/php5/etc/php.ini

sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/php5/etc/php.ini

sed -i 's/expose_php = On/expose_php = off/g' /usr/local/php5/etc/php.ini

sed -i 's/#user nobody;/& \nuser  www www;/g' /usr/local/nginx/conf/nginx.conf

sed -i 's/\index.html/&\ index.php/g' /usr/local/nginx/conf/nginx.conf

sed -i '/FastCGI/a\ location ~ \.php$ {\nroot html;\nfastcgi_pass 127.0.0.1:9000;\nfastcgi_index index.php;\nfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\ninclude fastcgi_params;\n}' /usr/local/nginx/conf/nginx.conf



/etc/init.d/nginx restart


chown 700 /usr/local/nginx/html/ -R

echo "LNMP is alreday install!"