yum安装nginx

迷南。 2023-01-02 08:28 279阅读 0赞

1、镜像配置(可以略过)

  1. # If the mirrorlist= does not work for you, as a fall back you can try the
  2. # remarked out baseurl= line instead.
  3. #
  4. #
  5. [base]
  6. name=CentOS-$releasever - Base - mirrors.aliyun.com
  7. failovermethod=priority
  8. baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
  9. http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
  10. http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
  11. gpgcheck=1
  12. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  13. #released updates
  14. [updates]
  15. name=CentOS-$releasever - Updates - mirrors.aliyun.com
  16. failovermethod=priority
  17. baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
  18. http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
  19. http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
  20. gpgcheck=1
  21. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  22. #additional packages that may be useful
  23. [extras]
  24. name=CentOS-$releasever - Extras - mirrors.aliyun.com
  25. failovermethod=priority
  26. baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
  27. http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
  28. http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
  29. gpgcheck=1
  30. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  31. #additional packages that extend functionality of existing packages
  32. [centosplus]
  33. name=CentOS-$releasever - Plus - mirrors.aliyun.com
  34. failovermethod=priority
  35. baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
  36. http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
  37. http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
  38. gpgcheck=1
  39. enabled=0
  40. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  41. #contrib - packages by Centos Users
  42. [contrib]
  43. name=CentOS-$releasever - Contrib - mirrors.aliyun.com
  44. failovermethod=priority
  45. baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
  46. http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
  47. http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
  48. gpgcheck=1
  49. enabled=0
  50. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

2、命令

  1. yum install -y nginx

3、安装成功

20210105135407904.png

4、配置文件

路径:

  1. cd /etc/nginx

编辑文件

nginx.conf

  1. user root;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log;
  4. pid /run/nginx.pid;
  5. # Load dynamic modules. See /usr/share/nginx/README.dynamic.
  6. #include /usr/share/nginx/modules/*.conf;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. '$status $body_bytes_sent "$http_referer" '
  15. '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. access_log /var/log/nginx/access.log main;
  18. sendfile on;
  19. #tcp_nopush on;
  20. #keepalive_timeout 0;
  21. keepalive_timeout 65;
  22. gzip on;
  23. include /etc/nginx/conf.d/*.conf;
  24. }

具体的server配置

  1. cd /etc/nginx/conf.d

对应的配置

如 vim xxxx.conf

  1. server {
  2. listen 80 default_server;
  3. listen [::]:80 default_server;
  4. server_name _;
  5. root /usr/share/nginx/html;
  6. # Load configuration files for the default server block.
  7. location /{
  8. proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
  9. proxy_set_header Host $host;
  10. proxy_set_header X-Real-IP $remote_addr;
  11. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  12. proxy_set_header Cookie "$http_cookie";
  13. proxy_hide_header X-Powered-By;
  14. proxy_hide_header X-AspNet-Version;
  15. proxy_hide_header X-AspNetMvc-Version;
  16. proxy_pass http://127.0.0.1:8080;
  17. }
  18. error_page 404 /404.html;
  19. location = /404.html {
  20. }
  21. error_page 500 502 503 504 /50x.html;
  22. location = /50x.html {
  23. }
  24. }

5、启动

启动命令

  1. nginx -c /etc/nginx/nginx.conf

查看进程

  1. ps -ef | grep nginx

202101051425450.png

启动成功

重新加载

  1. nginx -s reload

6、设置开机启动

编辑或者新建文件,如果有就不需要了。

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

加入以下内容:

  1. [Unit]
  2. Description=The nginx HTTP and reverse proxy server
  3. After=network.target remote-fs.target nss-lookup.target
  4. [Service]
  5. Type=forking
  6. PIDFile=/run/nginx.pid
  7. # Nginx will fail to start if /run/nginx.pid already exists but has the wrong
  8. # SELinux context. This might happen when running `nginx -t` from the cmdline.
  9. # https://bugzilla.redhat.com/show_bug.cgi?id=1268621
  10. ExecStartPre=/usr/bin/rm -f /run/nginx.pid
  11. ExecStartPre=/usr/sbin/nginx -t
  12. ExecStart=/usr/sbin/nginx
  13. ExecReload=/bin/kill -s HUP $MAINPID
  14. KillSignal=SIGQUIT
  15. TimeoutStopSec=5
  16. KillMode=process
  17. PrivateTmp=true
  18. [Install]
  19. WantedBy=multi-user.target

设置开机自启动

  1. systemctl enable nginx

关闭开机自动启动

  1. systemctl disable nginx

服务相关命令

启动nginx服务

  1. systemctl start nginx.service

停止服务

  1. systemctl stop nginx.service

重新启动服务

  1. systemctl restart nginx.service

查看所有已启动的服务

  1. systemctl list-units --type=service

查看服务当前状态

  1. systemctl status nginx.service

设置开机自启动

  1. systemctl enable nginx.service

停止开机自启动

  1. systemctl disable nginx.service

参考文档:https://blog.csdn.net/weixin_42657158/article/details/101227582

发表评论

表情:
评论列表 (有 0 条评论,279人围观)

还没有评论,来说两句吧...

相关阅读