当前位置:Gxlcms > PHP教程 > linux上安装nginx

linux上安装nginx

时间:2021-07-01 10:21:17 帮助过:24人阅读

---------安装
先安装依赖
yum -y install openssl openssl-devel
yum -y install pcre-devel
下载nginx
wget http://nginx.org/download/nginx-1.10.0.tar.gz
安装
先解压
tar -zxvf nginx-1.10.0.tar.gz
再编译
cd nginx-1.10.0
./configure --prefix=/usr/local/nginx
make
make install
启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
停止
先查看进程号
ps -ef |grep nginx
root 12901 1 0 11:26 ? 00:00:00 nginx: master process ./nginx
nobody 12902 12901 0 11:26 ? 00:00:00 nginx: worker process
再停止
从容停止:kill -s QUIT 12901
快速停止:kill -s TERM 12901/ kill -s INT 12901
强制停止:killall nginx
重启
验证配置文件是否正确,
方式一:必须进入/usr/local/nginx/sbin/ 执行 ./nginx -t
方式二:/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
开始重启
方法一:必须进入/usr/local/nginx/sbin/ 执行 ./nginx -s reload
方法二:kill -HUP 主进称号
搭建nginx+tomcat环境
在nginx配置文件中添加以下配置(简单的例子)
nginx我安装在目录下:/usr/local/nginx/
user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
charset UTF-8;
sendfile on;
access_log off;
error_log logs/host.error.log crit;
keepalive_timeout 65;
gzip on;
gzip_min_length 1000;
gzip_comp_level 4;
gzip_types text/plain text/css text/xml application/json application/x-javascript;
open_file_cache max=655350 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;

#停机标志,不会被访问到
#server 192.168.239.134:8080 down;
#备份机,所有的非备份机挂掉了才启用
#server 192.168.239.134:8080 backup;
upstream tomcats {
fair;
server 192.168.239.134:8080 backup;
server 192.168.239.135:8080 weight=5;
server 192.168.239.136:8080 weight=2 max_fails=2 fail_timeout=60s;
}
server {
listen 80;
server_name 192.168.239.135;
access_log logs/host.access.log combined;
location ~ \.(jsp|jspx|do)$ {
##root /picclife/data0/www; #tomcat的部署路径,被我改了
index index.jsp index.jspx index.do;
##proxy_pass http://192.168.239.134:8080; #tomcat服务的地址
proxy_pass http://tomcats;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
---------负载均衡策略
1、none 轮询方式(一次一次轮),默认
2、可以通过权重weight来改变轮询(默认为1),权重值越高,被分配的请求越多
3、ip_hash 根据用户的ip地址进行hash运算得到hash值,绑定到某个服务器上,
后续用户的ip不变就会锁定在某个服务器上,此时权重就失效了
4、fair 第三方的,根据自己的算法,得到服务器的负载情况,进行请求分配
5、url_hash 第三方的,根据请求的url进行hash运算,来绑定服务器
安装第三方模块
1、下载gnosek-nginx-upstream-fair-a18b409.tar.gz
2、解压tar -zxvf gnosek-nginx-upstream-fair-a18b409.tar.gz
3、配置,进入nginx的解压目录
./configure --prefix=/usr/local/nginx --add-module=/picclife/soft/nginx-upstream-fair
4、编译,不安装,因为之前我们安装了nginx
make
5、进入objs目录
[root@zhanglw-5 objs]# ls
addon autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
6、替换nginx,之前我们的nginx安装在了/usr/local/nginx目录下了
cp nginx /usr/local/nginx/sbin/

替换前要停止nginx,否则替换不了

以上就介绍了 linux上安装nginx,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行