Thursday, August 16, 2012

Install Nginx php mysql phpmyadmin for Mac OS

Mac comes with PHP installed already, but instead of trying to get that version to work with everything I instead just used macports to install a clean version of PHP for me to configure to how I like things to run completely separate from the mac's default setup.

Install all packages


sudo port install php5 +fastcgi fcgi
 

Nginx Install

Enough foreplay, lets get to it, install nginx first.
  1. sudo port install nginx
Copy working configuration default into its own configuration
  1. sudo cp /opt/local/etc/nginx/nginx.conf.default /opt/local/etc/nginx/nginx.conf

If you want to launch nginx on system startup simply run the plist insalled by the port:
  1. sudo launchctl load -w /Library/LaunchDaemons/org.mackports.nginx.plist

TIP: start and stop nginx on demand from the command line, but don't do it yet! We have to configure it more first.

Start Nginx on demand:

  1. sudo nginx

Stop Nginx on demand:
  1. sudo nginx -s stop

Installing MySQL via MacPorts


I had a change of heart and decided to install mysql5 via MacPorts to keep it all under one package manager. If you've already installed MySQL using the package provided by dev.mysql.com then just skip ahead to Installing PHP with PHP-CGI. Otherwise keep reading.

Install MySQL via macports
  1. sudo port install mysql5-server
Once complete you'll need to start the mysql daemon and set a root password

  1. #start mysql daemon
  2. sudo mysqld_safe5 &
  1. sudo mysqladmin5 -u root password NEWPASSWORD

You will need to start mysql daemon whenever you wish to use it. I'd sugget making a shell command or an alias in your ~/.profile file.

  1. #~/.profile
  2. alias mysql='/opt/local/bin/mysql5'
  3. alias start_mysql='sudo mysqld_safe5 &'
  4. alias stop_mysql='mysqladmin5 --user=root --password=NEWPASSWORD shutdown'

Now you're all set with MySQL from MacPorts.

Installing PHP with PHP-CGI (FastCGI)

Like I said before, Lion comes with its own PHP install, but I much prefer to work with the package manager whenever possible, so I'm installing a fresh copy of PHP in /opt/local. Let's also install php5 with fastcgi (we need for nginx to talk to PHP) along with some of the nicer libraries we want access to for phpMyAdmin and mysql.

  1. sudo port install php5 +fastcgi fcgi php5-gd php5-mysql php5-mcrypt

Starting php-cgi:
  1. php-cgi -q -b 127.0.0.1:9000 &

Stopping PHP-CGI:
  1. sudo killall php-cgi

You'll need to start the php-cgi whenever you want nginx to talk to PHP via CGI daemon. I ended up writting a little bash to start this for me with a simple command and saved it to ~/bin/start_php_cgi.sh
  1. #!/bin/bash
  2. php-cgi -q -b 127.0.0.1:9000 &

Configure Nginx

Open up /opt/local/etc/nginx/nginx.conf in your favorite editor and configure it:


#user  nobody;
worker_processes  1;

error_log   /opt/local/etc/nginx/logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /opt/local/etc/nginx/logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /opt/local/etc/nginx/logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        access_log  /opt/local/etc/nginx/logs/host.access.log  main;

        location / {
            root   share/nginx/html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /opt/local/share/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /opt/local/share/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   share/nginx/html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   share/nginx/html;
    #        index  index.html index.htm;
    #    }
    #}

}

This is a basic nginx setup to run with PHP-CGI nicely. You'll have to add a little more to get it to work nicely with a CakePHP install, but we'll get to that soon enough. As you can see, I like to root things in /var/www, make sure this directory exists for you


  1. sudo mkdir /var/www
Lets write a testfile to see what we've got so far!
  1. sudo echo "" > /var/www/index.php


Start nginx, php-cgi and then navigate to localhost/index.php to see your hard work in all its glory.

Installing phpMyAdmin



Grab the latest version of phpMyAdmin from: http://www.phpmyadmin.net/home_page/downloads.php (version 3.4.7 when writing) and unpack it wherever you'd like (I put mine in /opt/local/phpMyAdmin).

Since I like to keep my route in /var/www as my default for nginx, I then created a symlink between where I installed phpMyAdmin and /var/www/phpMyAdmin

  1. sudo ln -s /opt/local/phpMyAdmin /var/www/phpMyAdmin


Now navigate to http://localhost/phpMyAdmin and you should be greeted with a nice phpMyAdmin screen.


Stop and start nginx:
  1. sudo nginx -s stop
  2. sudo nginx


Add your new server_name to your /private/etc/hosts file
  1. 127.0.0.1 localhost dev.testapp.devlocal


Now navigate to http://dev.testapp.devlocal on your machine and you should see your brand new baked CakePHP 2.0 app! Congratulations, happy baking!

I hope this tutorial helped, comments are appreciated.

No comments: