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/
centos下nginx安装和配置的更多相关文章
- Centos下 Nginx安装与配置
网上找了好多资料.都很难找全,这里以这个目录为主,进行备注. Nginx是一款轻量级的网页服务器.反向代理服务器.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.它最常的用途是提供 ...
- centos下Nginx安装和配置多个域名的虚拟主机
nginx安装步骤,源码编译安装(源码编译,可以自定制更多功能) openssl #user nobody; worker_processes ; #error_log logs/error.log; ...
- centos下nginx安装与配置
nginx依赖以下模块: l gzip模块需要 zlib 库 l rewrite模块需要 pcre 库 l ssl 功能需要openssl库 tar xzvf nginx-1.9.15.tar. ...
- CentOS 下 redis 安装与配置
CentOS 下 redis 安装与配置 1.到官网上找到合适版本下载解压安装 [root@java src]# wget -c http://redis.googlecode.com/files ...
- centos7系统下nginx安装并配置开机自启动操作
准备工作 我的centos7系统是最小化安装的, 缺很多库, 首先安装必须的运行库 ? 1 2 3 4 5 6 7 8 9 10 11 yum install wget gcc gcc-c++ pcr ...
- nginx在CentOs下的安装及配置
前言: 先介绍一下nginx: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.其特点是占有内存少,并发能力强, ...
- windows下nginx安装、配置与使用(转载)
目前国内各大门户网站已经部署了Nginx,如新浪.网易.腾讯等:国内几个重要的视频分享网站也部署了Nginx,如六房间.酷6等.新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx ...
- windows下nginx安装、配置与使用
目前国内各大门户网站已经部署了Nginx,如新浪.网易.腾讯等:国内几个重要的视频分享网站也部署了Nginx,如六房间.酷6等.新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx ...
- CentOS下nginx+php的配置及nginx开机启动配置
关闭防火墙 (不然外链接是访问不了 apache) service iptables stop 关闭安全系统 SELinux( 不然报403 访问页面错误 ) 1.Nginx安装主要在于配置文件的修改 ...
随机推荐
- C#字符串判断
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- Mybatis分页中遇到的坑2
站在巨人的肩膀上 http://crocutax.com/blog/mybatis-one-to-many-nestes-query-and-page-query Mybatis一对多嵌套查询和 ...
- 分布式通信-tcp/ip 广播
服务端 /** * 广播 */ public class MulticastServer { public static void main(String[] args) { try { //地址是2 ...
- java简单操作redis数据库
package RedisTest; import redis.clients.jedis.Jedis; public class RedisTest { private static String ...
- sQL存储过程的优缺点
目前具体的项目中没有使用sql存储过程,都用的封装好的框架,简单说下存储过程的优缺点. 优点: 1.存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般 SQL 语句每执行一次就编 ...
- pwnhub 相对路径覆盖
这个pwnhub小m师傅的题,做的时候完全没有思路. 首先是注册然后可以看到一个加载css的地方,是相对路径加载(当然我并没有觉得有什么问题). 服务端和浏览器解析URL是有区别的,就是%2f 服务器 ...
- POJ1129(贪心)
笔者休息娱乐.贪心即可,爱怎么暴力怎么暴力.莽WA,改了几下算法发现是输出中二了-- int T, color[26], tot; bool adj[26][26]; string s; void g ...
- bryce1010专题训练——Splay树
Prob Hint BZOJ 3323 文艺平衡树 区间翻转 BZOJ 1251 序列终结者 区间翻转,询问最值 BZOJ 1895 supermemo 区间加,翻转,剪切,询问最值.点插入,删除. ...
- 简单的鼠标经过特效-mouse事件
<!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...
- ubuntu下lnmp添加虚拟目录没有权限
lnmp.org下载的lnmp集成环境,通过lnmp vhost tsp创建了虚拟主机目录,将此目录导入到phpstorm中时提示错误,应该时权限的问题,想通过chmod -R 777 tsp来改变t ...