安装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