使用cat,awk和sort命令从nginx访问日志中统计user-agent类型
业务场景描述如下:
我有一个Nginx的web服务器,需要从统计日志中统计有哪些类型的设备终端和浏览器访问了我的网站。
访问日志中的每条记录是这样的:
使用下面的命令得到user-agent所在的字段
cat cpm.access.log-20141211 | awk -F '"' '{print $6}' > ua_1211.txt
意思是,处理该文件的每一行,指定“为分隔符,只输出第6个文本域
提取的user-agent字段得到的文件为
很显然,需要对这些行进行去重,这只需要一个简单的sort命令就可以请求搞定:
sort ua_1211.txt | uniq -u > ua_1211.sort.txt
对文档按照ascii字符顺序进行排序,同时去掉重复行,并将结果重定向到新文件中
下面是去重后的文件内容
为了结果更精确,我需要对当前目录下面的所有日志都做相同的分析,为此,我需要先将这些文件都合并到一个大日志文件中,再对这个大文件执行上面的操作。使用cat的通配符合并功能很容易实现文件合并:
cat cpm.access.log-2014121* > cpm.access.log
再对这个合并文件cpm.access.log做分析
cat cpm.access.log | awk -F '"' '{print $6}' | sort | uniq -u > ua_analysis.txt
这次我们看到了更多神奇的东西,看来访问各种移动端访问我们网站还是很多的,总结有1333种终端访问。
其实类似的其他场景的问题也可以参照解决。
下面是我收集的user-agent文件,参见下载链接
观察日志发现UA字符串:
Mozilla/5.0 (compatible; Baiduspider/2.0; http://www.baidu.com/search/spider.html)
参考文献
[1].http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html
[2].http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858385.html
nginx自定义header的坑
Nginx1.8.0版本平滑升级新版本1.9.7
首先查看现在环境nginx的版本为1.8.0 编译的参数只指定了安装路径;
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
configure arguments: --prefix=/usr/local/nginx
平滑升级步骤如下:
下载nginx1.9.7版本,解压并进入解压后的目录
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# tar -zxvf nginx-1.9.7.tar.gz
[root@localhost src]# cd nginx-1.9.7
编译安装之前查看nginx进程ID信息:
root 894 1 0 16:06 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 896 894 0 16:06 ? 00:00:00 nginx: worker process
nobody 897 894 0 16:06 ? 00:00:00 nginx: worker process
编译安装:指定用户www 支持ssl 支持pcre 支持状态查询 支持静态压缩模块;
编译安装后可以echo $?查看是否成功,成功后只需要执行make,不需要make install;
平滑升级,先移走现有的nginx二进制文件
拷贝新生成的nginx二进制文件到指定目录
执行升级命令
/usr/local/nginx//sbin/nginx -t
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx//logs/nginx.pid`
sleep 1
test -f /usr/local/nginx//logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx//logs/nginx.pid.oldbin`
查看版本,发现已经是1.9.7版本,编译的参数也存在;
nginx version: nginx/1.9.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx/ --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
创建www用户
查看nginx进程 PID已经更改
root 3814 1 0 16:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 3815 3814 0 16:23 ? 00:00:00 nginx: worker process
nobody 3816 3814 0 16:23 ? 00:00:00 nginx: worker process
修改nginx.conf配置文件,更改用户和用户组为www用户,保存退出,重启nginx服务;
root 3814 1 0 16:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www 4200 3814 0 16:48 ? 00:00:00 nginx: worker process
www 4201 3814 0 16:48 ? 00:00:00 nginx: worker process
Nginx1.8.0版本平滑升级新版本1.9.7就给大家介绍到这里,希望对大家有所帮助!
ngxtop:在命令行实时监控 Nginx 的神器
Nginx网站服务器在生产环境中运行的时候需要进行实时监控。实际上,诸如Nagios, Zabbix, Munin 的网络监控软件是支持 Nginx 监控的。
如果你不需要以上软件提供的综合性报告或者长期数据统计功能,只是需要一种快速简便的办法去监控 Nginx 服务器的请求的话,我建议你采用一个叫 ngxtop 的命令行工具。
你马上就会发现 ngxtop 从界面和名称都借鉴了著名的top命令。ngxtop 是通过分析 Nginx 或者其他的日志文件,使用类似 top 命令的界面实时展示出来的。你可以说你知道的其他高端监控工具,但是在简洁这方面 ngxtop 无疑是最好的。简单就意味着不可替代。
本指南中,我将介绍如何使用 ngxtop 实时监控 Nginx 网站服务器。
Linux 上安装 ngxtop
首先在 Linux 系统中安装依赖库pip(LCTT译注:ngxtop是用python编写的)。
然后使用如下命令安装 ngxtop。
- $ sudo pip install ngxtop
ngxtop 使用
基本使用方法如下:
- ngxtop [options]
- ngxtop [options] (print|top|avg|sum) <var>
- ngxtop info
这里是一些通用选项。
- -l : 指定日志文件的完整路径 (Nginx 或 Apache2)
- -f : 日志格式
- --no-follow: 处理当前已经写入的日志文件,而不是实时处理新添加到日志文件的日志
- -t : 更新频率
- -n : 显示行号
- -o : 排序规则(默认是访问计数)
- -a ..., --a ...: 添加表达式(一般是聚合表达式如: sum, avg, min, max 等)到输出中。
- -v: 输出详细信息
- -i : 只处理符合规则的记录
以下是一些内置变量,他们的含义不言自明。
- bodybytessend
- http_referer
- httpuseragent
- remote_addr
- remote_user
- request
- status
- time_local
使用 ngxtop 监控 Nginx
ngxtop 默认会从其配置文件 (/etc/nginx/nginx.conf) 中查找 Nginx 日志的地址。所以,监控 Nginx ,运行以下命令即可:
- $ ngxtop
这将会列出10个 Nginx 服务,按请求数量排序。
显示前20个最频繁的请求:
- $ ngxtop -n 20
获取Nginx基本信息:
- $ ngxtop info
你可以自定义显示的变量,简单列出需要显示的变量。使用 "print" 命令显示自定义请求。
- $ ngxtop print request http_user_agent remote_addr
显示请求最多的客户端IP地址
- $ ngxtop top remote_addr
显示状态码是404的请求
- $ ngxtop -i 'status == 404' print request status
除了Nginx,ngtop 还可以处理其他的日志文件,比如 Apache 的访问文件。使用以下命令监控 Apache 服务器:
- $ tail -f /var/log/apache2/access.log | ngxtop -f common
nginx log 记录请求的头信息
记录访问的log,为了在出现特殊情况时,方便检查出现问题的地方。
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /var/log/51yip.log access;
记录下,用户ip,用户真实ip,用户访问时间,用户访问的游览器信息等。这里面这样的$remote_addr变量是从哪来的呢?
$arg_PARAMETER 这个变量包含在查询字符串时GET请求PARAMETER的值。
$args 这个变量等于请求行中的参数。
$binary_remote_addr 二进制码形式的客户端地址。
$body_bytes_sent
$content_length 请求头中的Content-length字段。
$content_type 请求头中的Content-Type字段。
$cookie_COOKIE cookie COOKIE的值。
$document_root 当前请求在root指令中指定的值。
$document_uri 与$uri相同。
$host 请求中的主机头字段,如果请求中的主机头不可用,则为服务器处理请求的服务器名称。
$is_args 如果$args设置,值为"?",否则为""。
$limit_rate 这个变量可以限制连接速率。
$nginx_version 当前运行的nginx版本号。
$query_string 与$args相同。
$remote_addr 客户端的IP地址。
$remote_port 客户端的端口。
$remote_user 已经经过Auth Basic Module验证的用户名。
$request_filename 当前连接请求的文件路径,由root或alias指令与URI请求生成。
$request_body 这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义。
$request_body_file 客户端请求主体信息的临时文件名。
$request_completion 请求完成
$request_method 这个变量是客户端请求的动作,通常为GET或POST。包括0.8.20及之前的版本中,这个变量总为main request中的动作,如果当前请求是一个子请求,并不使用这个当前请求的动作。
$request_uri 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI。
$schemeHTTP 方法(如http,https)。按需使用,例:
rewrite ^(.+)$ $scheme://example.com$1 redirect;
$server_addr 服务器地址,在完成一次系统调用后可以确定这个值,如果要绕开系统调用,则必须在listen中指定地址并且使用bind参数。
$server_name 服务器名称。
$server_port 请求到达服务器的端口号。
$server_protocol 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$uri 请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。
上面的这些是nginx 支持一些内置的变量,当然我们可以自定义,例如
$http_x_forwarded_for
这个变量就是自定义的,用来获得用了代理用户的真实IP。
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
set nginx as Windows system service
- Download the latest version of Windows Service Wrapper from here.
- Current version as of this writing is v1.17.
- Rename
winsw-1.xx-bin.exe
to something likenginxservice.exe
.- This is the name that will show up for the process that owns your nginx process.
- Place an XML file next to the exe with the same base name, e.g.
nginxservice.xml
. The contents should be like below (verify your nginx location).<service> <id>nginx</id> <name>nginx</name> <description>nginx</description> <executable>c:\nginx\nginx.exe</executable> <logpath>c:\nginx\</logpath> <logmode>roll</logmode> <depend></depend> <startargument></startargument> <stopargument>-s stop</stopargument> </service>
- You can find up to date details about the configuration on the WinSW README page.
- Run the command
nginxservice.exe install
.
5分钟完成nginx ssl配置
1.准备环境
2.生成证书
- openssl genrsa -des3 -out openssl.key 1024
- openssl req -new -x509 -key openssl.key -out openssl.crt -days 3650
- openssl rsa -in openssl.key -out openssl_nopass.key
具体操作
报错 :Unable to load config info from /usr/local/ssl/openssl.cnf
下载: openssl.conf 使用参数 -config openssl.conf 指定
证书文件
3.nginx 配置ssl
4.验证结果
完成!
linux与windows配置差不多.
可以使用openssl自认证证书,商用需要使用第三方机构认证
openssl req -new -key openssl.key -out openssl.csr
openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr
openssl x509 -req -days 3650 -in openssl.csr -CA openssl.crt -CAkey openssl.key -CAcreateserial -out openssl.crt
# 生成一个RSA密钥 $ openssl genrsa -des3 -out 33iq.key 1024 # 拷贝一个不需要输入密码的密钥文件 $ openssl rsa -in 33iq.key -out 33iq_nopass.key # 生成一个证书请求 $ openssl req -new -key 33iq.key -out 33iq.csr # 自己签发证书 $ openssl x509 -req -days 365 -in 33iq.csr -signkey 33iq.key -out 33iq.crt
Nginx + https + 免费SSL证书配置指南
请参考 Nginx Wiki http://wiki.nginx.org/NginxHttpSslModule
生成证书
$ openssl genrsa -des3 -out server.key 1024
$ openssl req -new -key server.key -out server.csr
$ cp server.key server.key.org
$ openssl rsa -in server.key.org -out server.key
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
编辑 nginx.conf
server_name YOUR_DOMAINNAME_HERE;
listen 443;
ssl on;
ssl_certificate /usr/local/nginx/conf/server.crt;
ssl_certificate_key /usr/local/nginx/conf/server.key;
}
OK, 完成了。但这样证书是不被信任的,自己玩玩还行,要被信任请看下面。
以下内容转载自
http://goo.gl/YOb5
http://goo.gl/Gftj
HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容 请看SSL。
它是一个URI scheme(抽象标识符体系),句法类同http:体系。用于安全的HTTP数据传输。https:URL表明它使用了HTTP,但HTTPS存在不同 于HTTP的默认端口及一个加密/身份验证层(在HTTP与TCP之间)。这个系统的最初研发由网景公司进行,提供了身份验证与加密通讯方法,现在它被广 泛用于万维网上安全敏感的通讯,例如交易支付方面。
1、自行颁发不受浏览器信任的SSL证书:
HTTPS的SSL证书可以自行颁发,Linux下的颁发步骤如下:
openssl req -new -key api.bz.key -out api.bz.csr
openssl rsa -in api.bz.key -out api.bz_nopass.key
nginx.conf 的SSL证书配置,使用 api.bz_nopass.key,在启动Nginx是无需输入SSL证书密码,而使用 api.bz.key 则需要输入密码:
server_name sms.api.bz;
listen 443;
index index.html index.htm index.php;
root /data0/htdocs/api.bz;
ssl on;
ssl_certificate api.bz.crt;
ssl_certificate_key api.bz_nopass.key;
......
}
自行颁发的SSL证书虽然能够实现加密传输功能,但得不到浏览器的信任,会出现以下提示:
2、受浏览器信任的StartSSL免费SSL证书:
跟VeriSign一样,StartSSL(网址:http://www.startssl.com,公司名:StartCom)也是一家CA机构,它的根证书很 久之前就被一些具有开源背景的浏览器支持(Firefox浏览器、谷歌Chrome浏览器、苹果Safari浏览器等)。
在今年9月 份,StartSSL竟然搞定了微软:微软在升级补丁中,更新了通过Windows根证书认证程序(Windows Root Certificate Program)的厂商清单,并首次将StartCom公司列入了该认证清单,这是微软首次将提供免费数字验证技术的厂商加入根证书认证列表中。现在,在 Windows 7或安装了升级补丁的Windows Vista或Windows XP操作系统中,系统会完全信任由StartCom这类免费数字认证机构认证的数字证书,从而使StartSSL也得到了IE浏览器的支持。
注册成为StartSSL(http://www.startssl.com) 用户,并通过邮件验证后,就可以申请免费的可信任的SSL证书了。步骤比较复杂,就不详细介绍了,申请向导的主要步骤如下:
startssl在9月微软补丁更新中被支持,使得startssl证书得到承认。
申请流程如下 startssl.com
我的环境是windows 7
请不要使用chrome和IE8,否则将无法生成KEY而无法申请成功。建议使用firefox。
此外我还遇上了点意外,我使用的移动的网络被误判为澳大利亚的IP并收到邮件要求解释。
我将APNIC的查询地址和查询结果发送给对方后帐户才申请成功。
control panel / sign-up 注意填写正确资料。收到邮件后复制验证码。然后可以生成一个证书,注意,startssl.com不是以用户名、密码来验证用户的,是用证书来验证用户 的。
所以生成证书后(火狐会导入证书),注意备份证书。丢失证书后只能重新注册。
登录后还要验证域名才能为该域名生成SSL证书,可以选择在whois里的邮箱、hostmaster@domain、 postmaster@domain或者webmaster@domain
然后就可以在Certificates wizard里就可以申请SSL证书了。
有效期一年。
======2009/11/26 update===========
Toolbox
把你申请时的private key拿去Decrypt private key,然后把生成的key保存起来。
然后在Retieve certificate里选择你申请的域名,就可以得到cer文件,保存起来,放到nginx的conf目录。
在nginx里这样配置
server_name security.meettea.com;
listen 443;
index index.html index.htm index.php;
root /data0/htdocs/security.meettea.com;
ssl on;
ssl_certificate security.cer;
ssl_certificate_key security.key;
}
nginx -s reload下如果没有任何提示,就说明配置成功了。
可惜firefox3.5测试,不信任class 1的证书。
==============2009/11/26 update===================
在startssl forum看到官方人员说firefox3.5不支持是因为没有配置好。经过配置解决了firefox 3.5不信任该证书的问题,ubuntu studio下测试通过。
步骤是:获取https://www.startssl.com/certs/ca.pem
获取https://www.startssl.com/certs/sub.class1.server.ca.pem
cat ca.pem sub.class1.server.ca.pem >> ca-certs.crt
cat ca-certs.crt >> security.cer
这是因为需要把startssl的根证书和sub class1的证书附上,因为是他们把证书颁发给你,firefox需要这张证书才认识你的证书。
此外opera测试没通过,这点,连startssl.com自己都没有被opera通过。
Incoming search terms:
在windows上以php-fpm的形式部署nginx服务器
In case anyone still needs this info. This is how I setup php-fpm for use with nginx on windows:
Download the .zip file from http://windows.php.net/download/. The .zip file should be VC9 which has the FastCGI file (php-cgi.exe). Don't download VC6, and don't download the .msi file because it requires that you have IIS setup already in order to install php-fpm. The zip file contains the php-cgi.exe which is what you need for php-fpm. I downloaded a slightly older version, php-5.3.10-Win32-VC9-x86.zip, from here http://windows.php.net/downloads/releases/archives/ because I wanted to match the version running on my production server.
Unzip the file, e.g. unzip into C:\php-5.3.10-Win32-VC9-x86
Edit the php.ini file as needed. What I did:
# nginx security setting
cgi.fix_pathinfo=0
extension_dir = "C:\php-5.3.10-Win32-VC9-x86\ext"
enable the following modules by uncommenting them:
extension=php_curl.dll
extension=php_mbstring.dll
extension=php_mysqli.dll
Create a .bat file somewhere, e.g. start-php-fcgi.bat in nginx directory or in the php directory:
@ECHO OFF
ECHO Starting PHP FastCGI...
set PATH=C:\php-5.3.10-Win32-VC9-x86;%PATH%
C:\php-5.3.10-Win32-VC9-x86\php-cgi.exe -b 127.0.0.1:9123 -c C:\php-5.3.10-Win32-VC9-x86\php.ini
Double click the .bat file to start php-fpm. A window will popup and stay open while its running. Its kind of annoying, but just haven't looked into setting it up as service yet.
In the php section of nginx.conf I reference the 127.0.0.1:9123:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Install Nginx/PHP-FPM on Fedora 17/16, CentOS/RHEL 6.3/5.8
I have once written Install nginx, PHP 5.3 and FastCGI on CentOS 5.5, Fedora 13, Red Hat RHEL 5.5/6 guide, but this guide is “updated” version, howto install Nginx with PHP 5.4.10 and PHP-FPM on Fedora 17/16/15/14, CentOS 6.3/6.2/6.1/6/5.8 and Red Hat (RHEL) 6.3/6.2/6.1/6/5.8. PHP-FPM is easier to use and configure than FastCGI and some tests says that PHP-FPM is even much more efficient than FastCGI.
Nginx (engine x) is a robust, small and high performance http server, reverse proxy server and also mail proxy server.
PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.
Install Nginx, PHP 5.4.10 and PHP-FPM on Fedora 17/16/15/14, CentOS 6.3/6.2/6.1/6/5.8, Red Hat (RHEL) 6.3/6.2/6.1/6/5.8
1. Change to root user.
sudo -i ## OR ## su - |
2. Install needed repositories
Fedora 17/16/15/14 Remi repository
## Remi Dependency on Fedora 17, 16 rpm -Uvh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noarch.rpm rpm -Uvh http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-stable.noarch.rpm ## Fedora 17 ## rpm -Uvh http://rpms.famillecollet.com/remi-release-17.rpm ## Fedora 16 ## rpm -Uvh http://rpms.famillecollet.com/remi-release-16.rpm ## Fedora 15 ## rpm -Uvh http://rpms.famillecollet.com/remi-release-15.rpm ## Fedora 14 ## rpm -Uvh http://rpms.famillecollet.com/remi-release-14.rpm |
CentOS 6.3/6.2/6.1/6/5.8 and Red Hat (RHEL) 6.3/6.2/6.1/6/5.8 Remi repository
## Remi Dependency on CentOS 6 and Red Hat (RHEL) 6 ## rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm ## CentOS 6 and Red Hat (RHEL) 6 ## rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm ## Remi Dependency on CentOS 5 and Red Hat (RHEL) 5 ## rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm ## CentOS 5 and Red Hat (RHEL) 5 ## rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm |
CentOS 6.3/6.2/6.1/6/5.8 and Red Hat (RHEL) 6.3/6.2/6.1/6/5.8 Nginx repository
Create file /etc/yum.repos.d/nginx.repo and add following content to repo file:
CentOS
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 |
Red Hat (RHEL)
[nginx] name=nginx repo baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/ gpgcheck=0 enabled=1 |
3. Install Nginx, PHP 5.4.10 and PHP-FPM
Fedora 17/16/15/14, CentOS 6.3/5.8 and Red Hat (RHEL) 6.3/5.8
yum --enablerepo=remi install nginx php php-fpm php-common |
CentOS 6.3/5.8 and Red Hat (RHEL) 6.3/5.8
yum --enablerepo=remi,remi-test install nginx php php-fpm php-common |
4. Install PHP 5.4.10 modules
- APC (php-pecl-apc) – APC caches and optimizes PHP intermediate code
- CLI (php-cli) – Command-line interface for PHP
- PEAR (php-pear) – PHP Extension and Application Repository framework
- PDO (php-pdo) – A database access abstraction module for PHP applications
- MySQL (php-mysql) – A module for PHP applications that use MySQL databases
- PostgreSQL (php-pgsql) – A PostgreSQL database module for PHP
- MongoDB (php-pecl-mongo) – PHP MongoDB database driver
- SQLite (php-sqlite) – Extension for the SQLite V2 Embeddable SQL Database Engine
- Memcache (php-pecl-memcache) – Extension to work with the Memcached caching daemon
- Memcached (php-pecl-memcached) – Extension to work with the Memcached caching daemon
- GD (php-gd) – A module for PHP applications for using the gd graphics library
- XML (php-xml) – A module for PHP applications which use XML
- MBString (php-mbstring) – A module for PHP applications which need multi-byte string handling
- MCrypt (php-mcrypt) – Standard PHP module provides mcrypt library support
Select what you need: APC, CLI, PEAR, PDO, MySQL, PostgreSQL, MongoDB, SQLite, Memcache, Memcached, GD, MBString, MCrypt, XML
More info about PHP APC from PHP APC Configuration and Usage Tips and Tricks
Fedora 17/16/15/14
yum --enablerepo=remi install php-pecl-apc php-cli php-pear php-pdo php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml |
CentOS 6.3/5.8 and Red Hat (RHEL) 6.3/5.8
yum --enablerepo=remi,remi-test install php-pecl-apc php-cli php-pear php-pdo php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml |
5. Stop httpd (Apache) server, Start Nginx HTTP server and PHP-FPM (FastCGI Process Manager)
Stop httpd (Apache)
/etc/init.d/httpd stop ## OR ## service httpd stop |
Start Nginx
/etc/init.d/nginx start ## use restart after update ## OR ## service nginx start ## use restart after update |
Start PHP-FPM
/etc/init.d/php-fpm start ## use restart after update ## OR ## service php-fpm start ## use restart after update |
6. Autostart Nginx and PHP-FPM on boot, also prevent httpd (Apache) autostarting on boot
Prevent httpd (Apache) autostarting on boot
chkconfig httpd off |
Autostart Nginx on boot
chkconfig --add nginx chkconfig --levels 235 nginx on |
Autostart PHP-FPM on boot
chkconfig --add php-fpm chkconfig --levels 235 php-fpm on |
7. Configure Nginx and PHP-FPM
Create directory layout for your site
I use here testsite.local site, but this could of course be your real site, like www.if-not-true-then-false.com.
## public_html directory and logs directory ## mkdir -p /srv/www/testsite.local/public_html mkdir /srv/www/testsite.local/logs chown -R apache:apache /srv/www/testsite.local |
Alternative setup to add logs under /var/log directory.
## public_html directory and logs directory ## mkdir -p /srv/www/testsite.local/public_html mkdir -p /var/log/nginx/testsite.local chown -R apache:apache /srv/www/testsite.local chown -R nginx:nginx /var/log/nginx |
Note: I use apache user and group here, because PHP-FPM runs as apache default (apache choosed to be able to access some dir as httpd). If you use some other user on your php-fpm conf then change this accordingly.
Create and configure virtual host directories under /etc/nginx
mkdir /etc/nginx/sites-available mkdir /etc/nginx/sites-enabled |
Add following lines to /etc/nginx/nginx.conf file, after “include /etc/nginx/conf.d/*.conf” line (inside http block).
## Load virtual host conf files. ## include /etc/nginx/sites-enabled/*; |
Create testsite.local virtual host file
Add following content to /etc/nginx/sites-available/testsite.local file. This is very basic virtual host config.
server { server_name testsite.local; access_log /srv/www/testsite.local/logs/access.log; error_log /srv/www/testsite.local/logs/error.log; root /srv/www/testsite.local/public_html; location / { index index.html index.htm index.php; } location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /srv/www/testsite.local/public_html$fastcgi_script_name; } } |
Link your virtual host to /etc/nginx/sites-enabled
cd /etc/nginx/sites-enabled/ ln -s /etc/nginx/sites-available/testsite.local service nginx restart |
Add your testsite.local “domain” to /etc/hosts file
/etc/hosts file Nginx on same machine
127.0.0.1 … row should look like example following:
127.0.0.1 localhost.localdomain localhost testsite.local |
And if you use another machine where you are running your Nginx server, then add it’s public IP as following:
10.0.2.19 wordpress |
Note: This is just very simple basic configuration, but if you want configure and optimize Nginx and PHP-FPM more then check following guide, Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks
8. Test your Nginx and PHP-FPM setup
Create /srv/www/testsite.local/public_html/index.php file with following content:
<?php phpinfo(); ?> |
Access following address, with your browser. http://testsite.local/
Enable Remote Connection to Nginx Web Server (Open Port 80 on Iptables Firewall)
1. Edit /etc/sysconfig/iptables file:
nano -w /etc/sysconfig/iptables |
2. Add following line before COMMIT:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT |
3. Restart Iptables Firewall:
service iptables restart ## OR ## /etc/init.d/iptables restart |
4. Test remote connection
Access following address, with your browser. http://your.domain/