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

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/

centos下nginx安装和配置的更多相关文章

  1. Centos下 Nginx安装与配置

    网上找了好多资料.都很难找全,这里以这个目录为主,进行备注. Nginx是一款轻量级的网页服务器.反向代理服务器.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.它最常的用途是提供 ...

  2. centos下Nginx安装和配置多个域名的虚拟主机

    nginx安装步骤,源码编译安装(源码编译,可以自定制更多功能) openssl #user nobody; worker_processes ; #error_log logs/error.log; ...

  3. centos下nginx安装与配置

    nginx依赖以下模块: l  gzip模块需要 zlib 库 l  rewrite模块需要 pcre 库 l  ssl 功能需要openssl库 tar xzvf nginx-1.9.15.tar. ...

  4. CentOS 下 redis 安装与配置

    CentOS 下 redis 安装与配置   1.到官网上找到合适版本下载解压安装 [root@java src]# wget -c http://redis.googlecode.com/files ...

  5. centos7系统下nginx安装并配置开机自启动操作

    准备工作 我的centos7系统是最小化安装的, 缺很多库, 首先安装必须的运行库 ? 1 2 3 4 5 6 7 8 9 10 11 yum install wget gcc gcc-c++ pcr ...

  6. nginx在CentOs下的安装及配置

    前言: 先介绍一下nginx: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.其特点是占有内存少,并发能力强, ...

  7. windows下nginx安装、配置与使用(转载)

    目前国内各大门户网站已经部署了Nginx,如新浪.网易.腾讯等:国内几个重要的视频分享网站也部署了Nginx,如六房间.酷6等.新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx ...

  8. windows下nginx安装、配置与使用

    目前国内各大门户网站已经部署了Nginx,如新浪.网易.腾讯等:国内几个重要的视频分享网站也部署了Nginx,如六房间.酷6等.新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx ...

  9. CentOS下nginx+php的配置及nginx开机启动配置

    关闭防火墙 (不然外链接是访问不了 apache) service iptables stop 关闭安全系统 SELinux( 不然报403 访问页面错误 ) 1.Nginx安装主要在于配置文件的修改 ...

随机推荐

  1. nmon性能监控工具学习

    nmon在AIX环境上,是一款很出名的系统性能监控工具,其实它除了可以运行在AIX,还可以在Linux环境下编译.使用. 源码下载地址: http://nmon.sourceforge.net/pmw ...

  2. jar工具的使用

  3. IM 通讯录

    wkt-4024 6720

  4. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C

    Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an aff ...

  5. DWR+Spring配置使用

    一.DWR介绍 DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,利用这个框架可以让AJAX开发变得很简单.利用DWR可以在客户端 ...

  6. 转 DG switchover

    I. Pre-Switchover Checks These steps should be completed before the switchover planned maintenance w ...

  7. 转 如何诊断和解决high version count 10.2.0.4 and 11.2.0.4

    转自 http://blog.csdn.net/notbaron/article/details/50927492 在Oracle 10g以上的版本,High version count可谓是一个臭名 ...

  8. 关于JavaDate数据返回到前端变数字的问题(并引申到前后端时间的传输)

    不知道为什么,前端显示的所有数据项都没有错,就只有时间那一项很奇怪,是一串数字,而且这个数字在数据库怎么都找不到…… 然后我在后端从service到controller都debug了一遍,发现数据都没 ...

  9. nodejs 学习(2) 中间件

    var connect=require('connect'), morgan=require('morgan'),//日志 bodyparser=require('body-parser'), ses ...

  10. Railroad UVALive - 4888 记忆化搜索

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...