說明:
php在編譯安裝時,nginx要想能夠調用php提供動態php格式的網頁,必須用FastCGI來實現,但 FastCGI只是一個架構,實現FastCGI架構的有PHP-FPM,但對於5.2.x版本的php來說,預設是不支援PHP-FPM的,需要打上php-fpm的補丁,對於5.3.2之前版本的也是需要打補丁的,而且打補丁的過程比較麻煩。好在5.3.3版 本的PHP-FPM被直接做進了原始碼包中,在編譯安裝時只需啟用PHP-FPM功能即可。
但如果要使用PHP-FPM的話,還需要提供以下幾個功能:
需要提供可以解析xml格式的文檔,需要安裝libxml2 和libxml2-devel這兩個包,好在這兩個包在安裝完開發環境後這兩個包是預設安裝過的。
需要安裝libevent並且在1.4.12之後的版本,不幸的是rhel5.4版本中這個包是是在1.4.12之前的,需要從新手動編譯安裝該包。
libiconv 用來提供網路連接方式的功能組件,可以實現更快速的網路訪問,這個組件系統上是沒有裝的,需要手動編譯安裝。
構建編譯環境:
yum -y install gcc openssl-devel zlib-devel pcre-devel
yum groupinstall "Developement Tools" "Development Libraries" -yt
首先安裝Nginx:
wget http://nginx.org/download/nginx-1.0.14.tar.gz
tar zxvf nginx-1.0.14.tar.gz #
useradd -s /sbin/nologin -M nginx
cd nginx-1.0.14
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
make && make install
建立nginx的啟動指令碼:
vim /etc/init.d/nginxd
#!/bin/sh
#
# 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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
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 +x /etc/init.d/nginxd #給予指令碼執行許可權
chkconfig --add nginxd #加入開機啟動選項中
chkconfig nginxd on #設定開機自動啟動
service nginxd start #啟動nginx 服務
測試訪問:
安裝MySQL:
下載:
wget http://mysql.mirrors.hoobly.com/Downloads/MySQL-5.5/mysql-5.5.22.tar.gz
tar zxvf mysql-5.5.22.tar.gz
cd mysql-5.5.22-linux2.6-i686
/usr/sbin/groupadd mysql #添加mysql使用者
/usr/sbin/useradd -g mysql mysql #添加mysql組
編譯:
./configure --prefix=/usr/local/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innobase,myisammrg
make&&make install
編譯時間出現錯誤:
../depcomp: line 571: exec: g++: not found
make[1]: *** [my_new.o] Error 127
make[1]: Leaving directory `/root/lnmpsrc/mysql-5.1.62/mysys'
make: *** [all-recursive] Error 1
在其他安裝g++的伺服器上查看g++屬於哪個包:
[root@vps ~]# find / -name g++
/usr/bin/g++
[root@vps ~]# rpm -qf /usr/bin/g++
gcc-c++-4.4.6-3.el6.i686
可以看出g++屬於gcc-c++包
安裝gcc-c++:
[root@vps ~]#yum install gcc-c++ -y
重新編譯:
./configure --prefix=/usr/local/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innobase,myisammrg
make&&make install
改變/usr/local/mysql目錄使用者和屬組:
chown -R mysql.mysql /usr/local/mysql
建立mysql資料庫、日誌存放目錄
mkdir /mysql/{data,binlog,relaylog} -p
chown -R mysql.mysql /mysql
以mysql帳號建立資料表:
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/mysql/data --user=mysql
建立mysql設定檔:
vim /mysql/my.cnf
[client]
character-set-server = utf8
port = 3306
socket = /tmp/mysql.sock
[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
user = mysql
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /mysql/data
log-error = /mysql/mysql_error.log
pid-file = /mysql/mysql.pid
open_files_limit = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
#thread_concurrency = 8
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 3
log-slave-updates
log-bin = /mysql/data/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /mysql/relaylog/relaylog
relay-log-info-file = /mysql/relaylog/relaylog
relay-log = /mysql/relaylog/relaylog
expire_logs_days = 30
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve
#master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
#master-host = 192.168.1.1
#master-user = username
#master-password = password
#master-port = 3306
server-id = 1
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
#log-slow-queries = /mysql/slow.log
#long_query_time = 10
[mysqldump]
quick
max_allowed_packet = 32M
管理mysql指令碼:
vim /mysql/mysqld
#!/bin/sh
mysql_port=3306
mysql_username="admin" #帳號密碼可以自行建立
mysql_password="rootisnosafe"
function_start_mysql()
{
printf "Starting MySQL...\n"
/bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/mysql/my.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
/usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 5
function_start_mysql
}
function_kill_mysql()
{
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}
if [ "$1" = "start" ]; then
function_start_mysql
elif [ "$1" = "stop" ]; then
function_stop_mysql
elif [ "$1" = "restart" ]; then
function_restart_mysql
elif [ "$1" = "kill" ]; then
function_kill_mysql
else
printf "Usage: /mysql/mysqld {start|stop|restart|kill}\n"
fi
賦予指令碼執行許可權:
chmod +x /mysql/mysqld
啟動mysql
/mysql/mysqld start
命令列管理mysql:
/usr/local/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
建立一個具有root許可權的使用者:admin,密碼為rootisnosafe
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'rootisnosafe';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY 'rootisnosafe';
安裝php:
先安裝libevent和libiconv:
wget https://github.com/downloads/libevent/libevent/libevent-1.4.14b-stable.tar.gz
tar zxvf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure&&make&&make install
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure
make
make install
ln -sf /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2
現在安裝php:
wget http://cn.php.net/distributions/php-5.4.0.tar.gz
tar zxvf php-5.4.0.tar.gz
cd php-5.4.0
./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --enable-fpm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-iconv-dir=/usr/local
make ZEND_EXTRA_LIBS='-liconv' #因為-liconv的目錄不是在/usr/local下所以安裝時需要手動指定
make install
其中:
–with-mysql和–with-mysqli的路徑是你mysql的具體所在的目錄
–enable-fpm 啟動fpm。其他都是些基本選項,簡單易懂
cp php.ini-production /usr/local/php/etc/php.ini
修改設定檔:
vim /usr/local/php/etc/php-fpm.conf
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 35
啟動:
/usr/local/php/sbin/php-fpm &
檢查是否正常啟動:
netstat -tunlp|grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 7826/php-fpm
將 /usr/local/php/sbin/php-fpm &加入到rc.local:
echo '/usr/local/php/sbin/php-fpm &' >>/etc/rc.local
配置fastcgi_params 檔案:
vim /etc/nginx/fastcgi_params
將內容替換為:
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;
最後修改nginx.conf設定檔:
vim /etc/nginx/nginx.conf
location ~ \.php$ {
root /www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
location / {
root /www;
index index.php index.html index.htm;
}
儲存退出。
啟動nginx :
service nginxd start
編輯/www/index.php:
<?php
phpinfo();
?>
訪問測試:
測試資料庫連接:
編輯:/www/index.php
<?php
$link=mysql_connect("localhost","admin","rootisnosafe");
if($link) echo "OK";
else echo "FAIL";
?>
重新整理訪問。
如果出現OK字樣,表示串連正常。