Centos7安装、卸载nginx及配置,配置成系统服务方式(一步到位)

 更新时间:2023年12月25日 15:36:54   作者:luvJie-7c  
这篇文章主要介绍了Centos7安装、卸载nginx及配置,配置成系统服务方式(一步到位),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

前言

最近斥巨资买了台服务器,现记录下nginx安装配置过程。

一、下载安装解压

1.进入临时文件夹里(随便一个都行)

cd /tmp/

2.下载并安装nginx压缩包

wget http://nginx.org/download/nginx-1.23.3.tar.gz

3.解压该压缩包

tar -xvf nginx-1.23.3.tar.gz

4.创建目标文件夹

cd /tmp/nginx-1.23.3

5.(默认会安装在/usr/local/nginx)这里通过configure命令指定安装目录

./configure --prefix=/data/nginx

6.编译安装

make && make install

7.最后生成的文件夹具体如下

二、SSL模块安装(SSL证书)用于htpps请求  没此需求可略过

./configure --prefix=/data/nginx --with-http_ssl_module

三、运行

1.进入nginx下的sbin目录

cd /data/nginx/sbin

2.执行启动

./nginx

3.查看nginx是否启动

ps -ef | grep nginx

 

4.浏览器访问你的IP(如下就是成功了)

四、卸载

1.查看nginx是否运行

ps aux | grep nginx

2.进入nginx下的sbin目录

cd /data/nginx/sbin

3.停止nginx运行

./nginx -s stop

4.查看与Nginx有关的文件夹

find / -name nginx

5.删除与Nginx有关的文件

rm -rf file /data/nginx*

6.再查看

find / -name nginx*

7.卸载Nginx的依赖

yum remove nginx

五、Nginx的基本操作命令

1.进入nginx下的sbin目录

cd /data/nginx/sbin

2.启动

./nginx

3.关闭 

./nginx -s stop

4.重启 

./nginx -s reload

六、配置成系统服务

1.创建nginx.service文件

vim /usr/lib/systemd/system/nginx.service 

2.nginx.service文件中写入内容

[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target
 
[Service]
Type=forking
PIDFile=/data/nginx/logs/nginx.pid
ExecStartPre=/data/nginx/sbin/nginx -t -c /data/nginx/conf/nginx.conf
ExecStart=/data/nginx/sbin/nginx
ExecReload=/data/nginx/sbin/nginx -s reload
ExecStop=/data/nginx/sbin/nginx -s stop
PrivateTmp=true
 
[Install]
WantedBy=default.target

3.改权限

chmod 755 /usr/lib/systemd/system/nginx.service

4.文件生效  

systemctl daemon-reload

5.设置开机自启 

systemctl enable nginx.service 

七、系统服务操作Nginx基本命令

1.启动

systemctl start nginx

2.停止

systemctl stop nginx

3.重启

systemctl restart nginx

4.重新加载配置文件

systemctl reload nginx

5. 查看Nginx状态

systemctl status nginx

6.开机自动

systemctl enable nginx

八、nginx.conf文件基本配置详解

 
#user  nobody;
#进程的数量
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服务器,利用它的反向代理功能提供负载均衡支持
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
 
    #access_log  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
 
    #配置虚拟机
    server {
        listen       8585;#监听端口
        server_name  localhost;#主机ip
        #请求转发
        location / {
            proxy_pass http://localhost:8001;
        }
        
        location /app{
			try_files $uri $uri/ /app/index.html;
		}
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
 
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
 
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
 
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
 
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
 
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • nginx反向代理下的长连接实现

    nginx反向代理下的长连接实现

    本文主要介绍了Nginx反向代理下的长连接实现,包括客户端到Nginx和Nginx到后端服务器之间的长连接设置,具有一定的参考价值,感兴趣的可以了解一下
    2024-11-11
  • 在Nginx服务器中配置针对TCP的负载均衡的方法

    在Nginx服务器中配置针对TCP的负载均衡的方法

    这篇文章主要介绍了在Nginx服务器中配置针对TCP的负载均衡的方法,另外还介绍了TCP负载均衡的执行原理,需要的朋友可以参考下
    2015-12-12
  • nginx使用sticky基于cookie的会话保持方式

    nginx使用sticky基于cookie的会话保持方式

    这篇文章主要介绍了nginx使用sticky基于cookie的会话保持方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Nginx启动显示80端口占用问题的解决方案

    Nginx启动显示80端口占用问题的解决方案

    这篇文章主要介绍了Nginx启动显示80端口占用问题的解决方案,文中通过代码示例和图文讲解的非常详细,对大家解决问题有一定的帮助,需要的朋友可以参考下
    2024-04-04
  • nginx中的proxy_set_header参数指令详解

    nginx中的proxy_set_header参数指令详解

    本文介绍了Nginx中的proxy_set_header指令,用于自定义代理请求的HTTP头部信息,实现更灵活的反向代理功能,提供了实际应用场景和配置示例,帮助读者更好地理解和使用proxy_set_header指令,感兴趣的朋友一起看看吧
    2025-03-03
  • nginx访问返回504问题及解决

    nginx访问返回504问题及解决

    这篇文章主要介绍了nginx访问返回504问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • Nginx可视化管理工具结合cpolar实现远程访问的步骤详解

    Nginx可视化管理工具结合cpolar实现远程访问的步骤详解

    Nginx Proxy Manager 是一个开源的反向代理工具,本文将给大家介绍在Linux 安装Nginx Proxy Manager并且结合 cpolar内网穿透工具实现远程访问管理界面,同等,当我们使用Nginx Proxy Manager配置其他本地服务,并且需要远程访问,也是同样的方式,需要的朋友可以参考下
    2023-09-09
  • Nginx缓存设置案例详解

    Nginx缓存设置案例详解

    这篇文章主要介绍了Nginx缓存设置案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • Nginx中使用gzip_http_version解决CDN只支持http 1.0问题

    Nginx中使用gzip_http_version解决CDN只支持http 1.0问题

    这篇文章主要介绍了Nginx中使用gzip_http_version解决CDN只支持http 1.0问题,问题原因是在Header信息中看到Transfer-Encoding: chunked,使用本文方法就可以解决这个问题,需要的朋友可以参考下
    2014-09-09
  • Nginx如何安装withSSL模块

    Nginx如何安装withSSL模块

    这篇文章主要介绍了Nginx如何安装withSSL模块,Nginx 配置文件,开启ssl访问时经常报错,原因是由于nginx缺少http_ssl_module模块,编译安装的时候带上 --with-http_ssl_module 配置就行了,感兴趣的朋友跟随小编一起看看吧
    2024-04-04

最新评论