LINUX部署NGINX

下载部署包

https://nginx.org/en/download.html

解压部署包

cd /data/soft
tar -zxvf nginx-1.27.2.tar.gz

编译

cd /data/soft/nginx-1.27.2
#./configure --prefix=/usr/local/nginx(安装路径) --with-stream(tcp映射组件)
./configure --prefix=/data/soft/nginx --with-stream
编译执行报错 ./configure: error: C compiler cc is not found
#安装编译包

sudo yum groupinstall "Development Tools"

安装

cd /data/soft/nginx-1.27.2
make && make install
安装报错 -bash: make: 未找到命令
#安装make包 
yum install make 
安装报错 make: *** 没有规则可制作目标“build”,由“default” 需求。 停止。
#缺少依赖,需要安装相关依赖包
yum install pcre-devel zlib zlib-devel openssl openssl-devel

安装完后需要重新执行编译和安装命令

启动

#进入安装目录的sbin目录
cd /data/soft/nginx/sbin
./nginx

查看进程

[root@192 sbin]# ps -ef|grep nginx
root      117364       1  0 11:08 ?        00:00:00 nginx: master process ./nginx
nobody    117365  117364  0 11:08 ?        00:00:00 nginx: worker process
root      117369  106422  0 11:08 pts/0    00:00:00 grep --color=auto nginx
[root@192 sbin]# 

停止

#方法一:进入安装目录的sbin目录执行停止命令
cd /data/soft/nginx/sbin
./nginx -s quit
#方法二:直接杀进程
ps -ef|grep nginx |awk '{print $2}'|xargs kill -9 

修改配置后检查

#编辑配置文件(安装目录中conf目录下的nginx.conf)
vi /data/soft/nginx/conf/nginx.conf
#保存配置后,检查
/data/soft/nginx/sbin/nginx -t

[root@192 sbin]# /data/soft/nginx/sbin/nginx -t
nginx: the configuration file /data/soft/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/soft/nginx/conf/nginx.conf test is successful

强制在线加载配置文件

#安装目录中sbin目录下的ngin命令后加 -s reload
/data/soft/nginx/sbin/nginx -s reload

配置文件样例

http映射
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65; #超时时间
     upstream test {
	 ip_hash;  #负载方式  hash为会话保持 默认为轮询
	 server 192.168.1.1:80; #负载节点
	 server 192.168.1.2:80; #负载节点
     }
         server {
            listen       80; #暴露端口
            server_name  localhost;
            location /  {
            proxy_pass http://test/;
            proxy_set_header Host $host:$server_port;
            client_max_body_size    100m;
                        }
            }
    }

tcp映射(与http同级)
stream {
    upstream database {
        server 192.168.1.3:3306 ; #负载节点
    }
    server {
        listen 3306 ;  #暴露端口
        proxy_responses 1;
        proxy_pass database;
    }
}
文件目录映射
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    client_max_body_size 50m;
    server {
        listen       6868;
        server_name  localhost;

        location ~ /(images|videos)/  {
             root /data/soft/files;
            index  index.html index.htm;
                }
        }
    }

posted @ 2025-02-12 11:38  深夜暗月  阅读(116)  评论(0)    收藏  举报