Nginx Linux详细安装部署教程

1.下载安装

1.官网下载

官网:http://nginx.org/en/download.html
在这里插入图片描述

2.将下载好的 nginx-1.16.1.tar.gz 上传到linux或ubuntu指定的文件下

3.nginx安装

	tar -zxvf nginx-1.16.1.tar.gz	// 解压
	cd nginx-1.16.1	// 切换目录
	./configure --prefix=/usr/local/nginx	//  配置,将nginx安装到/usr/local/ngnix目录
	make	// 编译
	make install	// 安装

4.nginx常用命令

查看nginx安装位置
whereis nginx
配置文件
vim /usr/local/nginx/conf/nginx.conf

查看是否有nginx进程启动
ps -ef | grep nginx
在这里插入图片描述

启动服务
/usr/local/nginx/sbin/nginx
停止/重启
/usr/local/nginx/sbin/nginx -s stop(quit、reload)
重新加载配置
/usr/local/nginx/sbin/nginx -s reload

启动、停止、重新加载、重启、查看状态服务
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl enable nginx
systemctl disable nginx

测试配置文件是否正确
/usr/local/nginx/sbin/nginx -t
在这里插入图片描述

5.访问nginx

开启nginx后,在本地访问服务器ip也就是服务器的localhost,若不能访问,可有防火墙没有关闭。
nginx配置成功后,外网无法访问的问题

设置防火墙:

ubuntu:
ufw allow 'Nginx Full'
ufw status

centos
方法一直接关闭防火墙
关闭防火墙
[root@localhost ~]# service iptables stop
关闭开机自启动防火墙
[root@localhost ~]# chkconfig iptables off
[root@localhost ~]# chkconfig --list|grep ipt

方法二将开启的端口加入防火墙白名单中
编辑防火墙白名单
[root@localhost ~]# vim /etc/sysconfig/iptables
增加下面一行代码
-A INPUT -p tcp -m state -- state NEW -m tcp --dport 80 -j ACCEPT
保存退出,重启防火墙
[root@localhost ~]# service iptables restart

二.Nginx负载均衡配置

编辑配置文件:vim conf/nginx.conf

每一个server就是一个虚拟主机,我们可以当作web服务器来使用

 server {
        listen       8001;	// 代表监听80端口
        server_name  localhost;	// 代表外网访问的域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

		// 代表一个过滤器,/匹配所有请求,我们还可以根据自己的情况定义不同的过滤,
		// 比如对静态文件js、css、image制定专属过滤
        location / {
            root   html;	// 代表站点根目录
            index  index.html index.htm;	// 代表默认主页
        }
}        

负载均衡功能往往在接收到某个请求后分配到后端的多台服务器上,那我们就需要upstream{}块来配合使用,Nginx作为负载均衡策略有轮询、ip hash、权重等方式

upstream xxx{};upstream模块是命名一个后端服务器组,组名必须为后端服务器站点域名,内部可以写多台服务器ip和port,还可以设置跳转规则及权重等等
ip_hash;代表使用ip地址方式分配跳转后端服务器,同一ip请求每次都会访问同一台后端服务器
server;代表后端服务器地址

server{};server模块依然是接收外部请求的部分
server_name;代表外网访问域名
location / {};同样代表过滤器,用于制定不同请求的不同操作
proxy_pass;代表后端服务器组名,此组名必须为后端服务器站点域名

server_name和upstream{}的组名可以不一致,server_name是外网访问接收请求的域名,upstream{}的组名是跳转后端服务器时站点访问的域名
在这里插入图片描述
下面给出一个配置示例,修改conf/nginx.conf文件,默认是轮询策略:

worker_processes  1;
 
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    sendfile        on;
 
    keepalive_timeout  65;
 
    upstream web{
        #ip_hash;
        server 192.168.43.31:8080;
    }
 
   server {
        listen 80;
        server_name  wzj.test.com;
 
        location / {
 
            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_pass  http://web;
        }
   }
}

三.安装nginx遇到的一些问题

1.问题:我是在make nginx的时候出的问题只有一行提示:
make[1]: Leaving directory `/usr/local/nginx-1.12.1;我个人认为这句话的意思是,安装后,离开了这个文件;

解决:如果你走到make这一步的时候只出现了一行“make[1]: Leaving directory `/usr/local/nginx-1.12.1;”提示,不用管它,继续走make install
然后:
1、我只需要去看/usr/local下面是否有nginx文件夹?
2、如果已经有nginx文件夹?
3、我们再去看nginx是否可以正常启动?
4、如果nginx启动也可以成功,我们再去看下网站是否可以访问,是否会出现“Welcome to nginx!”?
5、如果上面都成功了,说明你的nginx已经ok!


2.问题:在Ubuntu18.04上安装Ngnix,在编译步骤出现如下错误:
cc1: all warnings being treated as errors
objs/Makefile:460: recipe for target 'objs/src/core/ngx_murmurhash.o' failed
make[1]: [objs/src/core/ngx_murmurhash.o] Error 1
make[1]: Leaving directory '/home/wzj/tools/nginx/nginx-1.11.3'
Makefile:8: recipe for target 'build' failed
make: [build] Error 2

解决找到对应的Maakefile文件,将gcc参数中的-Werror去掉


3.问题:[root@localhost nginx]# systemctl status nginx.service
● nginx.service - LSB: starts the nginx web server
Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2019-07-22 16:41:54 CST; 43s ago
Docs: man:systemd-sysv-generator(8)
Process: 28076 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=1/FAILURE)

Jul 22 16:41:54 localhost.localdomain systemd[1]: Starting LSB: starts the nginx web server...
Jul 22 16:41:54 localhost.localdomain nginx[28076]: Starting nginx... nginx (pid 26717 26716 26715 26042) already running.
Jul 22 16:41:54 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
Jul 22 16:41:54 localhost.localdomain systemd[1]: Failed to start LSB: starts the nginx web server.
Jul 22 16:41:54 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
Jul 22 16:41:54 localhost.localdomain systemd[1]: nginx.service failed.

解决:[root@localhost nginx]# ps -e | grep nginx
26042 ? 00:00:00 nginx
26715 ? 00:00:00 nginx
26716 ? 00:00:00 nginx
26717 ? 00:00:00 nginx
[root@localhost nginx]# kill -9 26042
[root@localhost nginx]# kill -9 26715
[root@localhost nginx]# kill -9 26716
[root@localhost nginx]# kill -9 26717
[root@localhost nginx]# ps -e | grep nginx

参考链接:
Nginx Linux详细安装部署教程
Ubuntu18.04安装Nginx
前端想要了解的Nginx
一文弄懂Nginx的location匹配

posted @ 2019-12-05 18:42  zlgSmile  阅读(936)  评论(0编辑  收藏  举报