前言:
本文参照:
http://www.cnblogs.com/siqi/p/3572695.html
http://www.cnblogs.com/dennisit/archive/2012/12/26/2834719.html
http://www.nginx.cn/install
https://blog.csdn.net/hfsu0419/article/details/7190152/
https://www.cnblogs.com/jackyzm/p/9600738.html
nginx可以使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息。
正式开始前,编译环境gcc g++ 开发库之类的需要提前装好,这里默认你已经装好。
ububtu平台编译环境可:
apt-get install build-essential
apt-get install libtool
1,安装make:
yum -y install gcc automake autoconf libtool make
2,安装g++:
yum install gcc gcc-c++
3,安装依赖:
我们需要的依赖有:
pcre-8.38.tar.gz
zlib-1.2.8.tar.gz
openssl-fips-2.0.11.tar.gz
下载地址可以参照:
3.1选定源码目录:
cd /usr/local/src
3.2依次安装pcre,zlib,ssl库目录:
一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。
cd /usr/local/src wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz tar -zxvf pcre-8.34.tar.gz cd pcre-8.34 ./configure make make install
cd /usr/local/src wget http://zlib.net/zlib-1.2.8.tar.gz tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure make make install
cd /usr/local/src wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz tar -zxvf openssl-1.0.1c.tar.gz
或者可以参照:
https://blog.csdn.net/hfsu0419/article/details/7190152/
yum -y install pcre-devel openssl openssl-devel
4,安装nginx:
Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细
cd /usr/local/src wget http://nginx.org/download/nginx-1.4.2.tar.gz tar -zxvf nginx-1.4.2.tar.gz cd nginx-1.4.2
./configure --sbin-path=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.38 --with-zlib=/usr/local/src/zlib-1.2.8 --with-openssl=/usr/local/src/openssl-1.0.1c make make install
--with-pcre=/usr/src/pcre-8.34 指的是pcre-8.34 的源码路径。
--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。
安装成功后 /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
5,启动:
[root@localhost nginx]# pwd /usr/local/nginx [root@localhost nginx]# cd sbin [root@localhost sbin]# ls nginx nginx.old [root@localhost sbin]# ./nginx
验证:
浏览器输入:localhost
6,常用的命令:
检测配置文件:
/usr/local/nginx/nginx -t
失败例子:
[root@VM-5bb62e30-ecd0-4df8-b26f-9b9a2e4bd5f9 nginx]# ./nginx -t nginx: [warn] server name "http://www.deeblue.org/" has suspicious symbols in /usr/local/nginx/nginx.conf:38 nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok nginx: [emerg] open() "/usr/local/nginx/logs/misc/access.log" failed (2: No such file or directory) nginx: configuration file /usr/local/nginx/nginx.conf test failed
成功例子:
[root@VM-5bb62e30-ecd0-4df8-b26f-9b9a2e4bd5f9 nginx]# ./nginx -t nginx: [warn] server name "http://www.deeblue.org/" has suspicious symbols in /usr/local/nginx/nginx.conf:38 nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/nginx.conf test is successful
重新加载配置文件(不停止服务):
sudo /usr/local/nginx/nginx -s stop
./nginx
sudo /usr/local/nginx/nginx -s reload
如果需要直接使用nginx 命令的话,需要在/usr/local/bin 下创建一个软连接
ln -sf nginx_home/sbin nginx
7,引用额外配置:
在文件nginx.conf中的server{}下引用一个额外的配置,如下:
#引用额外的配置
include sites-enabled/flight.conf; #server{}块定义了虚拟主机 server {
新建一个sites-enabled文件夹。
放入一个flight.conf文件
upstream flight_server { server localhost:8080; } server { listen 80; server_name localhost; #individual nginx logs for this web vhost access_log /usr/local/nginx/logs/access.log; error_log /usr/local/nginx/logs/error.log ; location = /favicon.ico { return 404; } #when not specify request uri, redirect to /index; #location = / { # rewrite ^ /flihgt/login.jsp ; #} #static files location ~ ^/(assets|templates|easyui-1.3.5|public|ueditor)/(.*)$ { root /usr/local/apache-tomcat-7.0.65/webapps/flihgt; expires 30d; access_log off; } location / { proxy_pass http://flight_server; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; } }
启动tomcat就可以不用8080端口了,直接ip/项目名,就可以访问项目。因为了nginx监听的端口就是80端口。
未完~
浙公网安备 33010602011771号