zoukankan      html  css  js  c++  java
  • centos下nginx安装和配置

    注:此文是根据前辈的博客和自己实际动手总结出来的,不喜勿喷

    1、准备工作

    Nginx的安装依赖于以下三个包,意思就是在安装Nginx之前首先必须安装一下的三个包,注意安装顺序如下:

      1 SSL功能需要openssl库,直接通过yum安装: #yum install openssl

      2 gzip模块需要zlib库,直接通过yum安装: #yum install zlib

      3 rewrite模块需要pcre库,直接通过yum安装: #yum install pcre

    这个是在这篇博文 http://www.cnblogs.com/hanyinglong/p/5102141.html 里面看到的,这三个不知道系统安装的时候是不是直接安装了

    反正我是又重新安装一遍

    2、安装Nginx依赖项和Nginx

      1 使用yum安装nginx需要包括Nginx的库,安装Nginx的库

        #rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

      2 使用下面命令安装nginx

        #yum install nginx

      3 启动Nginx

        #service nginx start

    这个是在这篇博文 http://www.cnblogs.com/Robert-huge/p/6003934.html里面看到的,我直接安装Nginx报错后搜到的,用此博主的方法安装后没有出错

    3、配置nginx开机启动     

    在liunx环境中,安装后nginx目录结构如下:

    主程序: /usr/sbin/nginx 

    存放配置文件:/etc/nginx

    存放静态文件:/usr/share/nginx

    存放日志 : /var/log/nginx

    根目录 : /var/www/html

    如果是新的nginx,在/lib/systemd/system/目录就有nginx.service文件,需要

    systemctl enable nginx.service添加启动命令即可。否则在此目录下新建此文件,写入

    [Unit]
    Description=nginx - high performance web server
    Documentation=http://nginx.org/en/docs/
    After=network-online.target remote-fs.target nss-lookup.target
    Wants=network-online.target

    [Service]
    Type=forking
    PIDFile=/var/run/nginx.pid
    ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s TERM $MAINPID

    [Install]
    WantedBy=multi-user.target

    保存后,添加到系统指令.

    4.安装php-fpm

      apache下也有fcgi了,没配过。

      nginx必须配置php-fpm(fpm = fastcgi process manager)提高php解析性能,降低内存消耗。

      直接用yum安装即可,默认是5.0版本,如果需要7.0就要跟新源。

      yum -y install php-fpm

      安装后在/etc/php-fpm.d/www.conf是配置文件,注意这两个值

    session.save_hander = files,表示session以文件形式保存,如果要共享session,这里可以配置写到redis中。

    session.save_path = xxxx,表示session保存的位置,需要特别注意此目录的写权限,否则在使用session时候回保存不了。

    5>配置php-fpm和虚拟站点

        server {
            listen       8001;
            listen       [::]:8001;
            #server_name  _;
    		
            root         /var/www/html/webone/;
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    		
            location / {
            }
    		
            error_page 404 /404.html;
                location = /40x.html {
            }
    		
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }		
    		
    		location ~ .php$ {
    			root           /var/www/html/webone/;
    			fastcgi_pass   127.0.0.1:9000;
    			fastcgi_index  index.php;
    			#fastcgi_param  SCRIPT_FILENAME  /root/html/$fastcgi_script_name;
    			fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    			include        fastcgi_params;
    		}
        }
    

      

     6>负载均衡配置

    可以先参考: https://blog.51cto.com/13178102/2063271

      但我是在同一个nginx下利用端口来模拟均衡,参考配置如下

    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 2048;
    
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
    	
    	upstream nginx_pools {
    
    		#ip_hash; #按照IP路由
    		#server 192.168.10.1:8668 down; #表示此机不提供服务
    		#server 192.168.10.2:8668 weight=2; #表示此机权重为2,越大权重越高
    		#server 192.168.10.3:8668;
    		#server 192.168.10.4:8668 backup; #表示其他机器在忙或者被标记为down时候,此机提供服务
    		
    		server 47.100.226.xxx:8001;
    		server 47.100.226.xxx:8002;
    		
    	}
    	
        # Load modular configuration files from the /etc/nginx/conf.d directory.
        # See http://nginx.org/en/docs/ngx_core_module.html#include
        # for more information.
        include /etc/nginx/conf.d/*.conf;
    
        server {
            listen       80 default_server;
            listen       [::]:80 default_server;
            location / {
                #access_log logs/access.log main;
                proxy_pass http://nginx_pools;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_redirect default;
                proxy_buffer_size 512k;
                proxy_buffers 6 512k;
                proxy_busy_buffers_size 512k;
                proxy_temp_file_write_size 512k;
                client_max_body_size 100m;
            }
        }
    
    }
    

      这是一个整http内容配置,其中虚拟站点在include /etc/nginx/conf.d/*.conf中配置了8001,8002两个站点,可以参考第5点的虚拟站点配置。

          亲测成功的!

    7》学习资料

    目前个人认为比较好的书是:

    深入剖析Nginx
    深入理解Nginx:模块开发与架构解析
    以下三个网站都很好,可以学习下:
        http://tengine.taobao.org/book/

    http://blog.sina.com.cn/s/articlelist_1929617884_0_1.html
    http://blog.csdn.net/Marcky/

  • 相关阅读:
    iOS企业版打包 发布在线安装包 plist
    iOS企业版打包(转载)
    微信小程序开发入门教程
    SVN常用命令说明
    高仿QQ、微信效果的图片浏览器(支持原图和缩略图、多种手势、CocoaPods)
    iOS 调试大法
    MFMailComposeViewController发送邮件的实例
    自定义UISearchBar外观
    npm 包命令:不是内部或外部命令 问题分析
    angular 子路由跳转出现Navigation triggered outside Angular zone, did you forget to call ‘ngZone.run() 的问题修复
  • 原文地址:https://www.cnblogs.com/huhu1020387597/p/9698522.html
Copyright © 2011-2022 走看看