Nginx、PHP开发环境配置

安装Nginx、PHP

以Archlinux为例,安装命令如下:

sudo pacman -S nginx php php-fpm

配置PHP

PHP-fpm接收FastCGI请求有两种方式,一种是通过端口,第二种是通过unix socket文件进行通信。在/etc/php/php-fpm.conf可以配置:

listen = 127.0.0.1:9000
;listen = /run/php-fpm/php-fpm.sock

这里采用了监听端口的方式。除此之外,还要配置php本身的配置文件,以支持mysql等扩展。编辑/etc/php/php.ini,取消extension=mysql.soextension=mysqli.so两行的注释。

除此之外,还需查看php.ini文件中open_basedir中的值,如果PHP文件的路径在设置值之外,你需要把对应的父目录加入到open_basedir中。

配置Nginx

在给Nginx添加配置时,可以直接在/etc/nginx/nginx.conf中添加配置,也可以在这个配置文件中包含新的配置文件,这里采用后者。

编辑/etc/nginx/nginx.conf,在server段中添加如下内容:

location / { 
    include        conf.d/ci.conf;
}

conf.d/ci.conf为自定,然后编辑该文件,添加如下内容:

root           /path/to/php/code;   # php代码的根路径
try_files      $uri $uri/ /index.php;
index          index.php index.html index.htm;
fastcgi_pass   127.0.0.1:9000;
# fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;

其中fastcgi_pass配置由上一步的PHP-fpm配置决定。此外,最后两行可以换成include fastcgi.conf。附上两个fastcgi文件内容。

fastcgi_params:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

fastcgi.conf:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

可以看出来fastcgi.conffastcgi_params文件只多出第一行。

测试

在Nginx设置的PHP路径添加测试文件index.php,重启nginx服务和PHP-fpm服务,测试下你的PHP服务吧。

Alvie's Blog

Alvie's Blog

If you're going through hell, keep going

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin google pinterest medium