Blogger Information
Blog 291
fans 0
comment 0
visits 349828
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Linux安装Nginx并配置启动命令
Original
931 people have browsed it

镜像下载、域名解析、时间同步请点击 阿里云开源镜像站

安装前准备工作

因为Nginx依赖于gcc的编译环境,所以,需要安装编译环境来使Nginx能够编译起来

  1. yum install gcc-c++

Nginx的http模块需要使用pcre来解析正则表达式,需要安装pcre

  1. yum install -y pcre pcre-devel

安装依赖的解压包

  1. yum install -y zlib zlib-devel

ssl 功能需要 openssl 库,安装 openssl

  1. yum install -y openssl openssl-devel

下载Nginx

可以自己建立一个包,将nginx下载到这个路径,我设置的路径/opt/crm/nginx

如果需要其他nginx版本的可以参考 nginx仓库

  1. wget http://nginx.org/download/nginx-1.10.2.tar.gz

下载完之后解压

  1. tar zxvf nginx-1.10.2.tar.gz

进入到解压之后的nginx目录

  1. [root@localhost src]# cd nginx-1.10.2
  2. [root@localhost nginx-1.10.2]# ./configure && make && make install

如果要使用ssl

  1. ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

注意:如果配置了ssl,检查配置文件时报错

  1. nginx -t
  2. nginx:[emerg]unknown directive ssl错误
  3. 去到nginx安装的目录
  4. ./configure --with-http_ssl_module
  5. 注意要把新生成的文件复制到对应目录
  6. cp objs/nginx /usr/local/nginx/sbin/nginx
  7. 显示成功就搞定
  8. [root@iZ2ze02hshpth1x0vxo8r6Z sbin]# ./nginx -t
  9. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  10. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  11. [root@iZ2ze02hshpth1x0vxo8r6Z sbin]#

安装完之后查看安装目录

  1. [root@izbp10k7vskcf4soxxbp5gz /]# whereis nginx
  2. nginx: /usr/local/nginx
  3. [root@izbp10k7vskcf4soxxbp5gz /]#

通过查找文件名方式

  1. [root@izbp10k7vskcf4soxxbp5gz /]# find / -name nginx
  2. /opt/crm/nginx
  3. /opt/crm/nginx/nginx-1.10.2/objs/nginx
  4. /usr/local/nginx
  5. /usr/local/nginx/sbin/nginx
  6. [root@izbp10k7vskcf4soxxbp5gz /]#

直接执行

  1. [root@izbp10k7vskcf4soxxbp5gz /]# /usr/local/nginx/sbin/nginx
  2. [root@izbp10k7vskcf4soxxbp5gz /]# ps -ef | grep nginx
  3. root 4666 1 0 09:32 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  4. nobody 4667 4666 0 09:32 ? 00:00:00 nginx: worker process
  5. root 5028 29443 0 09:40 pts/0 00:00:00 grep --color=auto nginx
  6. [root@izbp10k7vskcf4soxxbp5gz /]#

在浏览器输入服务器IP地址

file

增加systemctl命令方式启动

直接启动和关闭nginx的方式

  1. 启动nginx的命令为 /usr/local/nginx/sbin/nginx
  2. 停止nginx的命令为 /usr/local/nginx/sbin/nginx -s stop
  3. 重启nginx的命令为 /usr/local/nginx/sbin/nginx -s reload

配置方式 去到/usr/lib/systemd/system/目录新建一个nginx服务,给予执行权限

  1. vim /usr/lib/systemd/system/nginx.service
  2. chmod +x /usr/lib/systemd/system/nginx.service

打开文件nginx.service新建内容

  1. [Unit]
  2. Description=nginx - high performance web server
  3. After=network.target remote-fs.target nss-lookup.target
  4. [Service]
  5. Type=forking
  6. PIDFile=/usr/local/nginx/logs/nginx.pid
  7. ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
  8. ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  9. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  10. ExecStop=/usr/local/nginx/sbin/nginx -s stop
  11. ExecQuit=/usr/local/nginx/sbin/nginx -s quit
  12. PrivateTmp=true
  13. [Install]
  14. WantedBy=multi-user.target

保存之后重载Ststemctl命令

  1. 在启动服务之前,需要先重载systemctl命令
  2. systemctl daemon-reload

