1.環境准備
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
yum install nginx
2.下載 nginx
wget http://nginx.org/download/nginx-1.2.1.tar.gz
tar –xzvf nginx-1.2.1.tar.gz
cd nginx-1.0.2
./configure--sbin-path=/root/soft/nginx
configure 支持下面的選項:
--prefix=<path> - Nginx安裝路徑。如果沒有指定,默認為 /usr/local/nginx。
--sbin-path=<path> - Nginx可執行文件安裝路徑。只能安裝時指定,如果沒有指定,默認為<prefix>/sbin/nginx。
--conf-path=<path> - 在沒有給定-c選項下默認的nginx.conf的路徑。如果沒有指定,默認為<prefix>/conf/nginx.conf。
--pid-path=<path> - 在nginx.conf中沒有指定pid指令的情況下,默認的nginx.pid的路徑。如果沒有指定,默認為 <prefix>/logs/nginx.pid。
--lock-path=<path> - nginx.lock文件的路徑。
--error-log-path=<path> - 在nginx.conf中沒有指定error_log指令的情況下,默認的錯誤日志的路徑。如果沒有指定,默認為 <prefix>/logs/error.log。
--http-log-path=<path> - 在nginx.conf中沒有指定access_log指令的情況下,默認的訪問日志的路徑。如果沒有指定,默認為 <prefix>/logs/access.log。
--user=<user> - 在nginx.conf中沒有指定user指令的情況下,默認的nginx使用的用戶。如果沒有指定,默認為 nobody。
--group=<group> - 在nginx.conf中沒有指定user指令的情況下,默認的nginx使用的組。如果沒有指定,默認為 nobody。
--builddir=DIR - 指定編譯的目錄
--with-rtsig_module - 啟用 rtsig 模塊
--prefix=<path> - Nginx安裝路徑。如果沒有指定,默認為 /usr/local/nginx。
--sbin-path=<path> - Nginx可執行文件安裝路徑。只能安裝時指定,如果沒有指定,默認為<prefix>/sbin/nginx。
--conf-path=<path> - 在沒有給定-c選項下默認的nginx.conf的路徑。如果沒有指定,默認為<prefix>/conf/nginx.conf。
--pid-path=<path> - 在nginx.conf中沒有指定pid指令的情況下,默認的nginx.pid的路徑。如果沒有指定,默認為 <prefix>/logs/nginx.pid。
--lock-path=<path> - nginx.lock文件的路徑。
--error-log-path=<path> - 在nginx.conf中沒有指定error_log指令的情況下,默認的錯誤日志的路徑。如果沒有指定,默認為 <prefix>/logs/error.log。
--http-log-path=<path> - 在nginx.conf中沒有指定access_log指令的情況下,默認的訪問日志的路徑。如果沒有指定,默認為 <prefix>/logs/access.log。
--user=<user> - 在nginx.conf中沒有指定user指令的情況下,默認的nginx使用的用戶。如果沒有指定,默認為 nobody。
--group=<group> - 在nginx.conf中沒有指定user指令的情況下,默認的nginx使用的組。如果沒有指定,默認為 nobody。
--builddir=DIR - 指定編譯的目錄
--with-rtsig_module - 啟用 rtsig 模塊
3.最后安裝
make && make install
我們測試一下nginx是否正常工作。因為我們把nginx的二進制執行文件配置安裝在 /usr/local/sbin/nginx (前面的configure參數 --sbin-path=/usr/local/sbin/nginx ),目錄/usr/local/sbin/默認在linux的PATH環境變量里,所以我們可以省略路徑執行它:
[root@fsvps nginx-1.2.1]# nginx
它不會有任何信息顯示。打開瀏覽器,通過IP地址訪問你的nginx站點,如果看到這樣一行大黑字,就證明nginx已經工作了:
Welcome to nginx!
看上去很簡陋,只是因為沒有給它做漂亮的頁面。我們來簡單配置一下它的運行配置。我們configure的參數--conf-path=/usr/local/conf/nginx/nginx.conf 這就是它的配置文件。我們去看看。
[root@fsvps nginx-1.2.1]# cd /usr/local/conf/nginx/
[root@fsvps nginx]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
如果這里沒有nginx.conf,但有個 nginx.conf.default,我們用它復制個副本nginx.conf (早期版本默認是沒有nginx.conf的)
用你熟悉的編輯器打開它,vi nginx.conf
找到如下的部分,大概在35行,
server {
listen 80
default;
server_name localhost;
root /var/www/html/default;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
index index.html index.htm;
}
....................
}
如上紅色加粗顯示部分,做三處修改:
listen 80 后插入“空格default”
在里面加入一行 root /var/www/html/default;
注釋掉root html;一行
上面的server{....}是定義的一台虛擬主機,我們剛加入的一行是定義這個虛假主機的web目錄。listen 行的default是表示這一個server{...}節點是默認的虛假主機(默認站點)
執行 nginx -t 測試剛才的nginx配置文件是否有語法錯誤:
[root@fsvps nginx]# nginx -t
nginx: the configuration file /usr/local/conf/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/conf/nginx/nginx.conf test is successful
顯示沒有錯誤,檢測通過。如果不是這樣,請返回仔細檢查你的配置文件是否哪里寫錯了,注意行尾要有英文分號。
我們在硬盤上創建這個目錄:
[root@fsvps nginx]# mkdir -p /var/www/html/default
寫一個html文件index.html 到/var/www/html/default目錄里,使用你熟悉的編輯器,隨便寫什么內容、怎么排版。
然后讓nginx重新加載配置文件,看看剛才創作的html頁面效果如何。
常見錯誤FAQ
1) 如果通常訪問時看到還是 Welcome to nginx! ,請返回檢查,重點在這幾處:
請確認/var/www/html/default 目錄下有你創作的index.html 文件?
檢查該文件權限,other用戶是否有讀的權限? 如果不懂linux文件權限,請執行 chmod 755 /var/www/html/default/index.html
檢查nginx.conf配置文件里,只否只有一個server{...} 節點,並且該節點里是否有 listen 80 default; 一行,注意其中要有 default 。
檢查上述server{...}節點里是否有 root /var/www/html/default; 一行,注意路徑是拼寫是否正確。
檢查其中 location / {...} 節點里的 # root html; 一行,是否注釋掉了。
2) 如果看到的是 404 Not Found 或者“找不到該頁面”類似的提示:
檢查上述 location / {...} 節點中是否有這一行 index index.html index.htm;
3) 如果訪問時,顯示“找不到服務器”、“無法連接到服務器”這樣的錯誤:
運行檢查nginx進程在運行,運行ps aux |grep nginx 命令,正常情況有如下三條:
nginx: master process nginx
nginx: worker process
grep nginx
如果只有第三條,請運行nginx 重新啟用nginx,如有報錯請照說明檢查。一般是配置文件的語法錯誤。
請運行nginx -t 檢查配置文件是否有語法錯誤。
[tips] location / {...} 節點里的 # root html; 一行,不注釋掉也可以:請把你創造的index.html 放到/var/www/html/default/html目錄下。
至此,我們的nginx也可以正常工作了。
查看nginx進程
ps –ef | grep nginx
4.設置成系統開機服務:
在 /etc/init.d/ 目錄下創建 nginx 文件 內容如下:
在 /etc/init.d/ 目錄下創建 nginx 文件 內容如下:
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# this script create it by gcec at 2009.10.22.
# it is v.0.0.1 version.
# if you find any errors on this scripts,please contact gcec cyz.
# and send mail to support at gcec dot cc.
#
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/app/nginx/sbin/nginx
nginx_config=/app/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[-x $nginxd ]||exit0
# Start nginx daemons functions.
start() {
if[-e $nginx_pid ];then
echo "nginx already running...."
exit1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL =0]&& touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit1
esac
exit $RETVAL
chkco