Fix Home Needs

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 12 March 2013

PHP output buffering on nginx

Posted on 19:48 by Unknown
Simple trick: if you're trying to use output buffering with PHP on nginx, and it should be working, but the whole response still comes down all at once to the client, try disabling gzip compression:

nginx.conf

gzip off;

That should do it! Sure, your "fast" pages might become a little slower, but your slow pages will appear to be loading instead of hanging.

(This works because, of course, nginx can't compress the output until it's all ready to go first!)
Read More
Posted in nginx, php | No comments

Thursday, 7 March 2013

Install nginx / PHP / MySQL on Mac OS X Mountain Lion with Homebrew

Posted on 08:35 by Unknown
Last time I wiped my Macbook Pro, I used Macports to install my web development environment. Doing it that way was really hard compared to using Homebrew. I now fully recommend Homebrew for all Mac package management needs. This is much easier than the Macports way. Trust me.

Here's how to install nginx, PHP, and MySQL using Homebrew on your Mac. It's actually quite easy. I did it on Mountain Lion (10.8) but it probably works for Lion too. I followed the tutorial here on EZUnix.org, but my version fixes some typos and explains some steps along the way.

Disclaimer: This is new to me; I'm not an expert. I didn't encounter any problems, and I did this from a clean install of OS X Mountain Lion. If you encounter any errors, I may or may not be able to help...



Estimated Time: 10-20 minutes



Got Command Line Developer Tools?


You're gonna need them. Finally, Apple provides the command line tools without needing to install the nearly-2GB-Xcode from the App Store. Go to their Developers Downloads page and download the latest "Command Line Tools" for your version of OS X, then install them.


Install Homebrew


In case you haven't already, install Homebrew by following the instructions at the bottom of this page.

Homebrew's most legit PHP "tap" (package source) is by Jose Gonzalez. Make sure to install it:

$ brew tap josegonzalez/homebrew-php

We also need a tap for a PHP 5.4 dependency, zlib:

$ brew tap homebrew/dupes

Install MySQL

$ brew install mysql

It'll chew on that for a few minutes, then we need to get it to run as our user account:

$ unset TMPDIR
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

I got a "Warning" during this operation, and while I don't think it's critical, I did this and things have seemed to work fine... if you got a warning during the last step, then you could do this:

$ sudo mv /usr/local/opt/mysql/my-new.cnf /usr/local/opt/mysql/my.cnf

Then, to launch MySQL at startup:

$ cp `brew --prefix mysql`/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Done! Next: the web server.


Install nginx

$ brew install nginx

Let that stew, then run these commands to have nginx run as root at startup (so we can listen on port 80, the default, instead of 8080 which is less convenient for development):

$ sudo cp `brew --prefix nginx`/homebrew.mxcl.nginx.plist /Library/LaunchDaemons/
$ sudo sed -i -e 's/`whoami`/root/g' `brew --prefix nginx`/homebrew.mxcl.nginx.plist


