Blog

Simple Nginx Setup with PHP

  1. Install Nginx
    1. ]# yum install nginx
  2. Install php-fpm
    1. ]# yum install php54-fpm
  3. Create a *.conf file for php-fpm
    1. ]# cd /etc/php-fpm.d/
    2. ]# cp www.conf your-domain.conf
  4. Setup the your-domain.conf file
    1. ]# vim your-domain.conf
    2. Type i to enter Edit mode.
    3. Change the second line [www] to [your-domain]
    4. Change line 12 to: listen = /var/run/php-fpm/your-domain.sock
    5. Change line 31 to: listen.owner = nginx
    6. Change line 32 to: listen.group = nginx
    7. Change line 39 to: user = nginx (do this if you’re running nginx as a proxy to Apache)
    8. Change line 41 to: group = nginx (do this if you’re running nginx as a proxy to Apache)
    9. Type Esc : wq [enter]
  5. Modify the default Nginx Config file
    1. ]# vim /etc/nginx/nginx.conf
    2. Change index index.html index.htm; to index index.html index.htm index.php;
    3. Type Esc: wq [enter]
  6. Setup your virtual host file for nginx
    1. ]# cd /etc/nginx/conf.d/
    2. ]# vim virtual.conf (This is a new file that will be created)
      1. Enter all your server {} info for the domain you’re adding.
        • To use unix sockets (recommended) type this into a location block
        • location ~[^/]\.php$ {
              fastcgi_pass     unix:/var/run/php5-fpm/your-domain.sock;
              include          fastcgi_params;
          }
      2. Type Esc: wq [enter]
  7. Modify the fastcgi_params file
    1. ]# vim /etc/nginx/fastcgi_params
    2. Add pastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name; to the list of parameters
    3. Type Esc: wq [enter]
  8. Start the servers
    1. ]# /etc/init.d/php-fpm restart
    2. ]# /etc/init.d/nginx restart
  9. Notice any errors, and make the appropriate adjustments.