您现在的位置是:群英 > 服务器 > 系统运维
centos7如何自主正确地安装配置Nginx?
Admin发表于 2023-07-07 11:25:56319 次浏览
这篇文章给大家分享的是“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、进入安装目录,编译安装

cd nginx-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. user  www www;  
  2. worker_processes  8;  
  3.   
  4. error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     use epoll;  
  13.     worker_connections  1024;  
  14. }  
  15.   
  16.   
  17. http {  
  18.     include       mime.types;  
  19.     default_type  application/octet-stream;  
  20.   
  21.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  22.     #                  '$status $body_bytes_sent "$http_referer" '  
  23.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  24.   
  25.     #access_log  logs/access.log  main;  
  26.      
  27.     client_max_body_size 20M;  
  28.     client_header_buffer_size  32k;  
  29.     large_client_header_buffers 4 32k;      
  30.   
  31.     sendfile        on;  
  32.     #tcp_nopush     on;  
  33.   
  34.     #keepalive_timeout  0;  
  35.     keepalive_timeout  65;  
  36.   
  37.     gzip  on;  
  38.     gzip_min_length 1k;  
  39.     gzip_buffers 4 16k;  
  40.     gzip_http_version 1.0;  
  41.     gzip_comp_level 2;  
  42.     gzip_types text/plain application/x-javascript text/css application/xml;  
  43.     gzip_vary on;  
  44.   
  45.     proxy_buffer_size 64k;  
  46.     proxy_buffers   32 32k;  
  47.     proxy_busy_buffers_size 128k;  
  48.   
  49.     server {  
  50.         listen       80;  
  51.         server_name  localhost;  
  52.   
  53.         #charset koi8-r;  
  54.   
  55.         #access_log  logs/host.access.log  main;  
  56.   
  57.         location / {  
  58.             root   html;  
  59.             index  index.html index.htm;  
  60.         }  
  61.           
  62.         location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {  
  63.             expires max;  
  64.             log_not_found off;  
  65.         }   
  66.           
  67.         location ^~ /HFC/ {  
  68.             proxy_pass   http://$remote_addr:8090$request_uri;    
  69.             proxy_set_header  Host  $host;  
  70.             proxy_set_header  X-Real_IP  $remote_addr;  
  71.             proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;  
  72.         }  
  73.   
  74.         #error_page  404              /404.html;  
  75.   
  76.         # redirect server error pages to the static page /50x.html  
  77.         #  
  78.         error_page   500 502 503 504  /50x.html;  
  79.         location = /50x.html {  
  80.             root   html;  
  81.         }  
  82.   
  83.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  84.         #  
  85.         #location ~ \.php$ {  
  86.         #    proxy_pass   http://127.0.0.1;  
  87.         #}  
  88.   
  89.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  90.         #  
  91.         #location ~ \.php$ {  
  92.         #    root           html;  
  93.         #    fastcgi_pass   127.0.0.1:9000;  
  94.         #    fastcgi_index  index.php;  
  95.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  96.         #    include        fastcgi_params;  
  97.         #}  
  98.   
  99.         # deny access to .htaccess files, if Apache's document root  
  100.         # concurs with nginx's one  
  101.         #  
  102.         #location ~ /\.ht {  
  103.         #    deny  all;  
  104.         #}  
  105.     }  
  106.   
  107.   
  108.     # another virtual host using mix of IP-, name-, and port-based configuration  
  109.     #  
  110.     #server {  
  111.     #    listen       8000;  
  112.     #    listen       somename:8080;  
  113.     #    server_name  somename  alias  another.alias;  
  114.   
  115.     #    location / {  
  116.     #        root   html;  
  117.     #        index  index.html index.htm;  
  118.     #    }  
  119.     #}  
  120.   
  121.   
  122.     # HTTPS server  
  123.     #  
  124.     #server {  
  125.     #    listen       443 ssl;  
  126.     #    server_name  localhost;  
  127.   
  128.     #    ssl_certificate      cert.pem;  
  129.     #    ssl_certificate_key  cert.key;  
  130.   
  131.     #    ssl_session_cache    shared:SSL:1m;  
  132.     #    ssl_session_timeout  5m;  
  133.   
  134.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  135.     #    ssl_prefer_server_ciphers  on;  
  136.   
  137.     #    location / {  
  138.     #        root   html;  
  139.     #        index  index.html index.htm;  
  140.     #    }  
  141.     #}  
  142.   
  143. }  


四、防火墙配置

CentOS7默认的防火墙为firewall

添加防火墙规则如下:

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


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

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-01-26 09:19:21 
摘要:云计算与网格计算是什么意思?关于云计算和网格计算,有很多朋友都不是很了解,对此这篇就给大家来详细的解决,以及带大家了解一下云计算与网格计算的区别在哪?感兴趣的朋友就继续往下看吧。
2022-11-19 17:48:40 
摘要:linux安装zip的命令有:1、“yum install zip”命令,可安装zip压缩程序,压缩后的文件后缀名为“.zip”;2、“yum install unzip”命令,可安装unzip解压缩程序,可为“.zip”压缩文件进行解压。
2023-05-26 11:56:34 
摘要:云计算之路:数据库服务器的选择——舍RDS取云服务器
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部