Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AndreiTelteu/c03a09c971a4b7685f4b to your computer and use it in GitHub Desktop.
Save AndreiTelteu/c03a09c971a4b7685f4b to your computer and use it in GitHub Desktop.
The configuration needed for runing a Wordpress (standard/multisite) app over NginX PHP-FPM on OpenShift.
  1. Create an app/gear on OpenShift with this cartidge: OpenShift Nginx PHP-FPM Cartridge

  2. Open the file: <your-app-folder>/config/nginx.d/default.conf.erb, earse all the content, and paste this instead:

server {
  root              <%= ENV['OPENSHIFT_REPO_DIR'] %>/php;
  listen            <%= ENV['OPENSHIFT_PHP_IP'] %>:<%= ENV['OPENSHIFT_PHP_PORT'] %>;
  server_name       <%= ENV['OPENSHIFT_APP_DNS'] %>;
  index             index.php index.html index.htm;

  set_real_ip_from  <%= ENV['OPENSHIFT_PHP_IP'] %>;
  real_ip_header    X-Forwarded-For;

  # avoid caching by proxies
  add_header        Cache-Control private;

  # php file goes straigth to backend
  location ~ \.php$ {
      root           <%= ENV['OPENSHIFT_REPO_DIR'] %>/php;
      fastcgi_pass   <%= ENV['OPENSHIFT_PHP_FPM_IP'] %>:<%= ENV['OPENSHIFT_PHP_FPM_PORT'] %>;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
      include        fastcgi_params;
      include        openshift_params;
      # uncomment to export all environment variables to fastcgi
      #include        <%= ENV['OPENSHIFT_REPO_DIR'] %>/config/nginx.d/export_env;
  }

  # site root is served by /index.php
  location ~ ^/$ {
      root           <%= ENV['OPENSHIFT_REPO_DIR'] %>/php;
      fastcgi_pass   <%= ENV['OPENSHIFT_PHP_FPM_IP'] %>:<%= ENV['OPENSHIFT_PHP_FPM_PORT'] %>;
      fastcgi_param  SCRIPT_FILENAME   $document_root/index.php;
      include        fastcgi_params;
      include        openshift_params;
      # uncomment to export all environment variables to fastcgi
      #include        <%= ENV['OPENSHIFT_REPO_DIR'] %>/config/nginx.d/export_env;
  }

  # avoid unnecessary log
  location = /favicon.ico {
      access_log off;
      log_not_found off;
  }

  location = /robots.txt {
      access_log off;
      log_not_found off;
  }

  # SECURITY : Deny all attempts to access hidden files .abcde
  location ~ /\. {
      deny all;
  }

  # PERFORMANCE : Set expires headers for static files and turn off logging.
  location ~* ^.+\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
      access_log off; log_not_found off; expires 30d;
  }

  # SECURITY : Deny all attempts to access PHP Files in the uploads directory
  location ~* /(?:uploads|files)/.*\.php$ {
      deny all;
  }

  # This order might seem weird - this is attempted to match last if rules below fail.
  # http://wiki.nginx.org/HttpCoreModule
  location / {
      try_files $uri $uri/ /index.php?$args;
  }

#  # Start Multisite configuration - comment this lines if you don't want multisite
#  location ~ ^/[_0-9a-zA-Z-]+/files/(.*)$ {
#      try_files /wp-content/blogs.dir/$1/files/$2 /wp-includes/ms-files.php?file=$2 ;
#      access_log off; log_not_found off; expires max;
#  }
#  # Rewrite multisite '.../wp-.*' and '.../*.php'.
#  if (!-e $request_filename) {
#      rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
#      rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
#  }
#  # End Multisite configuration

  # Add trailing slash to */wp-admin requests.
  rewrite /wp-admin$ $scheme://$host$uri/ permanent;

}
  1. For a multisite Wordpress instalation, uncomment (by removing the # at the begining of each line) the lines betwen # Start Multisite configuration and # End Multisite configuration from the file <your-app-folder>/config/nginx.d/default.conf.erb.

  2. Now get the latest wordpress version, extract it, and put all the files from the extracted wordpress directory, in the folder <your-app-folder>/php/.

  3. Add MySQL to your OpenShift app.

  4. Open the <your-app-folder>/php/wp-config-sample.php file and replace the following strings, and then save the file as wp-config.php

  • replace 'database_name_here' with $_ENV['OPENSHIFT_APP_NAME']
  • replace 'username_here' with $_ENV['OPENSHIFT_MYSQL_DB_USERNAME']
  • replace 'password_here' with $_ENV['OPENSHIFT_MYSQL_DB_PASSWORD']
  • replace 'localhost' with $_ENV['OPENSHIFT_MYSQL_DB_HOST']
  1. Install Wordpress, configure multisite (if you want it), and done !

Related tutorials and documentation:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment