您的位置:首页 > 运维架构 > Nginx

lnmp (nginx php-fpm mysql) 环境部署——nginx

2016-11-18 11:29 627 查看
前言:
随着业务的发展,原有的单服务器架构已无法支撑现有业务的访问量,经研究决定,对现有服务做拆分,并对应用做横向扩展。目的是为了减轻服务集中造成的压力。

架构:

前端使用nginx作为web服务,后端使用php-fpm作为应用服务,数据库为mysql
应用服务可使用多台服务器做集群



准备工作:
系统为centos 6.8 x64 minimal

1 ip划分
web服务:172.16.10.0/24

应用服务:172.16.20.0/24
数据库服务:172.16.30.0/24

2 更新系统
yum update -y
3 安装开发工具
如使用源码编译安装,则在编译过程中需要用到开发工具。可直接安装”Development tools“,也可在编译过程中根据错误提示安装相应的开发工具.
此处给出添加开发工具组建命令,但不代表一定要添加

yum groupinstall "Development tools"
4 安装vim ntpdate
vim用于修改配置文件,ntpdate用于同步系统时间,以确保系统时间为正确时间
yum install vim ntpdate -y
ntpdate us.pool.ntp.org
5 添加crond任务,每天凌晨更新系统时间
crontab -e
0 0 * * * /usr/sbin/ntpdate us.pool.ntp.org
service crond restart
这里写ntpdate 命令的绝对路径,是因为crontab 默认在/bin/ 目录下查找命令

安装:
安装有两种方式,一种是使用官方库,一种是使用源码编译
简单说下源码编译与二进制包的区别
二进制包:就是官方或第三方按照默认参数编译好的二进制包
源码编译:可根据需求选择安装需要的相关模块,以及添加第三方模块,亦可对各参数做相应调整

二进制包安装更容易些,源码编译常遇到的是缺失依赖问题

两种安装方式会分别介绍

使用官方库安装
nginx稳定版目前已更新到1.10.2

nginx官方 http://nginx.org/

官方给出的二进制包安装指导http://nginx.org/en/linux_packages.html#stable

1 添加repo
tee /etc/yum.repos.d/nginx.repo <<-'EOF'
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=1
enabled=1
EOF
2 添加签名
curl -O http://nginx.org/keys/nginx_signing.key rpm --import nginx_signing.key
3 安装nginx
yum update
yum install nginx
4 启动nginx并添加服务
service nginx start
chkconfig --add nginx && chkconfig --level 2345 nginx on


源码编译安装:
1 下载官方源码及签名文件
curl -O http://nginx.org/download/nginx-1.10.2.tar.gz curl -O http://nginx.org/download/nginx-1.10.2.tar.gz.asc[/code]2 验证签名
gpg --verify nginx-1.10.2.tar.gz.asc
会看到如下提示,并给出公钥ID
gpg: Signature made Tue Oct 18 23:04:49 2016 CST using RSA key ID A1C052F8
gpg: Can't check signature: No public key
导入公钥ID

gpg --keyserver keys.gnupg.net --recv-key A1C052F8
再次验证
gpg --verify nginx-1.10.2.tar.gz.asc
看到红色字体即表明验证成功
gpg: Signature made Tue Oct 18 23:04:49 2016 CST using RSA key ID A1C052F8
gpg: Good signature from "Maxim Dounin <mdounin@mdounin.ru>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
Primary key fingerprint: B0F4 2533 73F8 F6F5 10D4 2178 520A 9993 A1C0 52F8

3 安装编译工具
yum install -y gcc gcc-c++ openssl-devel gd-devel
4 下载pcre
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz /usr/local/src/
cd /usr/local/src
tar zxf pcre-8.39.tar.gz


5 解压
tar zxf nginx-1.10.2.tar.gz
6 配置编译
cd nginx-1.10.2

./configure --prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module\
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/scgi/ \
--with-pcre=/usr/local/src/pcre-8.39 \
--with-http_image_filter_module \
--with-file-aio --with-http_realip_module
7 编译
make
make install
8 配置启动脚本
官方wiki给出了启动脚本,可直接使用,如编译时修改了默认路径,需要对脚本做相应修改
https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/?highlight=script%20nginx

vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
修改nginx执行权限
chmod u+x /etc/init.d/nginx
添加nginx服务
chkconfig --add nginx && chkconfig --level 2345 nginx on
9 防火墙配置
web 服务默认使用80端口,在配置完nginx服务后,需要对防火墙做配置,即开放80端口服务
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
在INPUT链中插入一条接受tcp协议的80端口的规则,默认插入位置为INPUT链的第一条

可通过客户端浏览器访问nginx服务ip地址查看是否配置成功,若成功会看到nginx默认欢迎页
也可通过curl命令查看头信息,注意看http状态码,将nginxip替换成实际ip地址
curl --head nginxip
curl --head 172.16.10.10
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Fri, 18 Nov 2016 03:24:06 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 17 Nov 2016 07:28:02 GMT
Connection: keep-alive
ETag: "582d5c02-264"
Accept-Ranges: bytes


注:

可通过./configure --help 查看参数说明帮助,配置需要要编译的以及关闭不需要的模块
--with为需要的,--without为不需要的
新版本增加了支持动态模块,如编译了动态加载模块,在使用时需要在配置文件中加载模块
load_module modules/ngx_mail_module.so;


--with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
--with-http_image_filter_module=dynamic
enable dynamic ngx_http_image_filter_module
--with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
--with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
--with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
--with-stream=dynamic              enable dynamic TCP/UDP proxy module
--add-dynamic-module=PATH          enable dynamic external module
--add-dynamic-module 可增加第三方的模块,如要使用需要指定第三方模块路径
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx install