安裝Nginx環(huán)境(linux)
linux環(huán)境
- 下載依賴包
安裝nginx需要先將官網(wǎng)下載的源碼進(jìn)行編譯,編譯依賴gcc環(huán)境,如果沒有g(shù)cc環(huán)境胧沫,需要安裝gcc:yum install gcc-c++
PCRE(PerlCompatible Regular Expressions)是一個Perl庫啊终,包括 perl 兼容的正則表達(dá)式庫辈灼。nginx的http模塊使用pcre來解析正則表達(dá)式,所以需要在linux上安裝pcre庫蒲牧。
yum install -y pcre pcre-devel
pcre-devel是使用pcre開發(fā)的一個二次開發(fā)庫撇贺。nginx也需要此庫。
yum install -y zlib zlib-devel
zlib庫提供了很多種壓縮和解壓縮的方式造成,nginx使用zlib對http包的內(nèi)容進(jìn)行g(shù)zip显熏,所以需要在linux上安裝zlib庫雄嚣。
yum install -y openssl openssl-devel
OpenSSL是一個強大的安全套接字層密碼庫晒屎,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協(xié)議缓升,并提供豐富的應(yīng)用程序供測試或其它目的使用鼓鲁。
nginx不僅支持http協(xié)議,還支持https(即在ssl協(xié)議上傳輸http)港谊,所以需要在linux安裝openssl庫骇吭。
- 編譯安裝
第一步:從http://nginx.org/download/上下載相應(yīng)的版本(或者wget http://nginx.org/download/nginx-1.11.1.tar.gz直接在Linux上用命令下載)
第二步:解壓 tar -zxvf nginx-1.11.1.tar.gz
第三步:設(shè)置一下配置信息 ./configure --prefix=/usr/local/nginx ,或者不執(zhí)行此步歧寺,直接默認(rèn)配置
第四步:
make 編譯 (make的過程是把各種語言寫的源碼文件燥狰,變成可執(zhí)行文件和各種庫文件)
make install 安裝 (make install是把這些編譯出來的可執(zhí)行文件和庫文件復(fù)制到合適的地方)
安裝后在linux下啟動和關(guān)閉nginx:
啟動操作
/usr/nginx/sbin/nginx (/usr/nginx/sbin/nginx -t 查看配置信息是否正確)
第五步:配置nginx環(huán)境變量
vi /etc/profile
尾行添加
PATH=$PATH:/usr/local/nginx/sbin
export PATH
保存關(guān)閉后運行 source /etc/profile 即會加入環(huán)境變量
配置Nginx的配置文件
- 進(jìn)入路徑:/usr/local/nginx/conf
這個是nginx的配置文件夾,在nginx.conf文件中引入需要部署項目的配置文件斜筐。示例如下龙致。
- 引入對應(yīng)的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對應(yīng)著項目代碼文件夾的名稱榛了,這里因為自服務(wù)平臺的代碼放在/usr/local/nginx/admin下面,所以填寫admin煞抬。
下圖中的端口號需要每個項目一個霜大,注意區(qū)分。這個也和部署之后的地址相對應(yīng)革答。
其他代碼均不需要修改战坤。
替換部署包
在項目代碼更改的時候,需要從svn地址下載最新的項目代碼蝗碎,用dist包里面的代碼湖笨,去替換對應(yīng)的路徑下的代碼。
示例路徑:
/usr/local/nginx/admin ——自服務(wù)平臺
/usr/local/nginx/portal ——portal認(rèn)證頁面
/usr/local/nginx/operation ——運營管理平臺
重啟nginx
命令:nginx -s reload