Linux安装nginx详细步骤

1.安装依赖包

//一键安装上面四个依赖

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

2.下载并解压安装包

//创建一个文件夹
mkdir /usr/local/nginx
cd /usr/local/nginx
//下载tar包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -xvf nginx-1.18.0.tar.gz

3.安装nginx

//进入目录
cd /usr/local/nginx/nginx-1.18.0
//执行命令 考虑到后续安装ssl证书 添加两个模块
./configure --with-http_stub_status_module --with-http_ssl_module
//执行make命令
make
//执行make install命令
make install

4.启动nginx服务

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

5.配置nginx.conf

# 打开配置文件

vi /usr/local/nginx/conf/nginx.conf

将端口号改成8089(随便挑个端口),因为可能apeache占用80端口,apeache端口尽量不要修改,我们选择修改nginx端口。

将localhost修改为你服务器的公网ip地址。

worker_processes  1;

events {
    worker_connections  1024;
}


#my start 点播视频 服务器的配置 
#播放地址示例: rtmp://localhost/vod/qq.mp4 
rtmp {
    server {
        listen 1935 so_keepalive=5s:2:2;
        chunk_size 4000;

        application show {
            #live on;
            #enable HLS
            #hls on;
            play 10.88.1.25:554/test;
            #hls_fragment 3;
            #hls_playlist_length 20;
        }

        application hls {
            live on;        #直
            #回看功能 视频切片变成ts文件
            hls on;                                 #这个参数把直播服务器改造成实时回放服务器。
            wait_key on;                           #对视频切片进行保护,这样就不会产生马赛克了。
            hls_path /usr/local/nginx/video/hls;       #切片视频文件存放位置。
            hls_fragment 10s;                       #每个视频切片的时长。
            # hls_playlist_length 120;                #总共可以回看的事件,这里设置的是1分钟。
            hls_continuous on;                      #连续模式。
            # hls_cleanup off;                         #对多余的切片进行删除。
            #hls_nested on;                          #嵌套模式。
        }
    }
}


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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  10.88.1.25;
        location ^~ /jeecg-boot {
            proxy_pass              http://127.0.0.1:8080/highland/;
            proxy_set_header        Host 127.0.0.1;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        #解决Router(mode: 'history')模式下,刷新路由地址不能找到页面的问题
        location / {
            root   html;
            index  index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^(.*)$ /index.html?s=$1 last;
                break;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
        
        location /hls {
            alias /usr/local/nginx/video/hls;
            # Disable cache
            #add_header Cache-Control no-cache;
        
            # CORS setup
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length';
        
            # allow CORS preflight requests
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }
        
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
        
        }


    }
}

6.重启nginx

/usr/local/nginx/sbin/nginx -s reload

报错:[emerg] unknown directive "rtmp" in /usr/local/nginx/conf/nginx.conf:10

解决方案见:https://blog.csdn.net/weixin_54514751/article/details/129244800?spm=1001.2014.3001.5502

操作完成后再次执行5.重启nginx

查看nginx进程是否启动:

ps -ef | grep nginx

7.若想使用外部主机访问nginx,需要关闭服务器防火墙或开放nginx服务端口,端口为上一步nginx.conf的配置端口:

centOS6及以前版本使用命令: systemctl stop iptables.service
centOS7关闭防火墙命令: systemctl stop firewalld.service

关闭防火墙会导致服务器有一定风险,所以建议是单独开放服务端口 :

开放80端口:

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

查询端口号80 是否开启:

firewall-cmd --query-port=80/tcp

重启防火墙:

firewall-cmd --reload

8.随后访问该ip:端口 即可看到nginx界面。访问服务器ip查看(备注,由于我监听的仍是80端口,所以ip后面的端口号被省略)

http://192.168.2.246/

安装完成一般常用命令:

进入安装目录中,

命令: cd /usr/local/nginx/sbin
启动,关闭,重启,命令:
./nginx 启动
./nginx -s stop 关闭
./nginx -s reload 重启
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值