Wordpress on nginx, mac osx
eh?
I went about installing wordpress recently to set up a technical ‘blog’ dedicated to a small project we were working on at the time. I decided for speed just to use the tools we had on hand at the time. So this led to installing wordpress with nginx, homebrew, Mac OSX (Yosemite). Anyway, I’m not concerned with the why in this article, more-so just documenting the how (not covering nginx install here though).
I can tell you, it was not all that much fun and took longer than I thought it would. Certainly when I recall the process I went through a few years back installing wordpress onto an apache web server on linux (ubuntu, I believe). That was pretty smooth and seamless, this was not quite as nice.
Here’s how it went down, in case it helps anyone out there. As a by-product of this I also hope to get more familar with blogging in markdown, in particular the syntax for highighting commands & code blocks, which shamefully, I am not very fluent with.
The how
NB: It was a while ago I did this, and I put this together based on memory, my notes at the time, and my terminal command history. Its likely I might have missed something. I’ll redo it on a clean install and revise this if needed.
php
You’ll need brew for this. The following gets the php binary installed and sets it up for php-fpm (more on that later in the nginx section).
$ brew tap josegonzalez/homebrew-php
$ brew install --without-apache --with-fpm --with-mysql php55
mysql
From the terminal, use the following to install, start, configure where to store the db files, and finally secure the installation:
$ brew install mysql
$ mysql.server start
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/preferred/path/to/mysql/db/files --tmpdir=/tmp
$ mysql_secure_installation
I remember I had a bit of touble rebooting the installation at one point, so I copied the default config from the installation files back:
$ sudo cp $(brew --prefix mysql)/support-files/my-default.cnf /etc/my.cnf
This gets a base install up and running, you’ll also need to create a database for wordpress to use in mysql:
$ mysql -u root -p
$ mysql> CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'xxxxxxx';
$ mysql> CREATE DATABASE wordpress;
$ mysql> GRANT ALL ON wordpress.* TO 'wordpress'@'localhost';
Wordpress
Installs the wordpress web application files to your computer, using wp-cli. Use this util to download latest worpress, then configure it to connect to mysql wordpress db (with the details used before, mysql needs to be running here as well). Later we will need to tell nginx where these files are to serve it up:
$ brew install wp-cli
$ cd /preferred/path/to/wordpress/webapp
$ wp core download
$ wp core config --dbname=wordpress --dbuser=wordpress --dbpass=xxxxxx
$ wp core install --url=http://<preferred-url>/ --title='<preferred blog name>' --admin_user=admin --admin_password=<Preferred admin Password> --admin_email=<preferred admin email>
This leaves a wordpress install in your directory (from the 2nd step) that should be ready to host. If you mess up here, you can blow away the downloaded files and start over, but this also puts some config in the DB, e.g. the blog url, so it’s probably as well to drop the wordpress db as well and start over there too.
nginx
I don’t cover nginx install here - sorry, assuming you are familar with configuring nginx somewhat. Here’s the nginx configuration that worked for me:
server { listen 80; root /preferred/path/to/wordpress/webapp; index index.php index.html index.htm; client_max_body_size 2M; # <--for uploading large plugins server_name wordpress; location / { try_files $uri $uri/ /index.php?q=$uri&$args; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { include fastcgi.conf; fastcgi_pass 127.0.0.1:9000; # <-- this was the troublesome bit fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
Getting nginx to actually process the php and serve html was the part that caused a bit of hassle. If you are used to say, Apache, the above configuration is quite different. With Apache you have php processing compiled into your web-server, what differs here is that nginx is actually delegating the running of the php code off to a ‘downstream’ process, through the fastcgi_pass
configuration.
The idea here is for separation of concerns, so nginx is only responsible for delivering the processed html back to the client. This is where the php-fpm process comes in, it deals with the php itself. It needs to be running on the server:port
you have configured in your nginx server block.
The following will run php-fpm for you:
$ sudo php-fpm
It was tricky to troubleshoot this, but if you are getting a 200 OK response from nginx, but all you see is a blank screen in your browser, its likely you have mis-configured this stuff.
So…
As a follow on to this, to help manage and automate the running of this stack, I would look at the way a development environment with Mac OSX is described here, in particular the launchAgent management of nginx / php-fpm.
Reading this you can probably tell that I am actually not using wordpress to run this blog. Poole (based on Jekyll) is working out very nicely here - simple to install and use, not needing to worry about a database or hosting, very easy to create a markdown page using favorite text editor and push to git pages, which just goes ahead and deploys it to live.
Wordpress did have searching though, which I thought would be needed for a useful team blog to search why we made certain decisions or whatever. That’s probably available through Poole / Jekyll in some shape or form, will have a look into that.
Thanks!