1.创建临时目录
cd /
mkdir package
chmod -R 0777 package
cd package
2.获取nginx安装包
wget http://nginx.org/download/nginx-1.13.9.tar.gz
3.解压安装包
tar -xf nginx-1.13.9.tar.gz
切换目录
cd nginx-1.13.9
4.配置 nginx
./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module
出现如下内容说明配置成功,记录了你的 Nginx 相关配置信息。
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "
"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
可能出现错误
缺少编译器,错误信息如下:
./configure: error: C compiler cc is not found
- 1
解决方法:安装gcc-c++
yum -y install gcc-c++ autoconf automake
在配置信息./configure --prefix=/usr/local/nginx
的时,出现错误:
/configure: error: the HTTP rewrite module requires the PCRE library.
解决方法:安装pcre
yum -y install pcre pcre-devel
-y 是跳过所有需要手动确认的环节
缺少ssl错误,错误信息如下:
./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.
解决方法:安装openssl
yum -y install openssl openssl-devel
autoconf是自动配置,automake是自动编译
缺少zlib包,错误信息如下:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using –without-http_gzip_module
option, or install the zlib library into the system, or build the zlib
library
statically from the source with nginx by using –with-zlib=<path> option.
解决方法:安装zlib
yum install -y zlib-devel
确实libxml2,错误信息如下:
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
解决方法:
yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel
http_image_filter_module是nginx提供的集成图片处理模块,需要gd-devel的支持,错误信息如下:
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
解决方法:
yum -y install gd-devel
缺少ExtUtils,错误信息如下:
./configure: error: perl module ExtUtils::Embed is required
解决方法:
yum -y install perl-devel perl-ExtUtils-Embed
缺少GeoIP,错误信息如下:
./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.
解决方法:
yum -y install GeoIP GeoIP-devel GeoIP-data
5.编译安装
make
make install
make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件;
make install是把这些编译出来的可执行文件和库文件复制到合适的地方。
提示如下成功:
cp objs/ngx_http_xslt_filter_module.so '/usr/local/nginx/modules/ngx_http_xslt_filter_module.so'
make[1]: 离开目录“/root/nginx-1.11.10”
添加用户
useradd -s /sbin/nologin -M nginx
id nginx
检查是否安装成功
执行如下命令启动,并用 ps 命令检查启动情况
/usr/sbin/nginx -c /etc/nginx/nginx.conf
ps -ef | grep nginx
添加 nginx 到系统服务
vim /usr/lib/systemd/system/nginx.service
增加如下内容(结合安装时的配置文件进行修改),其中 特殊的环境变量 $MAINPID 可用于表示主进程的PID
。有关 systemd.service 的配置,可以参加下面两篇文章:
systemd 入门教程:命令篇
Systemd 入门教程:实战篇
systemd.service 中文手册
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/sbin/nginx -c /etc/nginx/nginx.conf -t
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
:wq 保存退出
停止原先启动的 Nginx 进程,
/usr/sbin/nginx -s stop
ps -ef|grep nginx
systemctl daemon-reload
systemctl start nginx.service
报错 nginx: [emerg] getpwnam("nginx") failed
解决办法:
/usr/sbin/groupadd -f nginx
/usr/sbin/useradd -g nginx nginx
服务启动后,打开浏览器,输入您网站的地址,查看欢迎页面是否正常显示。
不知道 ip 的,可以通过如下命令找到本机 ip 地址
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
命令行测试网页
curl http://yourip
安装 MySQL (MariaDB)
sudo yum install mariadb-server mariadb
sudo systemctl start mariadb
增加 mysql 安全性
sudo mysql_secure_installation
设置为系统启动加载
sudo systemctl enable mariadb
安装 php7
//编译安装
###############下载 php7.2.5
[root@localhost package]# wget http://cn2.php.net/get/php-7.2.5.tar.gz/from/this/mirror
--2019-01-31 10:52:49-- http://cn2.php.net/get/php-7.2.5.tar.gz/from/this/mirror
Resolving cn2.php.net (cn2.php.net)... 36.51.255.144
Connecting to cn2.php.net (cn2.php.net)|36.51.255.144|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://cn2.php.net/distributions/php-7.2.5.tar.gz [following]
--2019-01-31 10:52:50-- http://cn2.php.net/distributions/php-7.2.5.tar.gz
Reusing existing connection to cn2.php.net:80.
HTTP request sent, awaiting response... 200 OK
Length: 18305092 (17M) [application/x-gzip]
Saving to: ‘mirror’
100%[==========================================================>] 18,305,092 934KB/s in 23s
2019-01-31 10:53:13 (792 KB/s) - ‘mirror’ saved [18305092/18305092]
[root@localhost package]# ll
total 17880
-rw-r--r--. 1 root root 18305092 Apr 24 2018 mirror
drwxr-xr-x. 9 nginx rvm 186 Jan 30 10:17 nginx-1.13.9
#############下载完后的mirror 其实是一个 .tar.gz 压缩包
##########解压压缩包
[root@localhost package]# tar -zxvf mirror
[root@localhost package]# ll
total 17884
-rw-r--r--. 1 root root 18305092 Apr 24 2018 mirror
drwxr-xr-x. 9 nginx rvm 186 Jan 30 10:17 nginx-1.13.9
drwxrwxr-x. 14 root root 4096 Apr 24 2018 php-7.2.5
[root@localhost package]# cd php-7.2.5
#############安装 curl拓展包
[root@localhost php-7.2.5]# yum install -v curl-devel
#############CentOS 7 安装 oniguruma 和 oniguruma-devel
[root@localhost php-7.2.5]# yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-6.7.0-1.el7.x86_64.rpm
[root@localhost php-7.2.5]# yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
#############CentOS 8 安装 oniguruma 和 oniguruma-devel
[root@localhost php-7.2.5]# yum config-manager --set-enabled PowerTools
[root@localhost php-7.2.5]# yum -y install oniguruma oniguruma-devel
#############编译配置
[root@localhost php-7.2.5]# ./configure --prefix=/usr/local/php7.2.5 -with-config-file-path=/etc --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip --enable-opcache
#################编译安装
[root@localhost php-7.2.5]# make && make install
配置 apache的 变量
find / -name apxs
./configure --with-apxs2=/usr/bin/apxs
注意:上面命令最后的–with-apxs2=/usr/bin/apxs千万不能少,否则在Apache下的modules目录下不会生成libphp7.so模块
报错:PHP无法编译undefined reference to `libiconv_open ,安装libiconv
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
tar -zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr/local/libiconv
make
make install
然后php编译安装时指定路径
./configure --with-iconv=/usr/local/libiconv
配置php
############切换到php安装目录
[root@localhost php-7.2.5]# cd /usr/local/php7.2.5
[root@localhost php7.2.5]# ll
total 0
drwxr-xr-x. 2 root root 146 Jan 31 11:03 bin
drwxr-xr-x. 3 root root 68 Jan 31 11:03 etc
drwxr-xr-x. 3 root root 17 Jan 31 11:03 include
drwxr-xr-x. 3 root root 17 Jan 31 11:03 lib
drwxr-xr-x. 4 root root 28 Jan 31 11:03 php
drwxr-xr-x. 2 root root 21 Jan 31 11:03 sbin
drwxr-xr-x. 4 root root 28 Jan 31 11:03 var
##########切换到 php 的 etc目录
[root@localhost php7.2.5]# cd etc
[root@localhost etc]# ll
total 12
-rw-r--r--. 1 root root 1285 Jan 31 11:03 pear.conf
-rw-r--r--. 1 root root 4488 Jan 31 11:03 php-fpm.conf.default
drwxr-xr-x. 2 root root 30 Jan 31 11:03 php-fpm.d
############## 查找php.ini配置文件 ,复制过来
[root@localhost etc]# find / -name php.ini*
/package/php-7.2.5/php.ini-development
/package/php-7.2.5/php.ini-production
[root@localhost etc]# cp /package/php-7.2.5/php.ini-development php.ini
[root@localhost etc]# ll
total 84
-rw-r--r--. 1 root root 1285 Jan 31 11:03 pear.conf
-rw-r--r--. 1 root root 4488 Jan 31 11:03 php-fpm.conf.default
drwxr-xr-x. 2 root root 30 Jan 31 11:03 php-fpm.d
-rw-r--r--. 1 root root 70177 Jan 31 11:10 php.ini
############# 编辑配置 php.ini
[root@localhost etc]# vi php.ini
##########大概 769行,把 fix_pathinfo设置为0
769 cgi.fix_pathinfo=0
##########大概 932行, 配置时区
932 date.timezone = Asia/Shanghai
######保存 :wq 退出
########## 设置php 环境变量
[root@localhost etc]# ln -s /usr/local/php7.2.5/etc/php.ini /etc/php.ini
[root@localhost etc]# ln -s /usr/local/php7.2.5/bin/php /usr/bin/php
###### php配置完成
[root@localhost etc]# php -v
PHP 7.2.5 (cli) (built: Jan 31 2019 11:02:47) ( NTS DEBUG )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
配置php-fpm
1.复制配置文件,配置环境变量
############# 复制 php-fpm 配置文件
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
######### 配置 php-fpm.conf ################
[root@localhost etc]# vi php-fpm.conf
# 大概 17 行,开启 pid
17 pid = run/php-fpm.pid
# :wq 保存退出 ################
######### 配置 php-fpm.d/www.conf ################
[root@localhost etc]# vi php-fpm.d/www.conf
# 大概 23 行,设置用户和用户组为 nginx
23 user = nginx
24 group = nginx
# 大概 36 行,关闭监听端口 9000 , 添加监听 php-fpm.sock
36 ;listen = 127.0.0.1:9000
37 listen = /dev/shm/php-fpm.sock
# 大概 48 行,开启 监听的用户 nginx
48 listen.owner = nginx
49 listen.group = nginx
50 listen.mode = 0660
# :wq 保存退出 ################
##############配置环境变量
[root@localhost etc]# ln -s /usr/local/php7.2.5/sbin/php-fpm /usr/bin/php-fpm
[root@localhost etc]# php-fpm -v
PHP 7.2.5 (fpm-fcgi) (built: Jan 31 2019 11:02:57) (DEBUG)
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
2.复制php-fpm启动文件,启动php-fpm
######### 查找 php-fpm 的系统启动文件
[root@localhost etc]# find / -name init.d.php-fpm
/package/php-7.2.5/sapi/fpm/init.d.php-fpm
####### 复制启动文件 到 etc/init.d目录下
[root@localhost etc]# cp /package/php-7.2.5/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
############ 配置权限 并添加到自启动列表
[root@localhost etc]# chmod 755 /etc/init.d/php-fpm
[root@localhost etc]# chkconfig --add php-fpm
[root@localhost etc]# chkconfig php-fpm on
########## 配置完毕
[root@localhost etc]# service php-fpm start
Starting php-fpm done
php7以上自带zend_opcache,修改php.ini 即可使用
[root@izj6c4jirdug8kh3uo6rdez php-7.3.7]# vi /etc/php.ini
#添加 zend拓展 opcache
zend_extension=opcache
[opcache]
;开启opcache
opcache.enable=1
;CLI环境下,PHP启用OPcache
opcache.enable_cli=1
;OPcache共享内存存储大小,单位MB
opcache.memory_consumption=128
;PHP使用了一种叫做字符串驻留(string interning)的技术来改善性能。例如,如果你在代码中使用了1000次字符串“foobar”,在PHP内部只会在第一使用这个字符串的时候分配一个不可变的内存区域来存储这个字符串,其他的999次使用都会直接指向这个内存区域。这个选项则会把这个特性提升一个层次——默认情况下这个不可变的内存区域只会存在于单个php-fpm的进程中,如果设置了这个选项,那么它将会在所有的php-fpm进程中共享。在比较大的应用中,这可以非常有效地节约内存,提高应用的性能。
这个选项的值是以兆字节(megabytes)作为单位,如果把它设置为16,则表示16MB,默认是4MB
opcache.interned_strings_buffer=8
;这个选项用于控制内存中最多可以缓存多少个PHP文件。这个选项必须得设置得足够大,大于你的项目中的所有PHP文件的总和。
设置值取值范围最小值是 200,最大值在 PHP 5.5.6 之前是 100000,PHP 5.5.6 及之后是 1000000。也就是说在200到1000000之间。
opcache.max_accelerated_files=4000
;设置缓存的过期时间(单位是秒),为0的话每次都要检查
opcache.revalidate_freq=60
;如果启用(设置为1),OPcache会在opcache.revalidate_freq设置的秒数去检测文件的时间戳(timestamp)检查脚本是否更新。
如果这个选项被禁用(设置为0),opcache.revalidate_freq会被忽略,PHP文件永远不会被检查。这意味着如果你修改了你的代码,然后你把它更新到服务器上,再在浏览器上请求更新的代码对应的功能,你会看不到更新的效果
强烈建议你在生产环境中设置为0,更新代码后,再平滑重启PHP和web服务器。
opcache.validate_timestamps=0
;开启Opcache File Cache(实验性), 通过开启这个, 我们可以让Opcache把opcode缓存缓存到外部文件中, 对于一些脚本, 会有很明显的性能提升.
这样PHP就会在/tmp目录下Cache一些Opcode的二进制导出文件, 可以跨PHP生命周期存在.
opcache.file_cache=/tmp
#保存退出
:wq
# 重启php-fpm
[root@izj6c4jirdug8kh3uo6rdez php-7.3.7]# service php-fpm restart
# 查看开启的php模块
[root@izj6c4jirdug8kh3uo6rdez php-7.3.7]# php -m
[PHP Modules]
xmlrpc
xmlwriter
xsl
Zend OPcache
zip
zlib
[Zend Modules]
Zend OPcache
[root@izj6c4jirdug8kh3uo6rdez php-7.3.7]# php -v
PHP 7.3.7 (cli) (built: Jul 9 2019 11:59:23) ( NTS DEBUG )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.7, Copyright (c) 1999-2018, by Zend Technologies
php5.6配置安装zend_opcache
####下载 目前最新的是 7.0.5稳定版本
[root@localhost package]# wget http://pecl.php.net/get/zendopcache-7.0.5.tgz
#解压
[root@localhost package]# tar -zxvf zendopcache-7.0.5.tgz
[root@localhost package]# cd zendopcache-7.0.5
[root@localhost zendopcache-7.0.5]# /usr/local/php7.2.5/bin/phpize
Configuring for:
PHP Api Version: 20170718
Zend Module Api No: 20170718
Zend Extension Api No: 320170718
#找到php安装目录下的 php-config
[root@izj6c4jirdug8kh3uo6rdez zendopcache-7.0.5]# find / -name php-config
/package/php-5.6.0/scripts/php-config
/usr/local/php5.6.0/bin/php-config
# 配置
[root@izj6c4jirdug8kh3uo6rdez zendopcache-7.0.5]# ./configure --with-php-config=/usr/local/php5.6.0/bin/php-config
#安装
[root@localhost zendopcache-7.0.5]# make && make install
#开启,修改php.ini配置
在PHP.ini添加或修改如下代码
zend_extension=/usr/local/php/lib/php/extensions/debug-non-zts-20170718/opcache.so
[opcache]
;开启opcache
opcache.enable=1
;CLI环境下,PHP启用OPcache
opcache.enable_cli=1
;OPcache共享内存存储大小,单位MB
opcache.memory_consumption=128
;PHP使用了一种叫做字符串驻留(string interning)的技术来改善性能。例如,如果你在代码中使用了1000次字符串“foobar”,在PHP内部只会在第一使用这个字符串的时候分配一个不可变的内存区域来存储这个字符串,其他的999次使用都会直接指向这个内存区域。这个选项则会把这个特性提升一个层次——默认情况下这个不可变的内存区域只会存在于单个php-fpm的进程中,如果设置了这个选项,那么它将会在所有的php-fpm进程中共享。在比较大的应用中,这可以非常有效地节约内存,提高应用的性能。
这个选项的值是以兆字节(megabytes)作为单位,如果把它设置为16,则表示16MB,默认是4MB
opcache.interned_strings_buffer=8
;这个选项用于控制内存中最多可以缓存多少个PHP文件。这个选项必须得设置得足够大,大于你的项目中的所有PHP文件的总和。
设置值取值范围最小值是 200,最大值在 PHP 5.5.6 之前是 100000,PHP 5.5.6 及之后是 1000000。也就是说在200到1000000之间。
opcache.max_accelerated_files=4000
;设置缓存的过期时间(单位是秒),为0的话每次都要检查
opcache.revalidate_freq=10
;从字面上理解就是“允许更快速关闭”。它的作用是在单个请求结束时提供一种更快速的机制来调用代码中的析构器,从而加快PHP的响应速度和PHP进程资源的回收速度,这样应用程序可以更快速地响应下一个请求。把它设置为1就可以使用这个机制了。
opcache.fast_shutdown=1
;如果启用(设置为1),OPcache会在opcache.revalidate_freq设置的秒数去检测文件的时间戳(timestamp)检查脚本是否更新。
如果这个选项被禁用(设置为0),opcache.revalidate_freq会被忽略,PHP文件永远不会被检查。这意味着如果你修改了你的代码,然后你把它更新到服务器上,再在浏览器上请求更新的代码对应的功能,你会看不到更新的效果
强烈建议你在生产环境中设置为0,更新代码后,再平滑重启PHP和web服务器。
opcache.validate_timestamps=0
;开启Opcache File Cache(实验性), 通过开启这个, 我们可以让Opcache把opcode缓存缓存到外部文件中, 对于一些脚本, 会有很明显的性能提升.
这样PHP就会在/tmp目录下Cache一些Opcode的二进制导出文件, 可以跨PHP生命周期存在.
opcache.file_cache=/tmp
配置 Nginx 使其能执行 php 页面
vi /etc/nginx/nginx.conf
主要有如下步骤:
-
设置 root 根目录
-
添加 index.php 为默认目录首页请求
-
修改 server_name ,添加域名或 IP
-
定义 404 等错误页面的规则
-
配置 PHP 处理模块,主要调整
location ~ \.php$ {
部分,尤其注意fastcgi_pass unix:
指向的 php-fpm.sock 路径是否正确; -
保存后重启 nginx 服务
我的配置文件大致如下:
server {
listen 80;
server_name server_domain_name_or_IP;
# note that these lines are originally from the "location /" block
root /www;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /www;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
在根目录添加一个 phpinfo 的 php 文件,打开浏览器进行测试是否正常显示。
成功后删除测试页面。
安装 phpmyadmin
去官网下载解压即可:
wget https://files.phpmyadmin.net/phpMyAdmin/4.6.6/phpMyAdmin-4.6.6-all-languages.zip
unzip phpMyAdmin-4.6.6-all-languages.zip -d /www
cp config.sample.inc.php config.inc.php
如果浏览器打开出现如下错误:
session_start(): open(SESSION_FILE, O_RDWR) failed: Permission de
则修改报错信息文件的所有权,如下
chown nginx:nginx /var/lib/php/session
安装fastCGI
cd /package
wget http://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.4.tar.gz 下载fcgi
tar -xf spawn-fcgi-1.6.4.tar.gz 解压
cd spawn-fcgi-1.6.4 切换目录
./configure 编译
make && make install 安装
附上 nginx 配置反向代理和负载均衡的文件配置:
# 运行用户
user nginx;
# 启动进程,通常设置成和cpu的数量相等
worker_processes 1;
# 全局错误日志及PID文件
error_log /var/log/nginx/error.log;
# 工作模式及连接数上限
events {
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
worker_connections 1024; #单个后台worker process进程的最大并发链接数
#multi_accept on;
}
http {
#设定mime类型,类型由mime.type文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;
#设定日志格式
access_log off;
log_not_found off;
#access_log /var/log/nginx/access.log;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#将tcp_nopush和tcp_nodelay两个指令设置为on用于防止网络阻塞
tcp_nopush on;
tcp_nodelay on;
#连接超时时间
keepalive_timeout 65;
#开启gzip压缩
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#代理缓存设置
proxy_cache cache;
proxy_cache_key $scheme$proxy_host$request_uri;
proxy_cache_valid 200 5s;
add_header cache-status $upstream_cache_status;
#注意 /var/www/html/nginx/cache 目录需要手动创建 ############
proxy_cache_path /var/www/html/nginx/cache/proxy_cache levels=1:2 keys_zone=cache:512m inactive=5m max_size=8g use_temp_path=off;
#proxy_cache_path指令配置:
#evels=1:2:表示创建两级目录结构,缓存目录的第一级目录是1个字符,第二级目录是2个字符,比如/export/cache/proxy_cache/7/3c/,如果将所有文件放在一级目录下的话,文件量很大,会导致文件访问慢。
#keys_zone=cache:512m :设置存储所有缓存key和相关信息的共享内存区,1M大约能存储8000个key。
#inactive=5m :inactive指定被缓存的内容多久不被访问将从缓存中移除,以保证内容的新鲜,默认为10分钟。
#max_size=8g :最大缓存阀值,“cache manager”进程会监控最大缓存大小,当缓存达到该阀值时,该进程将从缓存中移除最近最少访问的内容。
#use_temp_path:如果为on,则内容首先被写入临时文件(proxy_temp_path ),然后重命名到proxy_cache_path指定的目录;如果设置为off,则内容直接被写入到proxy_cache_path指定的目录,如果需要cache建议off,则该特性是1.7.10提供的。
#优化输出缓存
fastcgi_buffers 256 16k;
fastcgi_buffer_size 128k;
fastcgi_connect_timeout 3s;
fastcgi_send_timeout 120s;
fastcgi_read_timeout 120s;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
upstream www {
ip_hash;
server 10.10.87.243:8001 weight=1 max_fails=2 fail_timeout=10s;
server 10.10.87.243:8002 weight=2 max_fails=2 fail_timeout=10s;
server 10.10.87.242:8001 weight=3 max_fails=2 fail_timeout=10s;
server 10.10.87.242:8002 weight=4 max_fails=2 fail_timeout=10s;
#当负载调度算法为ip_hash时,后端服务器在负载均衡调度中的状态不能有backup。
#server 10.10.87.241:80 backup; #备份服务器,上面服务器都不可用时,访问这里
}
#gzip on;
server {
listen 80;
server_name 10.10.87.243;
root /var/www/html/nginx;
index index.html index.htm index.php;
#charset koi8-r;
location / {
#请求转向mysvr 定义的服务器列表
proxy_pass http://www;
#以下是一些反向代理的配置可删除.
proxy_redirect off;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 3s; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 5s; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 5s; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
location /8002 {
#将 8002 目录代理到 8002端口下, proxy_pass 最后记得加上 /
proxy_pass http://10.10.87.243:8002/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root html;
#}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 8001;
server_name 10.10.87.243;
root /var/www/html/nginx/8001;
index index.html index.htm index.php;
location ~ \.php$ {
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
}
server {
listen 8002;
server_name 10.10.87.243;
root /var/www/html/nginx/8002;
index index.html index.htm index.php;
location ~ \.php$ {
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
}
server {
listen 555;
server_name 10.10.87.243;
root /var/www/html/tp51/public/;
index index.html index.htm index.php;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
}
}
location ~ \.php$ {
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}