linux安装Nginx

1、官网

Nginx官网
http://nginx.org

2、简介

简介:Nginx(发音同 engine x)是一款轻量级的 Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个 BSD-like 协议下发行,可以在 UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及 Microsoft Windows 等操作系统中运行。

3、特点

3.1 IO多路复用

采用io多路复用的epoll模型,一种io空闲主动上报机制(内核态fd就绪被放入使用),一个线程并行处理io请求,没有堵塞。不会因为某一路请求的io堵塞导致后面在请求无法执行。

3.2 轻量级

1、功能模块少,代码模块化。
2、与http相关的核心功能集成在源代码里面,其他的非核心插件功能不会在源码中。这样功能不会很全,但是可以加入插件模块进行扩展。代码模块化支持二次开发。

3.3 cpu亲和

1、一般worker数量=cpu*2=cpu的核心数。多worker使用多cpu,把worker固定在一个CPU上运行,利用cpu亲和的绑定方式,减少切换cpu的cache miss,以获得比较好的性能。
2、与http相关的核心功能集成在源代码里面,其他的非核心插件功能不会在源码中。这样功能不会很全,但是可以加入插件模块进行扩展。代码模块化支持二次开发。

4、Nginx常用功能

4.1 Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理。

Nginx在做反向代理时,提供性能稳定,并且能够提供配置灵活的转发功能。Nginx可以根据不同的正则匹配,采取不同的转发策略,比如图片文件结尾的走文件服务器,动态页面走web服务器,只要你正则写的没问题,又有相对应的服务器解决方案,你就可以随心所欲的玩。并且Nginx对返回结果进行错误页跳转,异常判断等。如果被分发的服务器存在异常,他可以将请求重新转发给另外一台服务器,然后自动去除异常服务器。

4.2 负载均衡

Nginx提供的负载均衡策略有2种:内置策略和扩展策略。内置策略为轮询,加权轮询,Ip hash。扩展策略,就是各种自定义设置。

4.3 web缓存

Nginx可以对不同的文件做不同的缓存处理,配置灵活,并且支持FastCGI_Cache,主要用于对FastCGI的动态程序进行缓存。配合着第三方的ngx_cache_purge,对制定的URL缓存内容可以的进行增删管理。

5、Nginx应用场景

  1. 静态服务器。提供静态图片、视频服务等,且可以高并发。
  2. 动态服务
  3. 反向代理,负载均衡
  4. 缓存服务

6、Nginx安装(linux)

6.1 安装编译工具及库文件(即依赖包)

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

6.2 下载Nginx

cd /usr/local  进入local文件夹
wget http://nginx.org/download/nginx-1.18.0.tar.gz -- 下载nginx
tar zxvf nginx-1.18.0.tar.gz  -- 解压下载的nginx压缩包(解压后的文件夹叫做nginx-1.18.0)
mv nginx-1.18.0 nginx -- 重命名nginx-1.18.0为nginx
cd nginx -- 进入nginx文件夹

6.3 编译、安装nginx

-- 在  /usr/local/nginx文件夹内进行两步操作
-- 第一步
./configure
-- 第二步
make && make install

查看Nginx安装版本

/usr/local/nginx/sbin/nginx -v

检查配置文件nginx.conf的正确性

cd /usr/local/nginx/sbin && ./nginx -t

至此,Nginx下载安装完成

6.4 配置Nginx

cd /usr/local/nginx/conf && vim nginx.conf
-- 或者
vi /usr/local/nginx/conf/nginx.conf

我的Nginx配置内容如下,主要修改了http里的server里的location

user  nginx nginx;
worker_processes 2;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  65535;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
   
    upstream test_server {
        server www.xxxxxxxx.com;
        keepalive 2000;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           root   /var/webDemo/website;
           index index.html index.htm;
           try_files $uri $uri/ /index.html;
   		   #proxy_pass http://test_server/server/;
        }

		location /2048 {
            alias   /var/webDemo/game2048;
           index index.html index.htm;
           try_files $uri $uri/ /webDemo/game2048/index.html;
        }
        location /video {
            alias /var/webDemo/videoPlayer;
            index index.html index.htm;
            try_files $uri $uri/ /webDemo/videoPlayer/index.html;
        }
        location /app {
            alias /var/webDemo/hadcApp;
            index index.html index.htm;
            try_files $uri $uri/ /webDemo/hadcApp/index.html;
        }

        #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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

6.5 启动、使用Nginx

1、启动Nginx

/usr/local/nginx/sbin/nginx

如果出现报错:nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed,则运行下面的代码进行再次启动即可

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

2、查看Nginx进程是否启动

ps -ef | grep nginx

在这里插入图片描述

上图显示就是正常启动成功 如果没有启动成功的那就重启下nginx

如果没有运行成功,则可能是端口未放开,看下方的3、4、5点。运行成功的话,直接看6。

3、查看80端口是否被分配给了Nginx 执行

netstat -ntlp

在这里插入图片描述

4、如果有需要,再进行防火墙的开启/关闭

firewall-cmd --zone=public --add-port=80/tcp --permanent

5、重启防火墙

systemctl restart firewalld.service

在这里插入图片描述

6、运行成功的页面

在这里插入图片描述

6.6 Nginx服务的启动、停止

启动、重启、停止

/usr/local/nginx/sbin/nginx -s reload  -- 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reopen  -- 重启 Nginx
/usr/local/nginx/sbin/nginx -s stop  -- 停止 Nginx

注意:当配置好default.conf的时候,需要重启nginx才能运行新改的配置,这里在服务器里运行指令 ‘nginx -s reload’ 会报错‘nginx: [error] open() “/var/run/nginx.pid” failed (2: No such file or directory)’

/usr/local/nginx/sbin/nginx -s reload  -- 重新载入配置文件

6.7 防火墙小知识

1、查看防火墙是否开启,以及开启/关闭防火墙

systemctl status firewalld  -- 查看防火墙是否开启
systemctl start firewalld  -- 防火墙开启
systemctl stop firewalld  -- 防火墙

2、列出全部的port使用情况

firewa1l- cmd  --list-ports

2、防火墙80端口的开启/关闭

-- 开启80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
-- 关闭80端口
firewall-cmd --permanent --remove-port=8080/tcp --permanent

其中,–permanent #永久生效,没有此参数重启后失效

3、重启防火墙

systemctl restart firewalld.service

一、iptables防火墙

1、基本操作

查看防火墙状态

service iptables status

停止防火墙

service iptables stop 

启动防火墙

service iptables start  

重启防火墙

service iptables restart  

永久关闭防火墙

chkconfig iptables off

永久关闭后重启

chkconfig iptables on

2、开启80端口

vim /etc/sysconfig/iptables

加入如下代码

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

保存退出后重启防火墙

service iptables restart

二、firewall防火墙
1、查看firewall服务状态

systemctl status firewalld

出现Active: active (running)切高亮显示则表示是启动状态。
出现 Active: inactive (dead)灰色表示停止,看单词也行。

2、查看firewall的状态

firewall-cmd --state

3、开启、重启、关闭、firewalld.service服务

-- 开启
service firewalld start
-- 重启
service firewalld restart
-- 关闭
service firewalld stop

4、查看防火墙规则

firewall-cmd --list-all

5、查询、开放、关闭端口

-- 查询端口是否开放
firewall-cmd --query-port=8080/tcp
-- 开放80端口
firewall-cmd --permanent --add-port=80/tcp
-- 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
-- 重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、–permanent:表示设置为持久;
3、–add-port:标识添加的端口;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值