Blog

Conventions & File System Layout for Ideal WordPress-Nginx Setup

There is a famous proverb that goes like this:

A place for everything and everything in its place!

There is no denying to this and for this very reason, we are devoting our first chapter in this series to conventions only.

It is important to assign a place for all characters (read softwares’) we will encounter in this series. It will make things easier for us as we move ahead!

This chapter might be a little boring. But it will help you get a grip on all the conventions used. You will definitely thank me for this when you will end-up breaking something. Because in that panic moment, this knowledge will help you troubleshooting and quickly find things that you need to fix!

Conventions:

Codes/Commands:

Single-line of code/command:

This is an example of a single line code/command

Multiple line of codes/commands:

This is an example
of a multiline code-block
and/or multiple commands

Inline codes/commands

This is an example of inline command and this is an example of inline code.

File/Directory Paths:

  • I have tried to use absolute paths wherever needed. Rarely, you may find a relative path but then it will have a cd command before that or it will be part of multi-line code block.
  • Directory: /path/to/directory/ will always end with slash “/”.
  • File: /path/to/file won’t have slash “/” in the end.

If a path ends with slash “/” then its a directory.

File-System Layout:

Please note that following layout is for Ubuntu 12.04 LTS. Most of them are default locations for respective packages. Sticking with default will save us from some trouble. On non-Ubuntu/Debian OS, some locations may vary.

Nginx

Configuration Files:

  • /etc/nginx/ - all nginx related configuration will be in this folder
  • /etc/nginx/nginx.conf – THE (main) nginx configuration file
  • /etc/nginx/sites-available/ - nginx configuration for different sites will be available here
  • /etc/nginx/sites-enables/ - symlinks to nginx configuration files which are “active” [TODO- more info on how to enable/disable nginx]

Log Files:

  • /var/log/nginx/ – default log directory for nginx. We will use this for logs of all sites we will create.
  • /var/log/nginx/example.com.access.log - access log file for example.com
  • /var/log/nginx/example.com.error.log - error log file for example.com
  • /etc/logrotate.d/nginx  - this file control log-rotation policy for nginx related log files

PHP

Configuration Files:

  • /etc/php5/ - all php related configuration will be in this folder
  • /etc/php5/fpm/php.ini – THE (main) php configuration file
  • /etc/php5/fpm/php-fpm.conf – FPM related settings
  • /etc/php5/fpm/conf.d/www.conf – “www” i.e. default pool related settings

Log Files:

Note: You may not find following files by default. They were added in by us. 

  • /var/log/php5-fpm/ – php related to logs. you should check this if you feel your site is slow or broken
  • /var/log/php5-fpm/slow.log - this file will help you find slow php scripts
  • /var/log/php5-fpm/php.log - this file will help you find slow php scripts
  • /etc/logrotate.d/php5-fpm  - this file control how long php logs will be maintained

MySQL – Configuration & logs

Configuration Files:

  • /etc/mysql/my.cnf - this is mysql configuration file (not folder)

Log Files:

  • /var/log/mysql/mysql.log – mysql general/error logs
  • /var/log/mysql/mysql-slow.log – this file will help you find slow mysql queries
  • /etc/logrotate.d/mysql-server  – this file control how long php logs will be maintained

Website Structure

Following is the convention we will be using for WordPress as well as non-WordPress sites.

  • /var/www - all your websites will be here
  • /var/www/example.com - everything related to example.com will be inside this folder
  • /var/www/example.com/htdocs - this is web-root for example.com. Its like DocumentRoot in Aapche. You will put WordPress will here.
  • /var/www/example.com/logs - contains logs for example.com only.
  • /var/www/example.com/logs/access.log - contains access.logs for example.com only. If you want to use a tool like AWStat then this is the server-log file you will need. This is a symbolic link to /var/log/nginx/example.com.access.log file.
  • /var/www/example.com/logs/error.log - contains error.logs for example.com only. This will help you in debugging. It captures some PHP related error as well. This is a symbolic link to /var/log/nginx/example.com.error.log file.
  • /var/www/example.com/wp-content - in case you want to keep wp-content outside web-accessible folder. I will NOT cover this in this tutorial. Consider this is an exercise for yourself! ;-)

Notes:

Few notes about the above website-structure:

  1. Following structure does not take into account shared-hosting scenarios where generally all sites for a users are located under his home directory. Something like /home/bill/www or /home/bill/public_html
  2. Subdomains are treated like domains. They reside directly under /var/www with their own htdocs and logs folder. e.g. subdomain.example.com will use directory /var/www/subdomain.example.com

Notes about access.log & error.log files for websites:

You might have noticed that you can check access.log & error.log for a domain by 2 ways – either from logs folder under domain or nginx’s log folder. There are few reasons for this kind of setup:

  1. Keeping all log files places under /var/log/nginx location make things like logrotation, disk cleanup very easy. Also, things like checking logs for all sites hosted on your nginx server or for all subdomains for a top-level domain will be easy.
  2. Site-specific log folder can make debugging easy. Also, from security perspective you may want to create a user and give him access to a specific site only. In that case we can access logs for that site (if needed)

This is all for today. Tomorrow, we will jump to installation of nginx and other required software to run WordPress!

You can find the complete list of WordPress-Nginx tutorials here.

1 Comment…

 Share your views
  1. Very nice series!
    About the “[TODO- more info on how to enable/disable nginx]“, I have a tutorial on a2ensite-like commands for nginx: http://kamalnasser.net/nginx-apache-like-server-structure/

Leave a Comment

Connect with:

Your email address will not be published.

*