首页服务器centos7如何自主正确地安装配置Nginx?

centos7如何自主正确地安装配置Nginx?

时间2024-03-21 20:06:03发布访客分类服务器浏览1331
导读:这篇文章给大家分享的是“centos7如何自主正确地安装配置Nginx?”,文中的讲解内容简单清晰,易于理解,而且实用性强吗,对大家认识和了解“centos7如何自主正确地安装配置Nginx?”都有一定的帮助,有需要的朋友可以参考了解看看,...
这篇文章给大家分享的是“centos7如何自主正确地安装配置Nginx?”,文中的讲解内容简单清晰,易于理解,而且实用性强吗,对大家认识和了解“centos7如何自主正确地安装配置Nginx?”有一定的帮助,有需要的朋友可以参考了解看看,那么接下来就跟随小编的思路来往下学习吧

 

一、安装编译工具及库文件

yum -y install make zlib zlib-devel gcc gcc-c++ libtool openssl openssl-devel pcre pcre-devel

(PCRE 作用是让 Nginx 支持 Rewrite 功能)

二、安装Nginx

1、下载Nginx

下载地址:http://nginx.org/,选择稳定版本(例如:nginx-1.12.0.tar.gz)

2、将下载的二进制包移动到/usr/local目录,解压缩文件包

tar zxvf nginx-1.12.0.tar.gz

3、进入安装目录,编译安装

cdnginx-1.12.0

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

make

make install

安装完成后的摘要信息:

Configuration summary

+ using system PCRE library

+ using system OpenSSL library

+ using system zlib library



nginx path prefix: "/usr/local/nginx"

nginx binary file: "/usr/local/nginx/sbin/nginx"

nginx modules path: "/usr/local/nginx/modules"

nginx configuration prefix: "/usr/local/nginx/conf"

nginx configuration file: "/usr/local/nginx/conf/nginx.conf"

nginx pid file: "/usr/local/nginx/logs/nginx.pid"

nginx error log file: "/usr/local/nginx/logs/error.log"

nginx http access log file: "/usr/local/nginx/logs/access.log"

nginx http client request body temporary files: "client_body_temp"

nginx http proxy temporary files: "proxy_temp"

nginx http fastcgi temporary files: "fastcgi_temp"

nginx http uwsgi temporary files: "uwsgi_temp"

nginx http scgi temporary files: "scgi_temp"

默认安装到/usr/local/nginx目录。

4、查看Nginx版本

/usr/local/nginx/sbin/nginx -v

输出结果如下:

nginx version: nginx/1.12.0

到此,nginx安装完成。

5、启动、关闭Nginx

#检查配置文件是否正确

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -V # 可以看到编译选项

#启动Nginx

/usr/local/nginx/sbin/nginx # 默认配置文件 /usr/local/nginx/conf/nginx.conf,-c 指定

# 重新载入配置文件

/usr/local/nginx/sbin/nginx -s reload

#重启Nginx,不会改变启动时指定的配置文件

/usr/local/nginx/sbin/nginx -s reopen

#停止Nginx

/usr/local/nginx/sbin/nginx -s stop

pkill nginx

三、Nginx配置

1、创建Nginx运行使用的用户www

/usr/sbin/groupadd www

/usr/sbin/useradd -g www www

2、配置nginx.conf,将/usr/local/nginx/conf/nginx.conf替换为以下内容

[plain]view plain copy
  1. userwwwwww;
  2. worker_processes8;
  3. error_loglogs/error.log;
  4. #error_loglogs/error.lognotice;
  5. #error_loglogs/error.loginfo;
  6. pidlogs/nginx.pid;
  7. events{
  8. useepoll;
  9. worker_connections1024;
  10. }
  11. http{
  12. includemime.types;
  13. default_typeapplication/octet-stream;
  14. #log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
  15. #'$status$body_bytes_sent"$http_referer"'
  16. #'"$http_user_agent""$http_x_forwarded_for"';
  17. #access_loglogs/access.logmain;
  18. client_max_body_size20M;
  19. client_header_buffer_size32k;
  20. large_client_header_buffers432k;
  21. sendfileon;
  22. #tcp_nopushon;
  23. #keepalive_timeout0;
  24. keepalive_timeout65;
  25. gzipon;
  26. gzip_min_length1k;
  27. gzip_buffers416k;
  28. gzip_http_version1.0;
  29. gzip_comp_level2;
  30. gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
  31. gzip_varyon;
  32. proxy_buffer_size64k;
  33. proxy_buffers3232k;
  34. proxy_busy_buffers_size128k;
  35. server{
  36. listen80;
  37. server_namelocalhost;
  38. #charsetkoi8-r;
  39. #access_loglogs/host.access.logmain;
  40. location/{
  41. roothtml;
  42. indexindex.htmlindex.htm;
  43. }
  44. location~*\.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?${
  45. expiresmax;
  46. log_not_foundoff;
  47. }
  48. location^~/HFC/{
  49. proxy_passhttp://$remote_addr:8090$request_uri;
  50. proxy_set_headerHost$host;
  51. proxy_set_headerX-Real_IP$remote_addr;
  52. proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
  53. }
  54. #error_page404/404.html;
  55. #redirectservererrorpagestothestaticpage/50x.html
  56. #
  57. error_page500502503504/50x.html;
  58. location=/50x.html{
  59. roothtml;
  60. }
  61. #proxythePHPscriptstoApachelisteningon127.0.0.1:80
  62. #
  63. #location~\.php${
  64. #proxy_passhttp://127.0.0.1;
  65. #}
  66. #passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
  67. #
  68. #location~\.php${
  69. #roothtml;
  70. #fastcgi_pass127.0.0.1:9000;
  71. #fastcgi_indexindex.php;
  72. #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
  73. #includefastcgi_params;
  74. #}
  75. #denyaccessto.htaccessfiles,ifApache'sdocumentroot
  76. #concurswithnginx'sone
  77. #
  78. #location~/\.ht{
  79. #denyall;
  80. #}
  81. }
  82. #anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration
  83. #
  84. #server{
  85. #listen8000;
  86. #listensomename:8080;
  87. #server_namesomenamealiasanother.alias;
  88. #location/{
  89. #roothtml;
  90. #indexindex.htmlindex.htm;
  91. #}
  92. #}
  93. #HTTPSserver
  94. #
  95. #server{
  96. #listen443ssl;
  97. #server_namelocalhost;
  98. #ssl_certificatecert.pem;
  99. #ssl_certificate_keycert.key;
  100. #ssl_session_cacheshared:SSL:1m;
  101. #ssl_session_timeout5m;
  102. #ssl_ciphersHIGH:!aNULL:!MD5;
  103. #ssl_prefer_server_cipherson;
  104. #location/{
  105. #roothtml;
  106. #indexindex.htmlindex.htm;
  107. #}
  108. #}
  109. }


四、防火墙配置

CentOS7默认的防火墙为firewall

添加防火墙规则如下:

#firewall-cmd --add-port=80/tcp //http协议基于TCP传输协议,放行80端口


以上就是关于“centos7如何自主正确地安装配置Nginx?”的相关知识,感谢各位的阅读,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注网络,小编每天都会为大家更新不同的知识。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: centos7如何自主正确地安装配置Nginx?
本文地址: https://pptw.com/jishu/649519.html
zabbix监控云服务器结构时报错怎么解决? SSH如何通过公钥连接云服务器?

游客 回复需填写必要信息