參考:http://www.nginx.cn/install
確認安裝make工具:gcc automake autoconf libtool make
確認安裝g++工具:gcc gcc-c++
下面正式開始
---------------------------------------------------------------------------
一般我們都需要先裝pcre, zlib,前者為了重寫rewrite,後者為了gzip壓縮。
1.選定源碼目錄
可以是任何目錄,本文選定的是/usr/local
cd /usr/local
2.安裝PCRE庫
cd /usr/local
tar -zxvf pcre-8.37.tar.gz
mv pcre-8.37 pcre
cd pcre
./configure
make
make install
3.安裝zlib庫
cd /usr/local
tar -xf zlib-1.2.8.tar.gz
mv zlib-1.2.8 zlib
cd zlib
./configure
make
make install
4.安裝ssl(某些vps預設沒裝ssl)
cd /usr/local
tar -xf openssl-1.0.2d.tar.gz
mv openssl-1.0.2d openssl
5.安裝nginx
Nginx 一般有兩個版本,分別是穩定版和開發版,您可以根據您的目的來選擇這兩個版本的其中一個,下面是把 Nginx 安裝到 /usr/local/nginx 目錄下的詳細步驟:
cd /usr/local
tar -xf nginx-1.9.3.tar.gz
cd nginx-1.9.3
mv nginx-1.9.3 nginx
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/pcre \
--with-zlib=/usr/local/zlib \
--with-openssl=/usr/local/openssl
make
make install
--with-pcre=/usr/local/pcre 指的是pcre 的源碼路徑。
--with-zlib=/usr/local/zlib 指的是zlib 的源碼路徑。
安裝成功後 /usr/local/nginx 目錄下如下
fastcgi.conf koi-win nginx.conf.default
fastcgi.conf.default logs scgi_params
fastcgi_params mime.types scgi_params.default
fastcgi_params.default mime.types.default uwsgi_params
html nginx uwsgi_params.default
koi-utf nginx.conf win-utf
6. 修改nginx目錄下的nginx.conf檔案,這裡採用的是88連接埠
#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;
events {
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;
upstream localhost {
server 192.168.5.151:8080;
}
server {
listen 88;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
7.啟動
確保系統的 88 連接埠沒被其他程式佔用,運行./usr/local/nginx/nginx 命令來啟動 Nginx,
netstat -ano|grep 88
如果查不到結果後執行,有結果則忽略此步驟(ubuntu下必須用sudo啟動,不然只能在前台運行)
sudo /usr/local/nginx/nginx
開啟瀏覽器訪問此機器的 IP,如果瀏覽器出現 Welcome to nginx! 則表示 Nginx 已經安裝並運行成功。