(Okay, to be honest, this didn't work for me to load nginx right away on start; I had to edit the /Library/LaunchDaemons/homebrew.mxcl.nginx.plist file and remove the two lines that specify the UserName key and value (one line specifies the key, the other the value). Then it worked.)

Then create a log file... this allows us to view server logs in the Mac Console, which is really convenient, but isn't required:

$ sudo mkdir /var/log/nginx/

(Don't forget to tell nginx to put the log file there in nginx.conf: error_log  /var/log/nginx/error.log;)

Done! Next up: PHP.


Install PHP

$ brew install --without-apache --with-fpm --with-mysql php54

Make sure to change "php54" to whatever version you want. At time of writing, PHP 5.4 is the latest stable, but PHP 5.5 is in alpha. I assume 5.5 would be php55, etc. Be sure to adjust any following commands with the proper version number.


Quick note: Yes, OS X does come with PHP pre-installed. But we don't want to use that. We need an install we can use with nginx and FastCGI Process Manager (fpm). Plus, we want the latest version, and I'm just not that into compiling from source.

To run php-fpm at startup:

$ sudo cp `brew --prefix php54`/homebrew-php.josegonzalez.php54.plist  /Library/LaunchAgents/
$ sudo launchctl load -w /Library/LaunchAgents/homebrew-php.josegonzalez.php54.plist

Done! Next up: configuration.



Finishing up

I want all php commands to be using the latest version, not the default PHP binary. So I use this little trick to create a symlink from the default PHP binary to the new one... I do this for both php and php-fpm. If you're confused about which versions are where, use the "whereis" command, like: "whereis php".


$ php-fpm -v
$ sudo mv /usr/sbin/php-fpm /usr/sbin/php-fpm.bak
$ sudo ln -s /usr/local/Cellar/php54/5.4.11/sbin/php-fpm /usr/sbin/php-fpm
$ php-fpm -v

Notice that the version went from 5.3 to 5.4 (in my case). Now for the php binary:

$ php -v
$ sudo mv /usr/bin/php /usr/bin/php.bak
$ sudo ln -s /usr/local/bin/php /usr/bin/php
$ php -v

I also added /usr/local/sbin to the PATH by adding that directory to the /etc/paths file, then restarting Terminal. You can see your current PATH by typing echo $PATH.

Important config files:

/usr/local/etc/nginx/nginx.conf
/usr/local/etc/php/5.4/php.ini
/usr/local/etc/nginx/fastcgi_params

You'll probably want to change these for your

The nice thing about Homebrew installations is that you generally don't need sudo to use or manage these services, since they're in /usr/local.

Alright. Well that did it for me. Enjoy your new dev environment!

You can stop nginx with nginx -s stop, and start it again with just nginx. You can also just reload the conf file with nginx -s reload.

I installed MySQL Workbench and was able to make a connection to the localhost MySQL server by adding a connection to host "localhost" with no password. The only thing I typed was that hostname and everything worked like a charm.

I did use my nginx.conf file from my previous install; you can view a sample conf file if you need it, in my other post about using Macports to do this (link at top of this post).
Read More
Posted in development, mac, mountain lion, osx, php, terminal | No comments

Saturday, 16 February 2013

PHP's PECL extension (for HttpRequest) worked on website but not command line (CLI)?

Posted on 16:44 by Unknown
I'm running PHP on my Mac under nginx and FastCGI... and that's great and fine. I used Macports to set that all up.

Well, Macs have PHP in another, default location: /usr/bin/php, not Macport's /opt/local/bin/php. When I installed the PECL extensions using Macports (after installing PHP), it installed them to the PHP at /opt/local/bin/php, not /usr/bin/php.

I have a PHP script that makes HTTP requests, and it may be a long process, so I spin up PHP on the command-line to do it in the background. Took me forever to figure out that that PHP binary was different from the one used by nginx when I loaded up my dev site.

The CLI version of PHP which I was running for this didn't have PECL installed. A quick, dirty way to fix this:

  1. sudo mv /usr/bin/php /usr/bin/php.bak
  2. sudo ls -s /opt/local/bin/php /usr/bin/php

That's right! Make php a symlink! Why? Well, I'm not sure. I tried simply doing the "mv" to kind of "hide" the binary from sh, but it was giving me "php - command not found" errors, even though /opt/local/bin is was in the PATH. Why didn't it look there? I have no idea. I'm not very proficient with unix stuff. But the symlink was enough to trick it and it's working great now.
Read More
Posted in cli, mac, pecl, php | No comments

Tuesday, 27 November 2012

How to remove a word from the dictionary (Chrome, Mac OS X)

Posted on 10:51 by Unknown
This trick works for any program on the Mac which uses the default dictionary (the same one that TextEdit, Mail, and the other built-in programs use). So when I accidentally added a misspelled word to the dictionary in Google Chrome, I wanted to remove it. Here's how:

  1. Open TextEdit
  2. Type the misspelled word
  3. Right-click it and click "Unlearn Spelling"
Done! Works across-the-board on Mac OS 10.5 (I'm pretty sure), and higher -- I am running Mountain Lion.
Read More
Posted in chrome, mac, osx | No comments

Monday, 1 October 2012

External hard drive backups while you sleep

Posted on 06:19 by Unknown
On most modern computers, there's an energy saver preference which will shut down your hard disks when the computer is idle or in "sleep mode." On a Mac, this is enabled by default in System Preferences --> Energy Saver.

I had been puzzled as to why CrashPlan, in my case, wasn't backing up remotely overnight... it did a grand total of about 20 files... If you want to enable your backups to run while you sleep, make sure your hard drives don't sleep.

Untick the checkbox in the Energy Saver window that says "Put hard disks to sleep when possible," and your backups should now continue to run as long as your Mac is alive and waking.
Read More
Posted in backup, mac, osx | No comments

Friday, 28 September 2012

Restoring a lost or moved iTunes library

Posted on 16:44 by Unknown

Background

When I installed an SSD in my Macbook Pro, I had to migrate all my files to new locations because the SSD is only 128 GB and my previous HDD was about 500 GB... and it was a little more than half full. So, where do I put them? I used another 500 GB external HDD (Firewire 800) as my Time Machine backup, so now I have 3 drives total. It took about a day, but I finally got everything shuffled around. This included moving my music to the external firewire drive, and thus, also my iTunes library.

The problem

At first, iTunes insisted on keeping the music library stored on my internal drive, in my home folder, in the Music directory. I told it "no, stop that!" several times but it persisted for some reason, refusing to accept my settings of putting the music library on an external drive. Finally, I nuked the silly empty library iTunes made when it started and couldn't find any existing one in my home folder, and closed iTunes.

The solution

iTunes has a couple files in the library folder: iTunes Library.xml and iTunes Music Library.xml. I don't know why there's two, but each one contained about the same things and were 30,000+ lines in length. There's also an important iTunes Library.itl file which is a binary representation of the library, which iTunes uses as its master copy of all your song titles, playlists, metadata, etc. Remove that, and iTunes will create a new empty one in its place. Remove the xml files, and iTunes will simply re-create them upon starting or closing (from the binary file).

I first tried opening iTunes with Option+Click which allows you to choose a new library (folder containing an ITL file) when it starts. This worked fine actually, except that none of the song files could be found: the library had moved to my external drive, but iTunes still expected them in my home folder.

So, I could locate them, one-by-one, or try something else.

The solution... for realz this time

After scouring Google for some time, I was finally led to this article in the Apple knowledge base, which describes how to restore an iTunes library. Summary:

First, since that KB article doesn't cover what to do when your library has moved, I had to figure this out. I opened both of those XML files and did a search-replace for the old paths with the new file paths. It's important to have iTunes closed in this process and not open it up until we're ready.

Then I dragged the ITL file (iTunes Library.itl) to the trash. And I dragged the two XML files to the desktop. Then I started iTunes. It created a new, empty library as expected. I changed the preferences so the library existed on my external hard drive in the folder I wanted. Then I did something very counter-intuitive, according to the KB article. I clicked File -> Library -> Import Playlist. Yeah, sure... except this isn't a playlist. It's a whole library.

However counter-intuitive this may seem, it worked for me. I chose iTunes Library.xml on my desktop and it restored the whole library like a charm. Now, whenever my drive is plugged in, iTunes just works. And when it's not plugged in, iTunes warns me and I can quit without messing anything up.
Read More
Posted in itunes, mac | No comments

Wednesday, 26 September 2012

Macbook Pro runs hot with SSD and TRIM Enabler

Posted on 09:47 by Unknown

The first problem


In a recent upgrade to my Macbook Pro (15-inch, early 2011), I swapped out the standard 2.5" HDD for a OCZ Vertex 4 SSD, which has a Sandforce controller Indilinx Everest 2 controller. I also did a clean install of OS X 10.8 (Mountain Lion) on the new drive.

I installed a hard drive performance benchmarking tool (Blackmagic Disk Speed Test) from the App Store and was pleased to see 500 MB+/sec read and write speeds, and my battery life on full charge went up to almost 6 hours! Then I encrypted the volume using FileVault 2 (Mac's built-in encryption utility; works very well). I rebooted, and did another speed test.

Write speeds were down to about 220 MB/sec and read speeds were just over 300 MB/sec. Performance had dropped drastically. Apparently, the FileVault encryption was interfering.

Background


Doing a little research turned up results that Sandforce-controlled SSDs, in Macs, may have trouble performing well when FileVault is doing its pass-thru (or "on-the-fly") encryption. Fortunately, this isn't a Sandforce-controlled drive. So why was this happening?

One possibility: SSDs should have a low-level command called TRIM which improves performance when writing to non-empty blocks on the drive. In a traditional HDD, writing to a non-empty sector has no (measurable) performance hit, but on an SSD, there's some technical reasons that writing to a non-empty block is slower than writing to an emtpy block. TRIM tidies things up after data is "deleted" so that write speeds are quick in that area later. Thus, enabling TRIM manually on Macs, whether there really is trouble or not, should theoretically help boost performance.


TRIM Enabler, the solution .... or is it?

I downloaded the nifty TRIM Enabler program and turned on the TRIM command with an easy switch-flip. After a reboot, I waited about an hour then ran a benchmark test. Performance was back up to 300-350 MB+/sec. Read speeds were even a little higher. I was very pleased. But a few days later, I noticed that my Macbook Pro was running hot and the battery life dropped from about 5.5 or 6 hours to 2 or 2.5, and the fan was spinning hard.

The CPU wasn't running out of control, and I couldn't narrow it down. I suspected it was SSD- or TRIM-related.

The fix to my Macbook Pro running hot / bad battery life

I simply disabled the TRIM command again in TRIM Enabler tool. Now, a day later, my MBP is running at normal temperatures, and battery life is back up to the high marks I was hoping for.

I suspect that the issue wasn't so much with a faulty SSD, but rather that there was some sort of initial lag in performance after encrypting. I don't know why I think it was Spotlight indexing all my files quickly, so the processor had to keep up -- but now a few days later  and without TRIM Enabler on, my benchmark tests show high read and write speeds, again up past the 350+MB/sec mark and my battery life is normal.

tl;dr Just be patient after encrypting your SSD with FileVault. Performance will probably go back up after a day, and the hit isn't too notable unless you're doing specific benchmark testing. TRIM Enabler probably isn't needed (with Vertex 4 SSDs).
Read More
Posted in osx, security, ssd | No comments
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Popular Posts

  • How to take FrontRunner from Provo to SLC airport
    I see this question a lot: how do I get from Provo or Orem to the SLC International Airport entirely by train (UTA FrontRunner/Trax)? Here I...
  • Behavior-driven testing in Go with GoConvey (BDD in "golang")
    First: the built-in Go testing tools Few things bring sweeter peace to the soul than making changes to Go code, then: $ go test ... PASS ok ...
  • Fix the Home and End keys on Mac OS X (Mountain Lion)
    If you use a keyboard that's not designed specifically for Macs, you probably are familiar with the annoying mapping of the Home and End...
  • Why yes, Go/Golang, I still want to read my CSV file!
    UPDATE: This appears to have been fixed and the fix  ships with   Go 1.2 . I like Go (1.1.1), but how disappointing that in order to read ...
  • Installing nginx / PHP / MySQL on Mac OS X Mountain Lion
    ** Update: See a quicker way to do this using Homebrew (this method uses Macports, and it's considerably more difficult). ** ... are yo...
  • Using Vagrant and cross-compiling Go (golang)
    This is mostly a memo-to-self about how to write Go code in my Mac environment, compile it there for a Linux environment, and run it in a pr...
  • Automatically make a Raspberry Pi with wifi support
    Okay, I love my Raspberry Pi, but setting it up just the way I want got so involved I was afraid I couldn't do it again if I had to. So ...
  • Install nginx / PHP / MySQL on Mac OS X Mountain Lion with Homebrew
    Last time I wiped my Macbook Pro, I used Macports to install my web development environment . Doing it that way was really hard compared to ...
  • Printing to BYU campus printers without extra software (Mac OS X)
    Macs come with a print server installed by default. This means you don't need to install any drivers or software to print to the BYU ca...
  • External hard drive backups while you sleep
    On most modern computers, there's an energy saver preference which will shut down your hard disks when the computer is idle or in "...

Categories

  • backup
  • bdd
  • byu
  • chrome
  • cli
  • command line
  • commute
  • compile
  • cross-compile
  • csv
  • development
  • dns
  • domain name
  • fastcgi
  • fcgi
  • go
  • golang
  • homebrew
  • inkscape
  • install
  • ip
  • ip address
  • ipaddress
  • itunes
  • javascript
  • keybinding
  • linux
  • mac
  • mountain lion
  • mysql
  • nginx
  • optimization
  • osx
  • parsing
  • pecl
  • php
  • printing
  • raspberry pi
  • security
  • ssd
  • terminal
  • testing
  • transportation
  • unit tests
  • vagrant

Blog Archive

  • ▼  2013 (16)
    • ▼  November (1)
      • How to take FrontRunner from Provo to SLC airport
    • ►  October (1)
    • ►  September (2)
    • ►  August (2)
    • ►  July (3)
    • ►  June (1)
    • ►  May (1)
    • ►  April (2)
    • ►  March (2)
    • ►  February (1)
  • ►  2012 (8)
    • ►  November (1)
    • ►  October (1)
    • ►  September (6)
Powered by Blogger.

About Me

Unknown
View my complete profile