安裝lnmpphp
環境:
html
[root@scj ~]# cat /etc/issue (系統)mysql
CentOS release 6.4 (Final)linux
Kernel \r on an \mnginx
[root@scj ~]# uname -r (內核)c++
2.6.32-358.el6.i686sql
Selinux和Iptables:數據庫
關閉selinux:json
vi /etc/sysconfig/selinuxvim
#找到SELINUX=****改爲SELINUX=disabled
關閉防火牆:
iptables -F
/etc/init.d/iptables stop
chkconfig iptables off
安裝必要的庫和依賴包:
yum -y install patch cmake make gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal nano fonts-chinese gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap
安裝nginx:
##安裝依賴包: ##只安裝nginx時安裝用: yum -y install gcc gcc-c++ autoconf automake yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
cd /usr/local/src/
安裝pcre庫:
使nginx支持rewrite URL地址重寫,先安裝pcre庫:
wget http://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.tar.gz
tar -zxf pcre-8.35.tar.gz
cd pcre-8.35
./configure (不用指定--prefix)
make
make install
安裝nginx:
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar -zxf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --with-http_gunzip_module --with-http_realip_module
make
make install
啓動nginx:
/usr/local/nginx/sbin/nginx (此命令爲啓動nginx)
注意:在啓動的時候可能會報下面這個錯誤:
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解決辦法:
[root@www nginx-1.6.2]# find / -name libpcre.so.1
/usr/local/src/pcre-8.35/.libs/libpcre.so.1
/usr/local/lib/libpcre.so.1
找到庫文件的位置,而後將其添加到/etc/ld.so.conf這個文件裏:
vi /etc/ld.so.conf
添加一行:/usr/local/lib
執行:ldconfig
再執行:/usr/local/nginx/sbin/nginx (啓動)
/usr/local/nginx/sbin/nginx -s stop(reload) #中止
查看是否啓動:
[root@www lib]# netstat -tlnp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13765/nginx
安裝mysql:
cd /usr/local/src/
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.22.tar.gz
tar -zxf mysql-5.6.22.tar.gz
cd mysql-5.6.22
mkdir -p /opt/mysql/data (建立數據目錄)
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/opt/mysql/data -DSYSCONFDIR=/usr/local/mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS:STRING=utf8,gbk -DWITH_DEBUG=0
make (這個過程可能會很慢,請耐心等待)
make install
groupadd mysql (建立mysql用戶組)
useradd -s /sbin/nologin -g mysql mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/opt/mysql/data --defaults-file=/usr/local/mysql/my.cnf --user=mysql (初始化數據庫)
chown -R mysql.mysql /opt/mysql (對數據目錄受權)
vi /usr/local/mysql/my.cnf (修改配置文件)
添加:datadir=/opt/mysql/data
basedir=/usr/local/mysql
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld (複製啓動腳本)
chkconfig --add mysqld
chkconfig mysqld on
/etc/init.d/mysqld start (啓動mysql)
[root@www mysql]# netstat -tlnp | grep mysql
tcp 0 0 :::3306 :::* LISTEN 28839/mysqld
vi /etc/profile (修改PATH路徑)
添加:export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile
mysqladmin -u root password "123456" (給root建立密碼)
注意:執行此命令可能會報下面的錯誤:
[root@www mysql]# mysqladmin -u root password "123456"
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysqld.sock' exists!
解決方法:
[root@scj mysql-5.6.22]# ps -ef | grep mysql
root 3225 1 0 11:00 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/mysql/data --pid-file=/opt/mysql/data/scj.51.com.pid
mysql 3449 3225 1 11:00 pts/0 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/opt/mysql/data/scj.51.com.pid --socket=/var/lib/mysql/mysql.sock
root 3498 10386 0 11:02 pts/0 00:00:00 grep mysql
/etc/init.d/mysqld stop
vi /usr/local/mysql/my.cnf
添加:[client]
socket=/var/lib/mysql/mysql.sock (socket文件的路徑,由上面可知)
/etc/init.d/mysqld start (OK)
注意:若是還不行能夠嘗試:ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
mysql -u root -p123456 (登錄myqsl)
安裝php:
yum -y install make gcc-c++ gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel libjpeg-devel libpng-devel libvpx-devel
yum -y install patch cmake make gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal nano fonts-chinese gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap
cd /usr/local/src/
安裝php的依賴庫:
安裝libmcrypt:
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
tar -zxf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make
make install
ldconfig
cd /usr/local/src/libmcrypt-2.5.7/libltdl/
./configure --enable-ltdl-install
make
make install
vi /etc/ld.so.conf
添加:/usr/local/lib/
ldconfig
安裝php:
wget http://mirrors.sohu.com/php/php-5.5.10.tar.gz
tar -zxf php-5.5.10.tar.gz
cd php-5.5.10
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-pdo-sqlite --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock --with-mcrypt --with-mhash --with-iconv-dir=/usr/local --with-gd --with-gettext --with-freetype-dir --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr --with-curl --with-xmlrpc --with-zlib --with-pear --with-openssl --with-libdir --with-kerberos --enable-cgi --enable-fpm --enable-pdo --enable-opcache --enable-mbstring --enable-gd-native-ttf --enable-xml --enable-exif --enable-zip --enable-soap --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-shmop --enable-json --enable-bcmath --enable-calendar --enable-ftp --enable-inline-optimization --enable-pcntl --enable-mbregex --disable-rpath --enable-filter
使用mysqlnd驅動鏈接,不須要安裝mysql也可安裝php
參考:http://zhangxugg-163-com.iteye.com/blog/1894990
注意:這裏可能會出現下面報錯:
configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no
解決方法:
ldconfig
cd /usr/local/src/libmcrypt-2.5.7/libltdl/
./configure --enable-ltdl-install
make
make install
執行後會出現以下所示:
vi /etc/ld.so.conf
添加:/usr/local/lib/ (上面添加過了)
ldconfig (這一步必定要執行,從新加載庫文件,不然編譯php會報錯)
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la >/dev/null 2>&1
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so >/dev/null 2>&1
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4 >/dev/null 2>&1
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8 >/dev/null 2>&1
(上面這4行能夠不用執行)
而後再:
cd /usr/local/src/php-5.5.10
執行./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-pdo-sqlite --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock --with-mcrypt --with-mhash --with-iconv-dir=/usr/local --with-gd --with-gettext --with-freetype-dir --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr --with-curl --with-xmlrpc --with-zlib --with-pear --with-openssl --with-libdir --with-kerberos --enable-cgi --enable-fpm --enable-pdo --enable-opcache --enable-mbstring --enable-gd-native-ttf --enable-xml --enable-exif --enable-zip --enable-soap --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-shmop --enable-json --enable-bcmath --enable-calendar --enable-ftp --enable-inline-optimization --enable-pcntl --enable-mbregex --disable-rpath --enable-filter
make (這個過程可能會很慢,請耐心等待)
make install
cp -a /usr/local/src/php-5.5.10/php.ini-production /usr/local/php/etc/php.ini (拷貝文件)
cp -a /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vim /usr/local/php/etc/php-fpm.conf
將pid = run/php-fpm.pid註釋去掉
cp -a /usr/local/src/php-5.5.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
/etc/init.d/php-fpm start #啓動php-fpm
[root@localhost php]# /etc/init.d/php-fpm -h Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status}
##參考: [root@www etc]# netstat -tlnp | grep php (查看是否啓動) tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 3222/php-fpm 注意: [root@scj ~]# ps -ef | grep php #找到php的主進程號 root 12161 1 0 05:48 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) kill -SIGUSR2 12161 #從新啓動php(相似reload) kill -SIGINT 12161 #馬上中止進程 kill -SIGQUIT 12161 #平滑中止進程 SIGINT, SIGTERM 馬上中止進程 SIGQUIT 平滑終止進程 SIGUSR1 從新打開日誌文件 SIGUSR2 平滑重載全部worker進程並從新載入配置和二進制模塊(相似reload)
nginx與php整合:
整合:
cd /usr/local/nginx/conf/
vi nginx.conf
找到以下幾行,去掉註釋,並修改,注意紅色部分:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
如圖:
/usr/local/nginx/sbin/nginx -t (檢查配置文件是否正確)
/usr/local/nginx/sbin/nginx -s reload (reload從新加載配置文件,關閉並啓動新的worker進程)
測試:
vi /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
打開瀏覽器,訪問:192.168.186.129/index.php
如圖:
附加:
使用/etc/init.d/nginx腳本統一管理nginx,php的啓動,關閉,重啓等等:
vi /etc/init.d/nginx
#!/bin/bash # #nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/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/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 /usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm.conf echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " pkill -9 php-fpm pkill -9 php-fpm killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval killall -9 nginx } restart() { configtest || return $? stop sleep 1 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
chmod 755 /etc/init.d/nginx
chkconfig nginx on