配置完之后

  1. systemctl status nginx
  2. systemctl start nginx
  3. systemctl stop nginx
  4. systemctl restart nginx

附上配置

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 65535;
  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. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. gzip on;
  22. #允许压缩的最小字节数
  23. gzip_min_length 1k;
  24. #4个单位为16k的内存作为压缩结果流缓存
  25. gzip_buffers 4 16k;
  26. #设置识别HTTP协议版本,默认是1.1
  27. gzip_http_version 1.1;
  28. #gzip压缩比,可在1~9中设置,1压缩比最小,速度最快,9压缩比最大,速度最慢,消耗CPU
  29. gzip_comp_level 2;
  30. #压缩的类型
  31. gzip_types text/plain application/x-javascript text/css application/xml;
  32. #让前端的缓存服务器混村经过的gzip压缩的页面
  33. gzip_vary on;
  34. # 配置转发到8700 端口
  35. upstream huida{
  36. server 127.0.0.1:8700;
  37. }
  38. server {
  39. listen 80;
  40. listen 443 ssl; # 配置https,监听433端口
  41. server_name xxx.xxx; # 注意如果申请了域名配置再此,如果配置了证书才能https访问
  42. error_page 405 =200 $request_uri;
  43. ssl_certificate cert/7629385.pem;
  44. ssl_certificate_key cert/7629385.key;
  45. client_max_body_size 50m;
  46. underscores_in_headers on;
  47. proxy_set_header Host $host;
  48. proxy_set_header X-Real-IP $remote_addr;
  49. proxy_set_header X-Real-IP $remote_addr;
  50. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  51. index index.htm index.html index.php;
  52. proxy_connect_timeout 60; #建立tcp协议的连接时间
  53. proxy_send_timeout 60; #发送接口的时间
  54. proxy_read_timeout 60; #读取时间(接口响应时间)
  55. #charset koi8-r;
  56. #access_log logs/host.access.log main;
  57. # 配置转发
  58. location /huida/ {
  59. add_header 'Access-Control-Allow-Origin' '*';
  60. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  61. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
  62. add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
  63. proxy_pass http://huida;
  64. }
  65. location / {
  66. root /home/html/huida/;
  67. index index.html index.htm;
  68. }
  69. #静态文件交给nginx处理 代理前端静态资源
  70. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
  71. {
  72. root /home/html/huida/;
  73. expires 12;
  74. }
  75. #静态文件交给nginx处理
  76. location ~ .*\.(js|css)?$
  77. {
  78. root /home/html/huida/;
  79. expires 15d;
  80. }
  81. #error_page 404 /404.html;
  82. # redirect server error pages to the static page /50x.html
  83. #
  84. error_page 500 502 503 504 /50x.html;
  85. location = /50x.html {
  86. root html;
  87. }
  88. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  89. #
  90. #location ~ \.php$ {
  91. # proxy_pass http://127.0.0.1;
  92. #}
  93. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  94. #
  95. #location ~ \.php$ {
  96. # root html;
  97. # fastcgi_pass 127.0.0.1:9000;
  98. # fastcgi_index index.php;
  99. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  100. # include fastcgi_params;
  101. #}
  102. # deny access to .htaccess files, if Apache's document root
  103. # concurs with nginx's one
  104. #
  105. #location ~ /\.ht {
  106. # deny all;
  107. #}
  108. }
  109. # another virtual host using mix of IP-, name-, and port-based configuration
  110. #
  111. #server {
  112. # listen 8000;
  113. # listen somename:8080;
  114. # server_name somename alias another.alias;
  115. # location / {
  116. # root html;
  117. # index index.html index.htm;
  118. # }
  119. #}
  120. # HTTPS server
  121. #
  122. #server {
  123. # listen 443 ssl;
  124. # server_name localhost;
  125. # ssl_certificate cert.pem;
  126. # ssl_certificate_key cert.key;
  127. # ssl_session_cache shared:SSL:1m;
  128. # ssl_session_timeout 5m;
  129. # ssl_ciphers HIGH:!aNULL:!MD5;
  130. # ssl_prefer_server_ciphers on;
  131. # location / {
  132. # root html;
  133. # index index.html index.htm;
  134. # }
  135. #}
  136. }

原文链接:https://blog.csdn.net/YMZ8848/article/details/123438614

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post