用Nginx和php5-fpm部署wordpress

配置 Wordpress

下载及配置 Nginx 和 php5-fpm

    sudo apt-get update
    sudo apt-get install php5-gd libssh2-php nginx

配置 Nginx

  • 新建配置文件

      cd /etc/nginx/sites-available/
      sudo rm default
      sudo vi wordpress.conf
    
  • wordpress.conf中输入以下配置

      server {
          listen 80 default_server;
          listen [::]:80 default_server ipv6only=on;
    
          root /path/to/your/wordpress;
          index index.php index.html index.htm;
    
          server_name your_domain.com;
    
          location / {
                  # try_files $uri $uri/ =404;
                  try_files $uri $uri/ /index.php?q=$uri&$args;
          }
    
          error_page 404 /404.html;
    
          error_page 500 502 503 504 /50x.html;
          location = /50x.html {
                  root /usr/share/nginx/html;
          }
    
          location ~ \.php$ {
                  try_files $uri =404;
                  fastcgi_split_path_info ^(.+\.php)(/.+)$;
                  fastcgi_pass unix:/var/run/php5-fpm.sock;
                  fastcgi_index index.php;
                  include fastcgi_params;
          }
      }
    
  • 创建软连接

      sudo ln -s /etc/nginx/sites-available/wordpress.conf /etc/nginx/sites-enabled/wordpress.conf
    
  • 重新启动服务

      sudo service nginx restart
      sudo service php5-fpm retart
    

问题

http://your_domain.com浏览时, 出现 Bad gateway 502错误, 推测应该是没有获取到 sock. 在/var/run/文件夹下查看php5-fpm.sock, 此文件不存在. 查看资料, 做如下修改:

  • wordpress.conffastcgi_pass 从原来的 unix:/var/run/php5-fpm.sock改为127.0.0.1:9000
  • 修改/etc/php5/fpm/pool.d/中的www.conf, 去掉以下行的注释

      listen = 127.0.0.1:9000
      listen.owner = www-data
      listen.group = www-data
      listen.mode = 0660
    
  • 重新启动服务

      sudo service nginx restart
      sudo service php5-fpm retart
    
  • 如果之前在服务器上安装过apache, 需先要停止apache服务, 再重新启动上一步的服务

      sudo apa2ctl stop
    
Apr 7th, 2015

Comments