Linux 系统快速安装 nginx (新手版)


1、安装所需依赖

yum  -y  install pcre pcre-devel gcc openssl openssl-devel zlib zlib-devel

(pcre: 包括 perl 兼容的正则表达式库
  openssl: 支持安全传输协议https(和财务有关系的请求会走的协议)

创建运行用户、组

useradd -M -s /sbin/nologin nginx   


2、上传安装包到local下

 解压  

 tar   -zxvf    nginx-1.20.2.tar.gz

 切换到解压目录下

 cd /usr/local/nginx-1.20.2


 
 执行安装命令
 

./configure
  
  make&&make install

 3、切换到安装目录

 cd  /usr/local/nginx/conf

 找到nginx.conf文件,需修改端口

  vim  nginx.conf

 server {
        listen       80;(默认时80端口需要更改)
        server_name  localhost;


4、启动nginx服务
   
 

 cd   /usr/local/nginx/sbin
   启动命令
   ./nginx


5.查看nginx是否启动成功

ps  -ef|grep   nginx


6、设置防火墙,开放8066端口
查看防火墙是否开放过此端口

firewall-cmd --list-all  
开放端口
firewall-cmd --zone=public --add-port=8066/tcp --permanent
 一定要重启防火墙
firewall-cmd --reload

7、设置开机自动启动

切换到/lib/systemd/system/目录,创建nginx.service文件vi nginx.service

    
# cd /lib/systemd/system/
# vim nginx.service

文件内容如下:

    
[Unit]
Description=nginx 
After=network.target 
  
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true 
  
[Install] 
WantedBy=multi-user.target

退出并保存文件,

   执行systemctl enable nginx.service   使nginx开机启动
    
   

systemctl start nginx.service    启动nginx

systemctl stop nginx.service    结束nginx

systemctl restart nginx.service    重启nginx

 ##############################nginx.conf配置文件############################

user  root;
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;
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;

    server {
    listen       80;
    server_name  localhost;

    location / {
        #这是nginx容器中的默认配置路径,已经映射到虚拟机/www下了 不需要改动
        root   /usr/share/nginx/html/;
        #这里添加index.php入口文件
        index index.php index.html index.htm;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    #配置php
    location ~ \.php$ {
        #这里要换成php容器的ip!
        fastcgi_pass   192.168.0.118:9000;
        fastcgi_index  index.php;
        #这一段一定要注意!把php容器中默认的/var/www/html写进去,替换掉之前的$document
        fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
    }
}

负载均衡三种方式:

##############################轮询负载均衡配置###############################
    upstream lunxun_fuzai {
        server 192.168.30.128:80;
        server 192.168.30.135:80;
    }
  
    server {
        listen 80;
        server_name   192.168.1.111;
  
        location / {
            proxy_pass http:// lunxun_fuzai ;
            proxy_set_header Host $proxy_host;
        }
    }
}


 ##############################weight负载均衡配置###########################
   # upstream order {
   #ip_hash;
   #server 192.168.0.118:8011 weight=1;  #weight权重
   #server 192.168.0.118:8088 weight=1;
   #}

 
   # server{
   # listen 80;
   # server_name 192.168.0.118;
   # location / {
   #    index  index.html  index.htm;
   #     proxy_pass http://order/;
# }
# }
####################################标准配置##############################
#   server {
#      listen   80;
#     server_name  somename  alias  another.alias;

#       location / {
#            root   /usr/share/nginx/html/;
#            index  index.html index.htm;
#       }
#    }
    
}

相关推荐

  1. 快速Linux系统安装MySQL

    2024-03-31 15:38:05       44 阅读
  2. linux系统安装nginx到指定目录

    2024-03-31 15:38:05       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-31 15:38:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-31 15:38:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-31 15:38:05       87 阅读
  4. Python语言-面向对象

    2024-03-31 15:38:05       96 阅读

热门阅读

  1. import关键字的使用

    2024-03-31 15:38:05       38 阅读
  2. Google Earth Engine(GEE)——reduceNeighborhood的使用

    2024-03-31 15:38:05       35 阅读
  3. 抖音直播招聘报白挂载官方招聘小程序

    2024-03-31 15:38:05       39 阅读
  4. sql中如何添加数据

    2024-03-31 15:38:05       33 阅读
  5. C++ 多线程

    2024-03-31 15:38:05       50 阅读
  6. 软考 - 系统架构设计师 - 敏捷开发方法

    2024-03-31 15:38:05       41 阅读
  7. lab-1:Xv6 and Unix utilities

    2024-03-31 15:38:05       39 阅读
  8. PostgreSQL备份还原数据库

    2024-03-31 15:38:05       39 阅读
  9. Unity | 工具类-利用事件系统进行业务串通

    2024-03-31 15:38:05       38 阅读
  10. 修改Element UI的样式,可以通过几种方法来实现

    2024-03-31 15:38:05       38 阅读
  11. 发挥ChatGPT潜力:高效撰写学术论文技巧

    2024-03-31 15:38:05       34 阅读
  12. 使用Spring的集成Quartz框架来管理定时任务

    2024-03-31 15:38:05       39 阅读
  13. -梦想-周游世界-论人生短暂-读书与写作-

    2024-03-31 15:38:05       37 阅读
  14. MySQL与SQLite区别

    2024-03-31 15:38:05       38 阅读