Nginx
nginx的配置運行需要pcre、zlib等軟件包支持。所以先搭建好環境,確保nginx安裝順利。以紅冒企業版RHEL6爲例,搭建Nginx。
yum -y install pcre-devel zlib-devel
創建運行用戶、組
useradd -M -s /sbin/nologin nginx
解壓文件,編譯安裝。指定安裝用戶、組、模塊等
tar zxf nginx-1.0.8.tar.gz cd nginx-1.0.8 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx —with-http_stub_status_module make && make install
爲主程序nginx創建
在瀏覽器上輸入服務器IP,可以看到如下,說明nginx起來了!
netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5612/nginx: master
編寫Nginx服務腳本,用來啓動、停止、重載等操作。
vim /etc/init.d/nginx
#!/bin/bash
#chkconfig : -99 20
#description: Ngins Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
echo "nginx service start!!"
;;
stop)
kill -s QUIT $(cat $PIDF)
echo "nginx service stop!!"
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
echo "stop...start...!!"
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
;;
esac
exit 0
chmod +x /etc/init.d/nginx chkconfig --add nginx
2.配置文件nginx.conf
配置文件位於/usr/local/nginx/conf
vim /usr/local/nginx/conf/nginx.conf
#user nobody;//運行用戶
worker_processes 1;//工作進程數量
#error_log logs/error.log;//錯誤日誌
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;//PID文件位置
//內核2.6以上,建議使用epoll模型提高性能。根據實際來定,一般10 000一下,默認1024
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;//訪問日誌位置
sendfile on;//支持文件下載
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;//鏈接超時
#gzip on;
server {
listen 80;//監聽端口
server_name localhost;//網站名稱
charset utf-8;//頁面默認字符集
#access_log logs/host.access.log main;
location / {//根目錄配置
root html;//網站根目錄的位置
index index.html index.htm;//默認首頁
}
location ~ /status {//編譯時添加了模塊,所以啓用模塊可以統計狀態
stub_status on;
access_log off;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;//錯誤頁
location = /50x.html {
root html;
}
}
通常有以上配置即可。
保存退出。重啓服務。
service nginx stop service nginx start
可以在瀏覽器上查看網站狀態:
nginx虛擬主機的配置。
與apache一樣,都可以配置基於IP、基於域名、基於端口的虛擬主機
現基於域名,DNS的配置不再羅嗦。
建立兩個根目錄、測試首頁
mkdir -p /wwwroot/test echo "<h1>here is test.com!!</h1>" > /wwwroot/test/index.html mkdir -p /wwwroot/web/ echo "<h1>web~~web~~com~~</h1>" > /wwwroot/web/index.html
編輯nginx.conf配置文件
vim /usr/local/nginx/conf/nginx.conf
//虛擬主機配置在server{}上,一臺虛擬主機爲一個servier{}.
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.130.132:80;//虛擬主機IP
server_name www.test.com;//網站名稱
charset utf-8;
location / {
root /wwwroot/test;//網站目錄
index index.html index.htm;
}
location ~ /status {//統計網站狀態,可以省略
stub_status on;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.130.132:80;//虛擬主機IP
server_name www.web.com;//網站名稱
charset utf-8;
location / {
root /wwwroot/web;//網站目錄
index index.html index.htm;
}
location ~ /status {
stub_status on;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
保存退出,重啓服務後在瀏覽器上驗證。
基於域名的虛擬主機配置完畢。如果基於IP、端口的,配置大同小異。此處略。
LEMP構架
LEMP,既是用nginx做服務器,和lamp的意思一樣,因爲nginx發音緣故,所以是LEMP。
刪除原先rpm包安裝的httpd、myql、php等,避免衝突
rpm -e httpd httpd-manual webalizer subversion mod_python mod_ssl mod_perl system-config-httpd php php-cli php-ldap php-common mysql dovecot --nodeps
解壓mysql,並編譯安裝
tar zxf mysql-5.1.55.tar.gz cd mysql-5.1.55 ./configure --prefix=/usr/local/mysql --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=gbk,gb2312 && make && make install
優化調整
cp support-files/my-medium.cnf /etc/my.cnf cp support-files/mysql.server /etc/init.d/mysqld chmod a+x /etc/rc.d/init.d/mysqld chkconfig --add mysqld ln -s /usr/local/mysql/bin/* /usr/local/bin/ ln -s /usr/local/mysql/lib/mysql/* /usr/lib/ ln -s /usr/local/mysql/include/mysql/* /usr/include/
初始化數據庫
useradd -M -u 27 -s /sbin/nologin mysql cd /usr/local/mysql/bin/ ./mysql_install_db --user=mysql chown -R root.mysql /usr/local/mysql/ chown -R mysql /usr/local/mysql/var/
啓動數據庫
service mysqld start
爲root用戶設置密碼:
mysqladmin -u root password 'pwd123'
//若彈出error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)錯誤,檢查/tmp/有沒有文件,沒有就創建軟鏈。
ln -s /home/mysql/data/my.sock /tmp/mysql.sock
安裝PHP解析環境
tar zxf php-5.3.6.tar.gz cd php-5.3.6 ./configure --prefix=/usr/local/php5 --with-gc --with-zlib --with-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm && make && make install
安裝過程中,如果出現錯誤,多半是環境沒搭建好,一般按照提示,裝上需要的環境即可。
安裝後調整
cp php.ini-development /usr/local/php5/php.ini ln -s /usr/local/php5/bin/* /usr/local/bin/ ln -s /usr/local/php5/sbin/* /usr/local/sbin
安裝ZendGuardLoader,提高PHP解析效率。
tar zxf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz cd ZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x/ cp ZendGuardLoader.so /usr/local/php5/lib/php/ vim /usr/local/php5/php.ini
......//略
zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
zend_loader.enable=1
保存退出
配置Nginx支持PHP環境
啓用PHP-fpm進程
cd /usr/local/php5//etc/ cp php-fpm.conf.default php-fpm.conf
編輯php-fpm.conf,找到一下幾項並修改。
vim php-fpm.conf
......//略
pid = run/php-fpm.pid//確認pid文件位置
user = nginx//運行用戶
group = nginx//運行組
pm.start_servers = 20//啓動時開啓到進程數
pm.min_spare_servers = 5//最少空間進程數
pm.max_spare_servers = 35//最多空間進程數
保存退出。
啓動
/usr/local/sbin/php-fpm -c /usr/local/php5/php.ini netstat -anpt | grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 13149/php-fpm.conf)
在nginx啓動腳本上修改,使nginx啓動或關閉時,php-fpm頁自動啓動或關閉。
#!/bin/bash
#kconfig : -99 20
#description: Ngins Service And PHP-fpm Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
PROG_FPM="/usr/local/sbin/php-fpm"
PIDF_FPM="/usr/local/php5/var/run/php-fpm.pid"
case "$1" in
start)
$PROG
$PROG_FPM -c $PIDF_FPM
echo "nginx service and php-fpm start!!"
;;
stop)
kill -s QUIT $(cat $PIDF)
kill -s QUIT $(cat $PIDF_FPM)
echo "nginx service and php-fpm stop!!"
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
echo "stop...start...!!"
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
;;
esac
exit
配置nginx支持PHP解析,有兩種方法,但都是在server中配置!!
第一種方法:轉交給其他WEB服務器處理,比如交給LAMP服務器(192.168.1.254:80)處理
vim /usr/local/nginx/conf/nginx.conf
http{
......略
server {
......略
location ~ \.php${//訪問.php頁面到配置段
proxy_passhttp://192.168.1.254:80;
//Apache服務器到監聽地址
}
......略
......略 }
}
第二種方法:調用本機php-fpm進程。
vim /usr/local/nginx/conf/nginx.conf
http{
......略
server {
......略
location ~ \.php$ {
root/var/www/test;//訪問.php頁面配置段
fastcgi_pass127.0.0.1:9000;//PHP網頁文檔根目錄
fastcgi_indexindex.php;//php-fpm到監聽地址
includefastcgi.conf;//包括fastcgi.conf樣本配置
}
......略
}
......略
}
現以第二種方法實驗!!!!!
測試PHP頁面訪問
vim /wwwroot/test/test.php
<?php
$link=mysql_connect('localhost','root','pwd123');
if ($link) echo "<h1>鏈接數據庫成功!!</h1>";
mysql_close();
?>
保存退出。如果打開網頁發現錯誤:
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
1.殺掉所有mysql進程,重啓服務。
ps -A |grep mysql
kill -9 xxxx
service mysqld restart
2.修改權限,重啓服務
chown -R mysql.mysql /var/lib/mysql
service mysqld restart
3.修改/etc/my.cf (根據名稱,可能是my.conf)
[mysql]
socket=/tmp/mysql.sock
保存退出。
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
重啓服務
service mysqld restart
看看即可!