当前位置: 首页 > news >正文

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

目录

前言

一、下载安装解压

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

2.下载并安装nginx压缩包

3.解压该压缩包

4.创建目标文件夹

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

6.编译安装

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

 二、运行

1.进入nginx下的sbin目录

2.执行启动

3.查看nginx是否启动

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

 三、卸载

1.查看nginx是否运行

2.进入nginx下的sbin目录

3.停止nginx运行

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

5.删除与Nginx有关的文件

6.再查看

7.卸载Nginx的依赖

 四、操作命令

1.进入nginx下的sbin目录

2.启动

3.关闭 

4.重启 

 五、配置成系统服务

1.创建nginx.service文件

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

3.改权限

4.文件生效  

5.设置开机自启 

 六、系统服务操作Nginx命令

1.启动

2.停止

3.重启

4.重新加载配置文件

5. 查看Nginx状态

6.开机自动


前言

最近斥巨资买了台服务器,现记录下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.最后生成的文件夹具体如下

 

 二、运行

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/nginx/logs/nginx.pid
ExecStartPre=/data/nginx/nginx/sbin/nginx -t -c /data/nginx/nginx/conf/nginx.conf
ExecStart=/data/nginx/nginx/sbin/nginx
ExecReload=/data/nginx/nginx/sbin/nginx -s reload
ExecStop=/data/nginx/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;
    #    }
    #}

}

相关文章:

  • 外包做网站/百度营业执照怎么办理
  • 英语做美食网站/网络广告营销
  • 沧州网站建设哪家好/互联网营销策划案
  • 来自元宇宙的声音:Chord Hero
  • 【GD32F427开发板试用】-03-定时器1 的不算坑的坑和时钟设置
  • 代码随想录No20 |654.最大二叉树 617.合并二叉树 700.二叉搜索树中的搜索 98.验证二叉搜索树
  • FFmpeg的抽帧filter:select的应用与源码分析
  • 基于Androidstudio的打车拼车app
  • [ 攻防演练演示篇 ] 利用谷歌 0day 漏洞上线靶机
  • 【Linux】进度条小程序
  • Elasticsearch:通过例子快速入门
  • FPGA学习笔记-知识点3-Verilog语法2
  • Linux网络编程——协议(C/S模型进行通信)
  • FinalShell的下载安装简单使用
  • 【贪心】洛谷 P1803 凌乱的yyy / 线段覆盖