安裝Nginx環境(linux)
linux環境
- 下載依賴包
安裝nginx需要先將官網下載的源碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc:yum install gcc-c++
PCRE(PerlCompatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。
yum install -y pcre pcre-devel
pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。
yum install -y zlib zlib-devel
zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
yum install -y openssl openssl-devel
OpenSSL是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,并提供豐富的應用程序供測試或其它目的使用。
nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。
- 編譯安裝
第一步:從http://nginx.org/download/上下載相應的版本(或者wget http://nginx.org/download/nginx-1.11.1.tar.gz直接在Linux上用命令下載)
第二步:解壓 tar -zxvf nginx-1.11.1.tar.gz
第三步:設置一下配置信息 ./configure --prefix=/usr/local/nginx ,或者不執行此步,直接默認配置
第四步:
make 編譯 (make的過程是把各種語言寫的源碼文件,變成可執行文件和各種庫文件)
make install 安裝 (make install是把這些編譯出來的可執行文件和庫文件復制到合適的地方)
安裝后在linux下啟動和關閉nginx:
啟動操作
/usr/nginx/sbin/nginx (/usr/nginx/sbin/nginx -t 查看配置信息是否正確)
第五步:配置nginx環境變量
vi /etc/profile
尾行添加
PATH=$PATH:/usr/local/nginx/sbin
export PATH
保存關閉后運行 source /etc/profile 即會加入環境變量
配置Nginx的配置文件
- 進入路徑:/usr/local/nginx/conf
這個是nginx的配置文件夾,在nginx.conf文件中引入需要部署項目的配置文件。示例如下。
- 引入對應的conf文件。下面給出示例代碼。
## Basic reverse proxy server ##
upstream apachephp {
server 172.23.29.137:8206; #Apache
}
server {
listen 9001;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root admin;
index index.html index.htm;
try_files $uri /index.html;
}
location ^~ /front/ {
proxy_pass http://apachephp;
#Proxy Settings
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#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;
#}
其中要注意的是,下圖的server地址是反向代理地址,需要隨著平臺部署地址的改變而改變。
下圖中的admin對應著項目代碼文件夾的名稱,這里因為自服務平臺的代碼放在/usr/local/nginx/admin下面,所以填寫admin。
下圖中的端口號需要每個項目一個,注意區分。這個也和部署之后的地址相對應。
其他代碼均不需要修改。
替換部署包
在項目代碼更改的時候,需要從svn地址下載最新的項目代碼,用dist包里面的代碼,去替換對應的路徑下的代碼。
示例路徑:
/usr/local/nginx/admin ——自服務平臺
/usr/local/nginx/portal ——portal認證頁面
/usr/local/nginx/operation ——運營管理平臺
重啟nginx
命令:nginx -s reload