Nginx安装

🍁博客主页:👉不会压弯的小飞侠
✨欢迎关注:👉点赞👍收藏⭐留言✒
✨系列专栏:👉Linux专栏
🔥欢迎大佬指正,一起学习!一起加油!

在这里插入图片描述


🍁简介

  • Nginx 是一个高性能的HTTP和反向代理服务器
  • 特点:
    • 占有内存少
    • 并发能力强

🍁正向代理

  • Nginx不仅可以做反向代理,实现负载均衡,还能用作正向代理来进行上网等功能。
  • 正向代理服务器时位于客户端和服务器之间,客户端为了从服务器获取数据,必须向代理服务器发送一个请求。并指定目标服务器,代理服务器将目标服务器返回的数据转交给客户端。

🍁反向代理

  • 反向代理:其实客户端对代理是无感知的,因为客户端不需要任何配置就可以访问,只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器IP地址。

🍁负载均衡

  • 在访问量较多的时候,可以通过负载均衡,将多个请求分摊到多台服务器上,相当于把一台服务器需要承担的负载量交给多台服务器处理,进而提高系统的吞吐率;另外如果其中某一台服务器挂掉,其他服务器还可以正常提供服务,以此来提高系统的可伸缩性与可靠性。

🍁动静分离

  • 为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度。降低原来单个服务器的压力。

🍁Nginx在线安装

  • 需要的安装包:

    • pcre-8.37.tar.gz
    • openssl-1.0.1t.tar.gz
    • zlib-1.2.8.tar.gz
    • nginx-1.12.2.tar.gz
  • 在finalshell中上传安装包:
    在这里插入图片描述

    • 安装pcre

      • 第一步 联网下载pcre-8.37.tar.gz
      • 第二步 解压压缩文件
        • 命令 tar -zxvf pcre-8.37.tar.gz
      • 第三步 ./configure完成后,回到pcre目录下执行make,最后执行make install
      • 第四步 pcre-config --version 检查版本
    • 安装openssl 、zlib 、 gcc 依赖

      • yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
    • 安装nginx

      • 使用命令解压:
      • ./configure
      • make && make install
      • 进入目录 /usr/local/nginx/sbin 执行./nginx 启动服务
    • 启动查进程:

[root@192 sbin]# ./nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@192 sbin]# ps -ef | grep nginx
root      53563      1  0 23:51 ?        00:00:00 nginx: master process ./nginx
nobody    53564  53563  0 23:51 ?        00:00:00 nginx: worker process
root      55529   1582  0 23:53 pts/0    00:00:00 grep --color=auto nginx
[root@192 sbin]# 

  • 关进程:
 ./nginx -s stop

🍁nginx常用的命令

  • 启动命令
    在/usr/local/nginx/sbin目录下执行 ./nginx
  • 关闭命令
    在/usr/local/nginx/sbin目录下执行 ./nginx -s stop
  • 重新加载命令
    在/usr/local/nginx/sbin目录下执行 ./nginx -s reload
  • 查看版本
    ./nginx -v
  • 查看进程
    ps -ef | grep nginx

🍁nginx.conf 配置文件

  • nginx 安装目录(/usr/local/nginx/conf/nginx.conf)下,其默认的配置文件都放在这个目录的 conf 目录下,而主配置文件 nginx.conf 也在其中,配置文件进行相应的修改:
  • vim nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

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

1.全局块

  • worker_processes 值越大,可以支持的并发处理量也越多

2.events块

  • 每个 work process 可以同时支持的最大连接数等,上面 work process 支持的最大连接数为 1024.

3.http块

  • http 块也可以包括 http全局块、server 块。
  • 代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这配置。

🍁访问Nginx首页

  • 关闭防火墙,访问nginx
    • 在windows系统中访问linux中nginx,默认不能访问的,因为防火墙问题
    • 关闭防火墙-开放访问的端口号,80端口
    • 查看开放的端口号
    • firewall-cmd --list-all
    • 设置开放的服务或端口号
    • firewall-cmd --add-service=http --permanent
    • firewall-cmd --add-port=80/tcp --permanent
    • 重启防火墙
    • firewall-cmd --reload
  • 重新启动Nginx
    • ./nginx
  • 去浏览器输入ip地址80可以省略(不成功,端口号占用问题)
    • 解决方法有两种,一种是换端口,另一种是查看占用端口的进程,把它关掉。
[root@192 sbin]# netstat -ntlp|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      89740/nginx: worker 
[root@192 sbin]# kill -9 89740

  • 重启nginx服务,浏览器输入ip地址:

在这里插入图片描述

评论 39
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会压弯的小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值