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/

 

posted @ 2018-09-25 11:15  糊糊饭团  阅读(387)  评论(0编辑  收藏  举报