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

慈云数据 2024-03-31 技术支持 66 0

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;

#       }

#    }

    

}

微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon