Sunday, January 13, 2013

Install php with nginx and php-fpm using homebrew osx

How I setup a osx php development environment with nginx and php-fpm using Homebrew

I didn't find any documentation specific to my situation of developing in osx and using the homebrew package manager. I hope to provide some useful configuration file settings, directory paths, comments, and a reference for myself. 

This is intended for performing development work on a mac, not running a live production system. 

I am running OSX 10.7. 5. 

Install Homebrew if you have not already. 

Add the php repository and install, adding compile flags for php-fpm and other tools you will be working with, such as mysql. For a list of options one may type 'brew options php54'

  $ brew tap josegonzalez/homebrew-php
  $ brew install php54 --with-fpm --with-mysql 

In another terminal start php-fpm

  $ php-fpm 

This will start php-fpm in the foreground, nice for debugging and seeing that everything is working. Later one can use php-fpm -D to daemonize it.  

Start nginx 

 $ sudo nginx

Test it by entering http://localhost into your web browser. A welcome page should appear.  The welcome page is located in /usr/local/Cellar/nginx/1.2.5/html/index.html

Edit the configs

I will use vi but any text editor is fine such as nano. 


The config file for php-fpm is called php-fpm.conf and is found in /usr/local/etc/php/5.4 along with php.ini . I left most of the settings alone, but made a backup copy of the file and set

  pid = run/

which tells it to create in /usr/local/var/run. This might be useful for a startup script, and nice to see something that says php-fpm is running and reading the config file. One can also set the port, error log, and a bunch of other things in php-fpm.conf. 

Set the PHP date.timezone to your time zone

  $ vi /usr/local/etc/php/5.4/php.ini
  date.timezone = Europe/Berlin


nginx.conf is located in /usr/local/etc/nginx. 

Make a backup of nginx.conf

Create a folder for logs 

  $ mkdir /usr/local/var/log/nginx

In nginx.conf update 

  error_log  /usr/local/var/log/nginx/error.log;

In the http section

  access_log  /usr/local/var/log/nginx/localhost.access.log  main;

In the server section

  listen       80;


  root   /Users/homedir/php-projects; 
  index  index.html index.php;


  location ~ \.php$ {
    root           /usr/local/var/www;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME document_root$fastcgi_script_name;
    include        fastcgi_params;

Complete nginx.conf

Reload nginx

  $ sudo nginx -s reload

Create a test index.php file in your php projects folder and test it out. 


Friday, January 11, 2013

MySQL Error 2013

I encountered 2013 during mysqldump and when selecting from a specific table.

A good tip I found was to check Uptime after the query

mysql> \s
Uptime: 7 min 4 sec

Which indicated the mysqld instance had restarted.

The best solution was for me was to check /var/log/mysql/mysql-error.log . It turned out advice from the log file saved the day:

InnoDB: It is also possible that your operating
InnoDB: system has corrupted its own file cache
InnoDB: and rebooting your computer removes the
InnoDB: error.

After rebooting the OS the error was gone.

Other advice I found online was:

1. Check wait_timeout is long enough

mysql> show variables

The default is 28800 seconds (8 hours). This was set to the default and thus not the problem.

2. Set max_allowed_packet to something larger

I opted for one gigabyte

mysql> set max_allowed_packet=1000 * 1024 * 1024;

This also had no effect. It would have been better if I had checked the error log immediately which makes good sense after an unexpected restart.

Monday, April 13, 2009

Checking default character set of MySQL database

For some reason this is just in the user comments. From the console

> show variables like "character_set_database";
> show variables like "collation_database";