配置一个nginx+php-fpm的web服务器
一、基本信息
- 系统(L):CentOS 6.9 #下载地址:http://mirrors.sohu.com
- 反代&负载均衡(N):NGINX 1.14.0 #下载地址:http://nginx.org/en/download.html
- OPENSSL:openssl-1.1.0h #下载地址:https://www.openssl.org/source/
指定服务安装的通用位置
mkdir /usr/local/services
SERVICE_PATH=/usr/local/services
创建服务运行的账户
useradd -r -M -s /sbin/nologin www
安装所需依赖包
yum -y install \
pcre pcre-devel \
gperftools \
gcc \
zlib-devel \
openssl-devel
二、软件安装配置
1、NGINX+OPENSSL安装
下载解压NGINX+OPENSSL
NGINX_URL="http://nginx.org/download/nginx-1.14.0.tar.gz"
OPENSSL_URL="https://www.openssl.org/source/openssl-1.1.0h.tar.gz"
wget -P ${SERVICE_PATH} ${NGINX_URL} && tar -zxvf ${SERVICE_PATH}/nginx*.tar.gz -C ${SERVICE_PATH}
wget -P ${SERVICE_PATH} ${OPENSSL_URL} && tar -zxvf ${SERVICE_PATH}/openssl*.gz -C ${SERVICE_PATH}
编译安装NGINX
cd ${SERVICE_PATH}/nginx-*;./configure \
--prefix=${SERVICE_PATH}/nginx \
--user=www --group=www \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_flv_module \
--with-pcre \
--with-http_gzip_static_module \
--with-openssl=${SERVICE_PATH}/openssl-* \
--with-http_realip_module \
--with-google_perftools_module \
--without-select_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--without-poll_module \
--without-http_autoindex_module \
--without-http_geo_module \
--without-http_uwsgi_module \
--without-http_scgi_module \
--without-http_memcached_module \
--with-cc-opt='-O2' && cd ${SERVICE_PATH}/nginx-*;make && make install
2、生成配置文件
写入主配置文件nginx.conf(配置已优化)
cat << EOF >${SERVICE_PATH}/nginx/conf/nginx.conf
user www;
worker_processes WORKERNUMBER;
worker_cpu_affinity auto;
worker_rlimit_nofile 655350;
error_log /var/log/nginx_error.log;
pid /var/run/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;
events {
use epoll;
worker_connections 655350;
multi_accept on;
}
http {
charset utf-8;
include mime.types;
default_type text/html;
log_format main '"\$remote_addr" - [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" '
'"\$sent_http_server_name \$upstream_response_time" '
'\$request_time '
'\$args';
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 120;
client_body_buffer_size 512k;
client_header_buffer_size 64k;
large_client_header_buffers 4 32k;
client_max_body_size 300M;
client_header_timeout 15s;
client_body_timeout 50s;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
server_names_hash_max_size 2048;
server_names_hash_bucket_size 256;
server_tokens off;
gzip on;
gzip_proxied any;
gzip_min_length 1024;
gzip_buffers 4 8k;
gzip_comp_level 9;
gzip_disable "MSIE [1-6]\.";
gzip_types application/json test/html text/plain text/css application/font-woff application/pdf application/octet-stream application/x-javascript application/javascript application/xml text/javascript;
include proxy.conf;
include vhost/*.conf;
}
EOF
写入反向代理配置文件proxy.conf
cat << EOF > ${SERVICE_PATH}/nginx/conf/proxy.conf
proxy_ignore_client_abort on;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 ;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_connect_timeout 60s;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_redirect off;
proxy_cache_lock on;
proxy_cache_lock_timeout 5s;
proxy_hide_header Vary;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Accept-Encoding '';
proxy_set_header Host \$host;
proxy_set_header Referer \$http_referer;
proxy_set_header Cookie \$http_cookie;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$remote_addr;
EOF
NGINX worker进程数配置,指定为逻辑CPU数量的2倍
THREAD=`expr $(grep process /proc/cpuinfo |wc -l) \* 2`
sed -i s"/WORKERNUMBER/$THREAD/" ${SERVICE_PATH}/nginx/conf/nginx.conf
三、安装完成后的清理与生成目录快捷方式
rm -rf ${SERVICE_PATH}/{nginx*.tar.gz,openssl*.tar.gz}
rm -rfv ${SERVICE_PATH}/nginx/conf/*.default
ln -sv ${SERVICE_PATH}/nginx /usr/local/
四、启动服务
生成nginx系统服务脚本,并加入开机启动项
cat << EOF > /etc/init.d/nginx
#!/bin/bash
#
# 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
#create temp dir in memory
mkdir -p /dev/shm/nginx/fastcgi_temp/
# Check that networking is up.
[ "\$NETWORKING" = "no" ] && exit 0
TENGINE_HOME="/usr/local/nginx/"
nginx=\$TENGINE_HOME"sbin/nginx"
prog=\$(basename \$nginx)
NGINX_CONF_FILE=\$TENGINE_HOME"conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x \$nginx ] || exit 5
[ -f \$NGINX_CONF_FILE ] || exit 6
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
killall -9 nginx
}
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
EOF
chmod a+x /etc/init.d/nginx
chkconfig nginx --add && chkconfig nginx on
启动服务
service nginx start
五、配置一个负载均衡与反向代理
mkdir ${SERVICE_PATH}/nginx/conf/vhost
cat << EOF > ${SERVICE_PATH}/nginx/conf/vhost/erbiao.px.com.conf
upstream api_php {
server 172.25.10.127:80 max_fails=3 fail_timeout=30s;
server 172.25.10.129:80 max_fails=3 fail_timeout=30s;
server 172.25.10.130:80 max_fails=3 fail_timeout=30s;
server 172.25.10.131:80 max_fails=3 fail_timeout=30s;
server 172.25.10.128:80 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name erbiao.px.com;
location / {
proxy_pass http://api_php;
}
access_log off;
}
EOF
重启服务
service nginx restart
六、命令其他选项
nginx
├── -s选项,向主进程发送信号
| ├── reload参数,重新加载配置文件
| ├── stop参数,快速停止nginx
| ├── reopen参数,重新打开日志文件
| ├── quit参数,Nginx在退出前完成已经接受的连接请求
├── -t选项,检查配置文件是否正确
├── -c选项,用于指定特定的配置文件并启动nginx
├── -V选项(大写),显示nginx编译选项与版本信息
配置一个nginx+php-fpm的web服务器的更多相关文章
- 配置一个nginx反向代理&负载均衡服务器
一.基本信息 系统(L):CentOS 6.9 #下载地址:http://mirrors.sohu.com 反代&负载均衡(N):NGINX 1.14.0 #下载地址:http://nginx ...
- Nginx系列2:用Nginx搭建一个可用的静态资源Web服务器
上一节中编译好自己的nginx服务器后, 现在要对nginx.conf文件进行配置,搭建一个可用的静态资源Web服务器 1.放入可访问的html文件到nginx文件夹下,如图1所示: 这里我放入的是一 ...
- Nginx+uWSGI+Django部署web服务器
目录 Nginx+uWSGI+Django部署web服务器 环境说明 前言 搭建项目 Django部署 编辑luffy/luffy/settings.py 编辑luffy/app01/views.py ...
- nginx搭建前端项目web服务器以及利用反向代理调试远程后台接口
前端同学用nginx搭建自己的web服务器,后台程序专门部署在一台服务器上(我们之前公司就有三套环境,开发/测试/生产),这样做的好处是 1.前端代码基本都是静态文件,重启一次很快,也就几秒钟时间. ...
- 利用Nginx实现反向代理web服务器
一.Nginx简介 Nginx是一个很强大的高性能Web服务器和反向代理服务器,它具有很多非常优越的特性: 可以高并发连接 内存消耗少 成本低廉 配置文件非常简单 支持Rewrite重写 内置的健康检 ...
- nginx做rails项目web服务器缓存配置方法
nginx作为Web服务器.或反向代理服务器都可以使用缓存 一.作为Web服务器 nginx可以通过 expires 指令来设置响应头的过期时间,实现浏览器缓存(Browser Caching),即浏 ...
- 从零开始学 Java - 利用 Nginx 负载均衡实现 Web 服务器更新不影响访问
还记得那些美妙的夜晚吗 你洗洗打算看一个小电影就睡了,这个时候突然想起来今天晚上是服务器更新的日子,你要在凌晨时分去把最新的代码更新到服务器,以保证明天大家一觉醒来打开网站,发现昨天的 Bug 都不见 ...
- nginx反向代理后端web服务器记录客户端ip地址
nginx在做反向代理的时候,后端的nginx web服务器log中记录的地址都是反向代理服务器的地址,无法查看客户端访问的真实ip. 在反向代理服务器的nginx.conf配置文件中进行配置. lo ...
- Tomcat结合Apache、Nginx实现高性能的web服务器
一.Tomcat为什么需要与apache.nginx一起结合使用? Tomcat虽然是一个servlet和jsp容器,但是它也是一个轻量级的web服务器.它既可以处理动态内容,也可以处理静态内容.不过 ...
随机推荐
- Maximum Subarray解题报告zz
http://fisherlei.blogspot.com/2012/12/leetcode-maximum-subarray.html Find the contiguous subarray wi ...
- Ubuntu下安装CUDA
cuda check: cuDNN 下载cuDNN后解压 更新软链接 更新链接库 symbol link 参考链接: http://docs.nvidia.com/cuda/cuda-installa ...
- 【pbrt】在c++程序中使用pbrt进行渲染
近段时间做一个关于水面的动画.由于我用c++实现水面动画的,然而使用c++我自己的渲染系统渲染结果被同学说是可视化不叫渲染,所以我决定修改一下…… 恰好进来在学习pbrt,所以索性就蛋疼了考虑直接用p ...
- Eclipse环境下配置Tomcat,并且把项目部署到Tomcat服务器上
一 配置Tomcat 1.打开Eclipse,单击"Window"菜单,选择下方的"Preferences". 2.单击"Server"选项 ...
- [原]零基础学习SDL开发之在Android使用SDL2.0加载字体
在上一篇文章我们知道了如何在android使用SDL2.0来渲染显示一张png图,而且在上上一篇我们知道如何使用sdl来渲染输出bmp图,那么sdl是否可以渲染输出自己喜爱的字体库的字体呢?答案是当然 ...
- 关于一篇对epoll讲的比较好的一篇文章
原文地址http://www.cnblogs.com/lojunren/p/3856290.html 前言 I/O多路复用有很多种实现.在linux上,2.4内核前主要是select和poll,自Li ...
- 4.30-5.1cf补题
//yy:拒绝转载!!! 悄悄告诉你,做题累了,去打两把斗地主就能恢复了喔~~~ //yy:可是我不会斗地主吖("'▽'") ~~~那就听两遍小苹果嘛~~~ 五一假期除了花时间建模 ...
- MATLAB入门学习(六)
今天学三维作图 (*^__^*)…… 三维曲线作图 用到的命令:plot3 基本格式:plot3(x,y,z,s) 这里要画曲线,你需要知道该曲线的参数方程x=x(t),y=y(t),z=z(t) 然 ...
- HDU 6206 Apple (高精确度+JAVA BigDecimal)
Problem Description Apple is Taotao's favourite fruit. In his backyard, there are three apple trees ...
- Swift3.0 调用C函数-_silen_name
一般情况下Swit要想调用obj-c,c或c++代码必须通过obj-c以及桥接文件才可以办到,但是使用@_silgen_name,可以对于某些简单的代码,直接跳过桥接文件和.h头文件与C代码交互. 创 ...