:::info 尽管市面上有很多Linux + Nginx + Mysql + PHP 的一健安装包,但在生产环境下,还是最好自己动手去编译。 :::

安装依赖

  1. yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel

下载 Nginx

  1. cd /usr/local/src/
  2. wget -cO nginx-1.16.0.tar.gz https://nginx.org/download/nginx-1.16.0.tar.gz
  3. tar -zxvf nginx-1.16.0.tar.gz
  4. cd nginx-1.16.0/

创建用户和组

  1. groupadd nginx
  2. useradd nginx -g nginx -s /sbin/nologin -M

配置编译选项

  1. ./configure --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_gunzip_module

编译安装

  1. make && make install

启动 Nginx

  1. /usr/local/nginx/sbin/nginx # 启动
  2. /usr/local/nginx/sbin/nginx -s reload # 重启
  3. /usr/local/nginx/sbin/nginx -s stop # 停止
  4. ps -ef | grep nginx #查询nginx主进程号
  5. /usr/local/nginx/sbin/nginx -s stop #快速关闭 Nginx 先查出nginx进程id再使用kill命令强制杀掉进程
  6. /usr/local/nginx/sbin/nginx -s quit #关闭Nginx 待nginx进程处理任务完毕进行停止

:::danger 注意:启动时,如果 mkdir /var/tmp/nginx/client 报错,请手动创建改目录结构。mkdir /var/tmp/nginx/client -p :::

测试 Nginx 服务器

:::info 直接在浏览器访问服务器的 IP 地址,如果能看到 Welcome to nginx! 页面,说明安装成功,否则检查一下服务器防火墙。
在服务器上可以用 curl -I 127.0.0.1 检查,如果能看到 HTTP 的头部信息,基本可以确定是防火墙的问题。 :::

  1. [root@ecs-4895 nginx-1.16.0]# curl -I localhost
  2. HTTP/1.1 200 OK
  3. Server: nginx/1.16.0
  4. Date: Thu, 30 May 2019 04:44:03 GMT
  5. Content-Type: text/html
  6. Content-Length: 612
  7. Last-Modified: Thu, 30 May 2019 04:17:17 GMT
  8. Connection: keep-alive
  9. ETag: "5cef594d-264"
  10. Accept-Ranges: bytes
  11. [root@ecs-4895 nginx-1.16.0]#

开机启动配置

  1. vim /usr/lib/systemd/system/nginx.service
  1. [Unit]
  2. Description=nginx service
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. ExecStart=/usr/local/nginx/sbin/nginx
  7. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  8. ExecStop=/usr/local/nginx/sbin/nginx -s stop
  9. PrivateTmp=true
  10. [Install]
  11. WantedBy=multi-user.target

:::info [Unit]:服务的说明

Description:描述服务
After:描述服务类别
[Service]:服务运行参数的设置

Type=forking:后台运行的形式
ExecStart:服务的具体运行命令
ExecReload:重启命令
ExecStop:停止命令
PrivateTmp=True:表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径。
[Install]:
运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3。

保存退出。 :::

开机启动

  1. systemctl enable nginx

:::info 如果开机启动不了,很大可能就是nginx: [emerg] open() “/var/run/nginx/nginx.pid” failed (2: No such file or directory),解决方案 :::

配置文件

  1. server {
  2. listen 80;
  3. server_name dati-h5.xinpu8.com;
  4. return 301 https://$server_name$request_uri;
  5. }
  6. server {
  7. listen 443 ssl;
  8. server_name dati-h5.xinpu8.com;
  9. keepalive_timeout 0;
  10. ssl_certificate /etc/nginx/ssl/dati-h5.xinpu8.com/fullchain.cer;
  11. ssl_certificate_key /etc/nginx/ssl/dati-h5.xinpu8.com/dati-h5.xinpu8.com.key;
  12. # root /var/www/datilive/vue-Zhibo/dist;
  13. root /var/www/datilive-h5;
  14. index index.php index.htm index.html;
  15. location / {
  16. try_files $uri $uri/ @router;
  17. index index.php index.htm index.html;
  18. }
  19. location @router {
  20. rewrite ^.*$ /index.html last;
  21. }
  22. }
  23. server {
  24. listen 80;
  25. server_name dati.xinpu8.com;
  26. return 301 https://$server_name$request_uri;
  27. }
  28. server {
  29. listen 443 ssl;
  30. server_name dati.xinpu8.com;
  31. keepalive_timeout 0;
  32. ssl_certificate /etc/nginx/ssl/dati.xinpu8.com/fullchain.cer;
  33. ssl_certificate_key /etc/nginx/ssl/dati.xinpu8.com/dati.xinpu8.com.key;
  34. root /var/www/datilive/public;
  35. index index.php index.htm index.html;
  36. client_max_body_size 200M;
  37. location / {
  38. if ($request_method = 'OPTIONS') {
  39. add_header 'Access-Control-Allow-Origin' '*';
  40. add_header "Access-Control-Allow-Credentials" "true";
  41. add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE';
  42. add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With';
  43. add_header 'Access-Control-Max-Age' 1728000;
  44. add_header 'Content-Type' 'text/plain charset=UTF-8';
  45. add_header 'Content-Length' 0;
  46. return 200;
  47. }
  48. if ($request_method = 'POST') {
  49. add_header 'Access-Control-Allow-Origin' '*';
  50. add_header "Access-Control-Allow-Credentials" "true";
  51. add_header 'Access-Control-Allow-Methods' 'GET, POST,DELET,PUT,OPTIONS';
  52. add_header 'Access-Control-Allow-Headers' 'Authorization,Keep-Alive,User-Agent,X-Requested-With,Content-Type';
  53. }
  54. if ($request_method = 'GET') {
  55. add_header 'Access-Control-Allow-Origin' '*';
  56. add_header "Access-Control-Allow-Credentials" "true";
  57. add_header 'Access-Control-Allow-Methods' 'GET, POST,DELET,PUT,OPTIONS';
  58. add_header 'Access-Control-Allow-Headers' 'Authorization,Keep-Alive,User-Agent,X-Requested-With,Content-Type';
  59. }
  60. if ($request_method = 'DELETE') {
  61. add_header 'Access-Control-Allow-Origin' '*';
  62. add_header "Access-Control-Allow-Credentials" "true";
  63. add_header 'Access-Control-Allow-Methods' 'GET, POST,DELET,PUT,OPTIONS';
  64. add_header 'Access-Control-Allow-Headers' 'Authorization,Keep-Alive,User-Agent,X-Requested-With,Content-Type';
  65. }
  66. if ($request_method = 'put') {
  67. add_header 'Access-Control-Allow-Origin' '*';
  68. add_header "Access-Control-Allow-Credentials" "true";
  69. add_header 'Access-Control-Allow-Methods' 'GET, POST,DELET,PUT,OPTIONS';
  70. add_header 'Access-Control-Allow-Headers' 'Authorization,Keep-Alive,User-Agent,X-Requested-With,Content-Type';
  71. }
  72. if (!-e $request_filename) {
  73. rewrite ^/(.*)$ /index.php?s=$1 last; break;
  74. }
  75. index index.php;
  76. autoindex off;
  77. }
  78. location ~ \.php($|/) {
  79. fastcgi_pass 127.0.0.1:9000;
  80. fastcgi_index index.php;
  81. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  82. include fastcgi_params;
  83. }
  84. # 配置设置图片格式文件
  85. location ~* \.(jpg|jpeg|gif|png|ico|swf)$ {
  86. # 过期时间为3年
  87. expires 3y;
  88. # 关闭日志记录
  89. access_log off;
  90. # 关闭gzip压缩,减少CPU消耗,因为图片的压缩率不高。
  91. gzip off;
  92. }
  93. location ~ /\.ht {
  94. deny all;
  95. }
  96. }