Linux系统下安装配置nginx

Linux系统下安装配置nginx


1、下载nginx

访问官网 ,获取nginx

nginx: download

下载最新稳定版本

image

2、解压包,并转换到nginx文件夹下

#解压包
tar -zxf nginx-1.28.0.tar.gz

#转换到nginx文件夹下
cd nginx-1.28.0

3、配置nginx

./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-http_slice_module \
--with-compat

#编译和安装
make&&make install

4、运行nginx

 #转换到sbin文件夹下
cd /usr/local/nginx/sbin

#运行nginx
 ./nginx

5、查看结果

curl http://localhost:80

6、设置nginx开机自启动

  1. 创建 Nginx 服务文件

    sudo vi /etc/systemd/system/nginx.service
    

  1. 在编辑器中输入以下内容

    [Unit]
    Description=The NGINX HTTP and reverse proxy server
    After=network.target
     
    [Service]
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s stop
    PrivateTmp=true
     
    [Install]
    WantedBy=multi-user.target
    

  1. 重新加载 systemd 配置,识别新创建的 Nginx 服务

    sudo systemctl daemon-reload
    

  1. 将 Nginx 服务设置为开机自启动

    sudo systemctl enable nginx
    

  1. 服务的启动/停止/刷新配置文件/查看状态

    # 启动nginx服务
    systemctl start nginx.service         
    
    # 停止服务
    systemctl stop nginx.service
    
    # 重新启动服务
    systemctl restart nginx.service        
    
    # 查看所有已启动的服务
    systemctl list-units --type=service     
    
    # 查看服务当前状态
    systemctl status nginx.service          
    
    # 设置开机自启动
    systemctl enable nginx.service          
    
    # 停止开机自启动
    systemctl disable nginx.service         
    
posted @ 2025-04-29 11:04  糜9  阅读(28)  评论(0)    收藏  举报