从 Lighttpd 到 OpenResty
两年前,我写了 从Apache到Lighttpd。现在,是时候又发生改变了。
前段时间,我写了许多关于 OpenResty 的文章,并在公司内网服务器和自己的电脑上开始使用 OpenResty。
现在,我开始在外网部署 OpenResty。
编译和安装
OpenResty 的编译和安装就是老三套:
1tar xzvf ngx_openresty-VERSION.tar.gz
2cd ngx_openresty-VERSION/
3./configure
4make
5make install
默认情况下,OpenResy 会安装到 /usr/local/openresty 中,编译时会默认使用 luajit。
我一般会将其安装到 /opt/openresty 中,这就需要指定 --prefix 参数:
1./configuare --prefix=/opt/openresty
2make
3make install
启用 PHP-FPM
以 Ubuntu 14.04 LTS(trusty) 为例,首先安装 php-fpm 。
apt-get install php5-fpm
安装成功后该进程会自动启动,查看状态:
ps aux | grep fpm
输出:
root     10785  0.0  1.8 133624 18308 ?        Ss   14:13   0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
www-data 10787  0.0  1.0 133764 10956 ?        S    14:13   0:00 php-fpm: pool www
www-data 10788  0.0  0.9 133624  9280 ?        S    14:13   0:00 php-fpm: pool www
注意,php-fpm 是使用 www-data 用户权限运行的。
修改 /opt/openresty/nginx/conf/nginx.conf 配置文件,找到下面的内容取消注释:
# 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  html$fastcgi_script_name;
	include        fastcgi_params;
}
默认情况下,会报错:
2015/01/16 14:09:03 [error] 10614#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 59.175.48.91, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "yourname.com:8080"
这是因为,php-fpm 默认没有使用端口监听。查看 /etc/php5/fpm/pool.d/www.conf 可以找到下面的配置:
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /var/run/php5-fpm.sock
我们可以在这里将 php-fpm 修改成监听端口,也可以直接修改 nginx.conf 配置文件。在这里我采用第二个方案,修改 fastcgi_pass :
fastcgi_pass   unix:/var/run/php5-fpm.sock;
重载配置文件:
nginx -s reload
如果出现这样的错误,说明 nginx 进程没有权限访问 sock 。
2015/01/16 14:19:58 [crit] 11022#0: *6 connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 59.175.48.91, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "youname.com:8080"
我们注意到前面 php-fpm 是使用 www-data 权限运行的。要解决这个问题,将 nginx worker 进程的权限也改为 www-data 。编辑 nginx.conf :
将 user  nobody; 改为 user  www-data; 。
然后必须使用 nginx -s quit 退出主进程再重启,这个修改才能生效。
(全文完)
- 文章ID:2229
- 原文作者:zrong
- 原文链接:https://blog.zengrong.net/post/from-lighttpd-to-openresty/
- 版权声明:本作品采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可,非商业转载请注明出处(原文作者,原文链接),商业转载请联系作者获得授权。