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
- userwwwwww;
- worker_processes8;
- error_loglogs/error.log;
- #error_loglogs/error.lognotice;
- #error_loglogs/error.loginfo;
- pidlogs/nginx.pid;
- events{
- useepoll;
- worker_connections1024;
- }
- http{
- includemime.types;
- default_typeapplication/octet-stream;
- #log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
- #'$status$body_bytes_sent"$http_referer"'
- #'"$http_user_agent""$http_x_forwarded_for"';
- #access_loglogs/access.logmain;
- client_max_body_size20M;
- client_header_buffer_size32k;
- large_client_header_buffers432k;
- sendfileon;
- #tcp_nopushon;
- #keepalive_timeout0;
- keepalive_timeout65;
- gzipon;
- gzip_min_length1k;
- gzip_buffers416k;
- gzip_http_version1.0;
- gzip_comp_level2;
- gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
- gzip_varyon;
- proxy_buffer_size64k;
- proxy_buffers3232k;
- proxy_busy_buffers_size128k;
- server{
- listen80;
- server_namelocalhost;
- #charsetkoi8-r;
- #access_loglogs/host.access.logmain;
- location/{
- roothtml;
- indexindex.htmlindex.htm;
- }
- location~*\.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?${
- expiresmax;
- log_not_foundoff;
- }
- location^~/HFC/{
- proxy_passhttp://$remote_addr:8090$request_uri;
- proxy_set_headerHost$host;
- proxy_set_headerX-Real_IP$remote_addr;
- proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
- }
- #error_page404/404.html;
- #redirectservererrorpagestothestaticpage/50x.html
- #
- error_page500502503504/50x.html;
- location=/50x.html{
- roothtml;
- }
- #proxythePHPscriptstoApachelisteningon127.0.0.1:80
- #
- #location~\.php${
- #proxy_passhttp://127.0.0.1;
- #}
- #passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
- #
- #location~\.php${
- #roothtml;
- #fastcgi_pass127.0.0.1:9000;
- #fastcgi_indexindex.php;
- #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
- #includefastcgi_params;
- #}
- #denyaccessto.htaccessfiles,ifApache'sdocumentroot
- #concurswithnginx'sone
- #
- #location~/\.ht{
- #denyall;
- #}
- }
- #anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration
- #
- #server{
- #listen8000;
- #listensomename:8080;
- #server_namesomenamealiasanother.alias;
- #location/{
- #roothtml;
- #indexindex.htmlindex.htm;
- #}
- #}
- #HTTPSserver
- #
- #server{
- #listen443ssl;
- #server_namelocalhost;
- #ssl_certificatecert.pem;
- #ssl_certificate_keycert.key;
- #ssl_session_cacheshared:SSL:1m;
- #ssl_session_timeout5m;
- #ssl_ciphersHIGH:!aNULL:!MD5;
- #ssl_prefer_server_cipherson;
- #location/{
- #roothtml;
- #indexindex.htmlindex.htm;
- #}
- #}
- }
四、防火墙配置
CentOS7默认的防火墙为firewall
添加防火墙规则如下:
#firewall-cmd --add-port=80/tcp //http协议基于TCP传输协议,放行80端口
以上就是关于“centos7如何自主正确地安装配置Nginx?”的相关知识,感谢各位的阅读,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注网络,小编每天都会为大家更新不同的知识。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: centos7如何自主正确地安装配置Nginx?
本文地址: https://pptw.com/jishu/649519.html