本篇文章介绍下Nginx有关内容,Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。
开源:直接获取源代码,高性能:支持海量并发,可靠:服务稳定
Web服务有很多,选择Nginx是因为他的轻量化,功能模块少(源代码仅保留http与核心模块代码,其余不够核心代码的作为插件来安装)代码模块化,对于开发人员友好。
Nginx与Apache区别
1、Nginx采用Epool网络模型,I/O异步非阻塞,效率高,Apache采用Select模型
2、Nginx处理静态文件好,静态处理性能比Apache高三倍以上
静态文件:图片 视频 js css文件 (放到存储服务器NFS)
动态数据:文字 个人信息 家庭住址 (放到数据库)
3、Nginx可以在不间断服务的情况下进行版本升级,社区活跃、各种高性能模块出品迅速,所以我们需要用稳定版本。
4、Apache的rewrite比nginx强大,在rewrite频繁的情况下,用Apache,Apache成熟稳定,bug比较少,Apache对PHP配置比较简单,Nginx需要配合其他后端使用。
Nginx安装方式
1、yum安装
2、编译安装,通过编译源代码安装
yum安装,包含安装包的有两个仓库。
一个是epel仓库 #版本较低,配置不宜读
二是通过官网的仓库安装 #可以选择版本较新,配置易读
Nginx安装流程
第一步:安装官网yum源,去官网(nginx news)
1. [nginx-stable] #仓库名称 2. name=nginx stable repo #名称 3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ #官网连接地址 4. gpgcheck=1 #检查nginx的完整性,通过MD5校验方式 5. enabled=1 #是否开启此仓库,1为开启,0为关闭,适用于临时关闭 6. gpgkey=https://nginx.org/keys/nginx_signing.key #MD5存放链接 7. module_hotfixes=true
第二步:安装
1. [root@NFS ~]# yum -y install nginx 2. Loaded plugins: fastestmirror 3. Loading mirror speeds from cached hostfile 4. * base: mirrors.aliyun.com 5. * extras: mirrors.aliyun.com 6. * updates: mirrors.aliyun.com 7. base | 3.6 kB 00:00 8. epel | 4.7 kB 00:00 9. extras | 2.9 kB 00:00 10. nginx-stable | 2.9 kB 00:00 11. updates | 2.9 kB 00:00 12. nginx-stable/7/x86_64/p | 81 kB 00:01 13. Resolving Dependencies 14. --> Running transaction check 15. ---> Package nginx.x86_64 1:1.22.1-1.el7.ngx will be installed 16. --> Processing Dependency: libpcre2-8.so.0()(64bit) for package: 1:nginx-1.22.1-1.el7.ngx.x86_64 17. --> Running transaction check 18. ---> Package pcre2.x86_64 0:10.23-2.el7 will be installed 19. --> Finished Dependency Resolution 20. 21. Dependencies Resolved 22. 23. ============================================= 24. Package 25. Arch Version Repository Size 26. ============================================= 27. Installing: 28. nginx x86_64 1:1.22.1-1.el7.ngx 29. nginx-stable 797 k #在这里看是在nginx-stable仓库下载的 30. Installing for dependencies: 31. pcre2 x86_64 10.23-2.el7 base 201 k 32. 33. Transaction Summary 34. ============================================= 35. Install 1 Package (+1 Dependent package) 36. 37. Total download size: 998 k 38. Installed size: 3.3 M 39. Downloading packages: 40. warning: /var/cache/yum/x86_64/7/nginx-stable/packages/nginx-1.22.1-1.el7.ngx.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 7bd9bf62: NOKEY 41. Public key for nginx-1.22.1-1.el7.ngx.x86_64.rpm is not installed 42. (1/2): nginx-1.22.1-1.e | 797 kB 00:06 43. (2/2): pcre2-10.23-2.el | 201 kB 00:06 44. --------------------------------------------- 45. Total 155 kB/s | 998 kB 00:06 46. Retrieving key from https://nginx.org/keys/nginx_signing.key 47. Importing GPG key 0x7BD9BF62: 48. Userid : "nginx signing key <signing-key@nginx.com>" 49. Fingerprint: 573b fd6b 3d8f bc64 1079 a6ab abf5 bd82 7bd9 bf62 50. From : https://nginx.org/keys/nginx_signing.key 51. Running transaction check 52. Running transaction test 53. Transaction test succeeded 54. Running transaction 55. Installing : pcre2-10.23-2.el7.x86_6 1/2 56. Installing : 1:nginx-1.22.1-1.el7.ng 2/2 57. ---------------------------------------------------------------------- 58. 59. Thanks for using nginx! 60. 61. Please find the official documentation for nginx here: 62. * https://nginx.org/en/docs/ 63. 64. Please subscribe to nginx-announce mailing list to get 65. the most important news about nginx: 66. * https://nginx.org/en/support.html 67. 68. Commercial subscriptions for nginx are available on: 69. * https://nginx.com/products/ 70. 71. ---------------------------------------------------------------------- 72. Verifying : pcre2-10.23-2.el7.x86_6 1/2 73. Verifying : 1:nginx-1.22.1-1.el7.ng 2/2 74. 75. Installed: 76. nginx.x86_64 1:1.22.1-1.el7.ngx #查看安装版本,也可以通过nginx -v查看 77. 78. Dependency Installed: 79. pcre2.x86_64 0:10.23-2.el7 80. 81. Complete! 82. [root@NFS ~]#
1. [root@NFS ~]# nginx -v #查看版本号,不是-v,-V就是--version 2. nginx version: nginx/1.22.1 3. [root@NFS ~]#
第三步:配置Nginx
第四步:设置开机自启动
1. [root@NFS ~]# systemctl start nginx 2. [root@NFS ~]# systemctl enable nginx 3. Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. 4. [root@NFS ~]#
第五步:检查Nginx是否运行
1、查看nginx状态
1. [root@NFS ~]# systemctl status nginx 2. ● nginx.service - nginx - high performance web server 3. Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) 4. Active: active (running) since Fri 2023-03-31 10:27:29 CST; 1min 40s ago 5. Docs: http://nginx.org/en/docs/ 6. Main PID: 3271 (nginx) 7. CGroup: /system.slice/nginx.service 8. ├─3271 nginx: master process /u... 9. └─3272 nginx: worker process 10. 11. Mar 31 10:27:29 NFS systemd[1]: Starting n... 12. Mar 31 10:27:29 NFS systemd[1]: Can't open... 13. Mar 31 10:27:29 NFS systemd[1]: Started ng... 14. Hint: Some lines were ellipsized, use -l to show in full. 15. [root@NFS ~]# 16.
2、netstat -tunlp #查看端口,不解析列出
1. [root@NFS ~]# netstat -tunlp|grep '80' #查看端口并过滤80 2. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3271/nginx: master 3. [root@NFS ~]# id nginx #安装nginx,自动创建了虚拟用户 4. uid=997(nginx) gid=995(nginx) groups=995(nginx) 5. [root@NFS ~]# 6. 7. #去网站上传东西,用户必须是nginx虚拟用户
通过官网下载的Nginx,访问就是这个状态,如果想用域名访问,可以Windows修改hosts
Nginx启动方式(两种选择一种方式使用,不能混合使用)
1、使用systemctl方式管理Nginx
systemctl start nginx #开启
systemctl stop nginx #停止
systemctl restart nginx #重启
systemctl reload nginx #加载
systemctl status nginx #状态
systemctl enable nginx #开机自启动
systemctl disable nginx #禁止开机自启动
2、使用绝对路径方式管理Nginx
/usr/sbin/nginx #启动Nginx
/usr/sbin/nginx -s stop #停止Nginx
/usr/sbin/nginx -s relload #重新加载配置,不停机维护
/usr/sbin/nginx -s stop && /usr/sbin/nginx #重启Nginx