I had some interest in link shorteners and found YOURLS, a php package which allows you to set up your own link shortening service.
I used brew to install the required components – mysql and php54. For php54 you want to get php-fpm, mysql and no apache. Dont miss createing the symlink /usr/sbin/php-fpm to the new php-fpm that brew installs:
http://shanelogsdon.com/installing-nginx-percona-php-fpm-with-homebrew-on-mountain-lion
If you have trouble with the brew formula for php54, take a look and try this:
https://github.com/josegonzalez/homebrew-php#installation
And now you can start php-fpm:
php-fpm
When you setup mysql, you’ll have to start the mysql server, then add the user “root” with password “root”. Also, you have to create a blank database called “yourls”.
YOURLS assumes you are going to use apache but I decided to use nginx on my mac instead. I also used brew to install nginx.
Then installed the php sources for YOURLS:
git clone https://github.com/YOURLS/YOURLS.git
I created an nginx configuration in the YOURLS repo:
cd YOURLS mkdir nginx cp -r /usr/local/etc/nginx ./nginx cd nginx mkdir log
The nginx.conf:
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 3030; server_name localhost; access_log /Users/lkang/msrc/YOURLS/nginx/log/access.log; error_log /Users/lkang/msrc/YOURLS/nginx/log/error.log; root /Users/lkang/msrc/YOURLS; index index.php; location / { try_files $uri $uri/ /yourls-loader.php =404; if (!-e $request_filename) { rewrite ^/([0-9a-z-\+]+)/?$ /yourls-loader.php?id=$1 last; } } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /Users/lkang/msrc/YOURLS/nginx/fastcgi_params; } } }
I used these two pages (as well as the nginx docs) to setup nginx.conf:
http://packetcollision.com/2012/01/27/yourls-and-nginx-an-updated-config/ http://foolrulez.org/blog/2009/08/foolz-us-make-yourls-work-on-nginx/
And now you can start nginx:
nginx -c ~/YOURLS/nginx/nginx.conf
In the YOURLS/user directory, you’ll have to copy the config-sample.php to config.php and edit some values:
define( 'YOURLS_DB_USER', 'root' ); define( 'YOURLS_DB_PASS', 'root' ); define( 'YOURLS_DB_NAME', 'yourls' ); define( 'YOURLS_DB_HOST', 'localhost:3306' ); #3306 is the default mysql port define( 'YOURLS_SITE', 'http://localhost:3030' ); # I've chosen 3030 as the local port for YOURLS
At this point YOURLS should work. In your browser, enter “localhost:3030/admin” and you should see a link to “install”. Press it and it will create your database tables.
From the same url “localhost:3030/admin” you should be able to login with “username” / “password” and create shortened links. After creating a shortened link, “localhost:3030/<shortened_link> should redirect you to the site whose link you shortened.
Also, “localhost:3030/readme.html” should show you the same page as “http://yourls.org/”.
Troubleshooting
I had some issues getting YOURLS up and running. Configuration of nginx was probably the biggest set of issues to work through, followed by mysql installation and php-fpm installation.
In some cases reconfiguration/restart of nginx would not fix a config error until the browser cache was cleared.
After mysql is up, it’s good to check that the database is actually there.
mysql -u root -p mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | yourls | +--------------------+ 5 rows in set (0.00 sec) mysql> use yourls; Database changed mysql> show tables; +------------------+ | Tables_in_yourls | +------------------+ | yourls_log | | yourls_options | | yourls_url | +------------------+ 3 rows in set (0.00 sec) mysql>
YOURLS has a cool plugin interface which allows you to write functions that will trigger on certain “actions”, or modify intermediate values with “filters”. There are several already available at https://github.com/YOURLS/YOURLS/wiki/Plugin-List.
It turned that the plugin capability helped debugging quite a bit. I wrote a plugin which printed out “action” triggers so I could trace execution of the php scripts that were running. It’s pretty easy to find actions being triggered throughout the code. The output goes to the rendered page and to a logfile, which was useful when redirection wasnt working properly.