Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 4 commits
  • 4 files changed
  • 0 comments
  • 1 contributor
2  _layouts/default.html
@@ -29,7 +29,7 @@
29 29
 	<body>
30 30
 		
31 31
 		<a href="https://github.com/lukearmstrong">
32  
-			<img style="position: absolute; top: 0; right: 0; border: 0;" src="//s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub">
  32
+			<img style="position: absolute; top: 0; right: 0; border: 0;" src="/images/fork-me-on-github.png" alt="Fork me on GitHub">
33 33
 		</a>
34 34
 
35 35
 		<div class="wrapper">
319  _posts/2012-09-27-lemp.markdown
Source Rendered
... ...
@@ -1,319 +0,0 @@
1  
----
2  
-layout: post
3  
-date: 2012-09-27 00:01
4  
-title: "LEMP: Linux, Nginx, MySQL and PHP"
5  
-description: Ubuntu 12.04 LTS
6  
-published: false
7  
----
8  
-
9  
-LEMP? Surely I mean LAMP? Nope...
10  
-
11  
-**```L```** - Linux  
12  
-**```E```** - Nginx (pronounced _Engine X_)  
13  
-**```M```** - MySQL  
14  
-**```P```** - PHP  
15  
-
16  
-I am going to explain how to install Nginx, MySQL and PHP on Ubuntu 12.04 LTS (Precise Pangolin).
17  
-
18  
-## Why not Apache?
19  
-
20  
-Well Apache isn't that efficient with resources (CPU/RAM), even when you don't have lots of traffic. We've had so many problems with it killing our servers that we have already migrated most of our sites to Nginx. Also, it is painfully slow when used with other languages I am interested in, like Ruby - I use Nginx and Unicorn for Ruby on Rails.
21  
-
22  
-
23  
-## Installing things...
24  
-
25  
-When installing anything, you generally have 4 choices...
26  
-
27  
-1. **Install the version in the Ubuntu repositories.**  
28  
-For the most part, this is what you should do. These are stable, are easy to install, and easy to keep updated. But sometimes you want or need the latest version, so you need to do something else.
29  
-
30  
-2. **Install from another repository, or find a PPA on Launchpad.**  
31  
-I generally only do this when it is either maintained by, or recommended by, the developers of the software. I have tried ones made by individuals, which was nice of them to do, but sometimes you can get unlucky and end up installing/updating to a broken package, or end up using a repository that stops being updated.
32  
-
33  
-3. **Compile from source.**  
34  
-Depending on the software, this can be quite simple, or it can be a pain in the arse to get it to compile (PHP). In some cases, it is not recommended at all (MySQL). It can teach you a lot about how something works, and what it requires to work, as once it has compiled you should go through the documentation to see what settings are in the various configuration files. To update, you have to manually download and compile the latest version, each time one is released.
35  
-
36  
-4. **Find a package (.deb) to install a specific version, if available.**  
37  
-Even though I recommend finding a PPA on Launchpad, which essentially installs a (.deb) package, it also adds a repository so that you can receive updates for it. With a .deb, you would have to manually download and install updates. Well in most cases. Google provide a .deb for Chrome, it installs a repository so that it is easy to keep updated, but I haven't come across anyone else doing this.
38  
-
39  
-
40  
-## Preparing /var/www
41  
-
42  
-Nearly every server setup I have seen keeps the web server's files in /var/www, so lets setup that folder now. Often your /var/www folder will need to be accessible by you, and other developers, so lets create a group to add these users to. Even if it will only be you, I would still recommend you do this.
43  
-
44  
-### umask
45  
-
46  
-If you are using Ubuntu 11.10 or 12.04, you can skip this step. The [default umask was changed from 0022 to 0002](https://wiki.ubuntu.com/OneiricOcelot/TechnicalOverview/Alpha2#Default_file_permissions) in Ubuntu 11.10 (Oneiric Ocelot)
47  
-
48  
-Running the ```umask``` command should show ```0002```
49  
-
50  
-{% highlight sh %}
51  
-umask
52  
-# 0002
53  
-{% endhighlight %}
54  
-
55  
-If it does, you **do not** need to do this next step. If it doesn't, you need to edit the ```/etc/profile``` file and add ```umask 0002``` at the end. Running this command will do this for you.
56  
-
57  
-{% highlight sh %}
58  
-sudo echo "umask 0002" >> /etc/profile
59  
-{% endhighlight %}
60  
-
61  
-You will need to log out and back in for the changes to take effect.
62  
-
63  
-
64  
-### Permissions
65  
-
66  
-Create a group, we will call it ```www-admin```.
67  
-
68  
-{% highlight sh %}
69  
-sudo groupadd www-admin
70  
-{% endhighlight %}
71  
-
72  
-Then add your user(s) to it.
73  
-
74  
-{% highlight sh %}
75  
-sudo usermod -a -G www-admin lukea
76  
-{% endhighlight %}
77  
-
78  
-Log out and back in and out for it to take effect. Now create the directory, owned by this group, and give the users of this group the permission to. 
79  
-
80  
-{% highlight sh %}
81  
-sudo mkdir /var/www
82  
-sudo chown -R root:www-admin /var/www
83  
-sudo chmod 2775 /var/www
84  
-{% endhighlight %}
85  
-
86  
-
87  
-
88  
-
89  
-
90  
-## Nginx
91  
-
92  
-If you want the latest stable version (v1.2.3), you don't have to compile from source, they provide official repositories for RHEL, CentOS, Debian and Ubuntu. Otherwise, if you prefer to use the version packaged by Ubuntu (v1.1.19), you can skip this step.
93  
-
94  
-### Add the official Nginx repository
95  
-
96  
-To use another repository, you need to download the key, and add it to the list of trusted keys.
97  
-
98  
-{% highlight sh %}
99  
-wget http://nginx.org/keys/nginx_signing.key
100  
-sudo apt-key add nginx_signing.key
101  
-{% endhighlight %}
102  
-
103  
-Now you can add the repository by creating this file:
104  
-
105  
-{% highlight sh %}
106  
-sudo vi /etc/apt/sources.list.d/nginx.list
107  
-{% endhighlight %}
108  
-
109  
-And adding this to it:
110  
-
111  
-{% highlight sh %}
112  
-deb http://nginx.org/packages/ubuntu/ precise nginx
113  
-deb-src http://nginx.org/packages/ubuntu/ precise nginx
114  
-{% endhighlight %}
115  
-
116  
-Now you will need to update the package manager for this new repository to be used.
117  
-
118  
-{% highlight sh %}
119  
-sudo apt-get update
120  
-{% endhighlight %}
121  
-
122  
-Update is used to resynchronize the package index files from their sources. The indexes of available packages are fetched from the location(s) specified in /etc/apt/sources.list
123  
-
124  
-
125  
-### Install Nginx
126  
-
127  
-Even if you skipped the step above, the command to install Nginx is the same.
128  
-
129  
-{% highlight sh %}
130  
-sudo apt-get install nginx
131  
-sudo service nginx restart
132  
-{% endhighlight %}
133  
-
134  
-### Test Nginx
135  
-
136  
-Let's see if it works.
137  
-
138  
-http://localhost/
139  
-(or whatever your Hostname/IP Address is)
140  
-
141  
-
142  
-
143  
-
144  
-
145  
-## MySQL
146  
-
147  
-The documentation for MySQL specifically states that attempting to compile from source will result in the death of kittens, so for the sake of your sanity, lets not do that.
148  
-
149  
-MySQL do provide .deb packages for Debian (v5.5.27), and I would assume they would work for Ubuntu, but there doesn't seem to be anything gained in having the latest version of MySQL as you would have to manually update it. Also, it's not clear what is included in the .deb, because there are 6 different packages for RHEL/Oracle, yet only 1 for Debian.
150  
-
151  
-So, to keep things simple, we will be installing the package provided by Ubuntu (v5.5.22).
152  
-
153  
-
154  
-### Install MySQL
155  
-
156  
-{% highlight sh %}
157  
-sudo apt-get install mysql-client mysql-server
158  
-{% endhighlight %}
159  
-
160  
-It will prompt you to set a password for the administrative "root" user, so set one, do not leave it blank.
161  
-
162  
-
163  
-
164  
-
165  
-
166  
-## PHP
167  
-
168  
-PHP is a strange one.
169  
-
170  
-At a conference I went to earlier this year, [PHP UK Conference 2012](http://2012.phpconference.co.uk/), [Rasmus Lerdorf](http://en.wikipedia.org/wiki/Rasmus_Lerdorf) was pissed off that web developers were using versions of PHP that were 6 years old. Yep, PHP 5.2 is 6 years old. 6 fucking years old, and even web hosts that are supposed to be good, like Rackspace, were using it [until very recently on their Cloud Sites hosting](http://feedback.rackspace.com/forums/71021-product-feedback/suggestions/997049-php-5-3-support-in-cloud-sites).
171  
-
172  
-The reason for this, quite frankly, is that PHP is a bastard to compile from source.
173  
-
174  
-The first time I tried, I remember spending several days trying to install PHP 5.2 on CentOS 5.0 (32-bit). It's not just the first time that is hard, when I tried the same steps on a 64-bit OS I had loads of fun getting the bugger to work.
175  
-
176  
-A simple solution for this, would be for the PHP developers to provide packages *of the latest version* for popular distributions. Unfortunately Zend seem more bothered with trying to sell LAMP stacks to IT managers.
177  
-
178  
-Ubuntu's official repositories come with PHP 5.3.10 - but PHP 5.4 has some nice features you could be using. PHP 5.3 was first released 3 years ago, you should *always* use the latest version of PHP.
179  
-
180  
-So today, we are going to install PHP 5.4.7, by compiling it from source, christ...
181  
-
182  
-
183  
-
184  
-### Dependency Hell
185  
-
186  
-PHP depends on a lot of other things (libraries) to work, you can install PHP on its own, without configuring any extensions, but it wouldn't do very much. It wouldn't even be able to connect to a database. Compiling it requires various odds and sods too, so hopefully this lot will do the trick.
187  
-
188  
-{% highlight sh %}
189  
-sudo apt-get install autoconf automake bison build-essential curl flex gettext libbz2-dev libc-client2007e-dev libc6-dev libcurl4-openssl-dev libicu-dev libkldap4 libldb-dev libmcrypt-dev libmysqlclient-dev libmysqld-dev libreadline6 libreadline6-dev libsqlite3-0 libsqlite3-dev libssl-dev libtool libxml2-dev libxslt-dev libyaml-dev ncurses-dev openssl re2c sendmail sqlite3 wget zlib1g zlib1g-dev imagemagick libmagickwand4 libmagickcore4 libgraphicsmagick1-dev libgraphicsmagick3 libvips-dev
190  
-{% endhighlight %}
191  
-
192  
-
193  
-### Download PHP
194  
-
195  
-Find your nearest mirror on the [PHP Downloads page](http://php.net/downloads.php), also check if a newer version of PHP has been released since I have written this article, and download that instead.
196  
-
197  
-This is the latest version, from my closest mirror, these are the commands I ran.
198  
-
199  
-{% highlight sh %}
200  
-wget http://php.net/get/php-5.4.7.tar.bz2/from/uk.php.net/mirror
201  
-mv mirror php-5.4.7.tar.bz2
202  
-tar -xvjf php-5.4.7.tar.bz2
203  
-{% endhighlight %}
204  
-
205  
-
206  
-### Compile PHP
207  
-
208  
-{% highlight sh %}
209  
-cd php-5.4.7
210  
-./configure --help
211  
-{% endhighlight %}
212  
-
213  
-This should list all of the options available to you. There are fucking shit loads of possibilities... this stems from a time before "it just works". Anyway, enough bitching, this is what I used....
214  
-
215  
-{% highlight sh %}
216  
-./configure --enable-bcmath --with-bz2 --enable-calendar --with-curl --enable-exif --enable-fpm --enable-ftp --with-gd --enable-gd-jis-conv --enable-gd-native-ttf --with-gettext --enable-intl --enable-mbstring --with-mcrypt --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --with-pdo-mysql=mysqlnd --with-pear --with-pspell --enable-soap --enable-sockets --with-tidy --with-xsl --enable-zip --with-zlib
217  
-{% endhighlight %}
218  
-
219  
-Now to actually compile it.
220  
-
221  
-{% highlight sh %}
222  
-make
223  
-{% endhighlight %}
224  
-
225  
- Running ```make``` is the point where it might fail because it doesn't like something you set in your ```configure``` command. If you get an error running ```make```, it usually means you don't have a library installed, when writing this article I spent a lot of time googling the error messages, and as soon as I got past one error, another would take it's place and the merry dance would continue.
226  
-
227  
-I'm hoping that by writing everything down like this, and testing it, it should also work for you.
228  
-
229  
-Once ```make``` has succeeded without throwing an error, you should do the PHP developers a favor by running their tests. This only takes a few minutes, and it will ask for your e-mail address to submit them, but please do it as it could improve PHP for everyone who uses it.
230  
-
231  
-{% highlight sh %}
232  
-make test
233  
-{% endhighlight %}
234  
-
235  
-Now to actually install it.
236  
-
237  
-{% highlight sh %}
238  
-sudo make install
239  
-{% endhighlight %}
240  
-
241  
-Once it is installed, you should run this command to make that hellish experience feel worth it.
242  
-
243  
-{% highlight sh %}
244  
-php -v
245  
-# PHP 5.4.7 (cli) (built: Sep 23 2012 19:44:48) 
246  
-# Copyright (c) 1997-2012 The PHP Group
247  
-# Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
248  
-{% endhighlight %}
249  
-
250  
-
251  
-
252  
-### It worked for me&trade;
253  
-
254  
-If it didn't work for you, you could install [PHP 5.4.6 from an unofficial PPA](https://launchpad.net/~ondrej/+archive/php5).
255  
-
256  
-{% highlight sh %}
257  
-sudo apt-get install python-software-properties
258  
-sudo add-apt-repository ppa:nginx/stable
259  
-sudo apt-get update
260  
-sudo apt-get install php5-cli php5-fpm php5-curl php5-gd php5-imagick php5-mcrypt php5-mysqlnd php5-sqlite php-pear
261  
-{% endhighlight %}
262  
-
263  
-
264  
-### Configure PHP
265  
-
266  
-When you download PHP, it comes with two example settings files. For some reason it doesn't appear to use either of them, nor is it clear where they should be put. So you need to copy it from the folder you extracted php to.
267  
-
268  
-{% highlight sh %}
269  
-sudo cp ~/php-5.4.7/php.ini-development /usr/local/lib/php.ini
270  
-{% endhighlight %}
271  
-
272  
-
273  
-### Configure PHP FPM
274  
-
275  
-We are going to be using php-fpm (FastCGI Process Manager), so we need to configure it.
276  
-
277  
-{% highlight sh %}
278  
-cd /usr/local/etc
279  
-sudo cp php-fpm.conf.default php-fpm.conf
280  
-sudo vi php-fpm.conf
281  
-{% endhighlight %}
282  
-
283  
-These are the settings you need to change.
284  
-
285  
-{% highlight sh %}
286  
-pid = run/php-fpm.pid
287  
-
288  
-listen = run/php-fpm.socket
289  
-
290  
-pm.start_servers = 20
291  
-{% endhighlight %}
292  
-
293  
-Now we need to setup a "service" to run PHP FPM on boot, and make it easy to restart.
294  
-
295  
-{% highlight sh %}
296  
-cp *** /etc/init.d/php-fpm
297  
-sudo chmod +x /etc/init.d/php-fpm
298  
-{% endhighlight %}
299  
-
300  
-
301  
-### Configure Nginx
302  
-
303  
-
304  
-
305  
-### Test PHP
306  
-
307  
-{% highlight php %}
308  
-<?php
309  
-
310  
-phpinfo();
311  
-{% endhighlight %}
312  
-
313  
-
314  
-
315  
-## phpMyAdmin
316  
-
317  
-Even though some things you are better off doing on the command line, such as importing/exporting databases, phpMyAdmin can be very useful for web developers. Also, it gives me an opportunity to demonstrate the entire LEMP stack is working together, and how to set up Nginx.
318  
-
319  
-
568  _posts/2012-10-06-lemp-linux-nginx-mysql-php.markdown
Source Rendered
... ...
@@ -0,0 +1,568 @@
  1
+---
  2
+layout: post
  3
+date: 2012-10-06 01:37
  4
+title: "LEMP: Linux, Nginx, MySQL and PHP"
  5
+description: "I am going to explain how to install Nginx, MySQL and PHP on Ubuntu 12.04 LTS (Precise Pangolin)."
  6
+---
  7
+
  8
+LEMP? Surely I mean LAMP? Nope...
  9
+
  10
+**`L`** - Linux  
  11
+**`E`** - Nginx (pronounced _Engine X_)  
  12
+**`M`** - MySQL  
  13
+**`P`** - PHP  
  14
+
  15
+I am going to explain how to install Nginx, MySQL and PHP on Ubuntu 12.04 LTS (Precise Pangolin).
  16
+
  17
+## Why not Apache?
  18
+
  19
+Well Apache isn't that efficient with resources (CPU/RAM), even when you don't have lots of traffic. We've had so many problems with it killing our servers that we have already migrated most of our sites to Nginx. Also, it is painfully slow when used with other languages I am interested in, like Ruby - I use Nginx and Unicorn for Ruby on Rails.
  20
+
  21
+
  22
+## Installing things...
  23
+
  24
+When installing anything, you generally have 4 choices...
  25
+
  26
+1. **Install the version in the Ubuntu repositories.**  
  27
+For the most part, this is what you should do. These are stable, are easy to install, and easy to keep updated. But sometimes you want or need the latest version, so you need to do something else.
  28
+
  29
+2. **Install from another repository, or find a PPA on Launchpad.**  
  30
+I generally only do this when it is either maintained by, or recommended by, the developers of the software. I have tried ones made by individuals, which was nice of them to do, but sometimes you can get unlucky and end up installing/updating to a broken package, or end up using a repository that stops being updated.
  31
+
  32
+3. **Compile from source.**  
  33
+Depending on the software, this can be quite simple, or it can be a pain in the arse to get it to compile (PHP). In some cases, it is not recommended at all (MySQL). It can teach you a lot about how something works, and what it requires to work, as once it has compiled you should go through the documentation to see what settings are in the various configuration files. Though to update the software, you have to manually download and compile the latest version again.
  34
+
  35
+4. **Find a package (.deb) to install a specific version, if available.**  
  36
+Even though I recommend finding a PPA on Launchpad, which essentially installs a (.deb) package, it also adds a repository so that you can receive updates for it. With a .deb, you would have to manually download and install updates. Well in most cases. Google provide a .deb for Chrome, it installs a repository so that it is easy to keep updated, but I haven't come across anyone else doing this.
  37
+
  38
+
  39
+## Preparing /var/www
  40
+
  41
+Nearly every server setup I have seen keeps the web server's files in /var/www, so lets setup that folder now. Often your /var/www folder will need to be accessible by you, and other developers, so lets create a group to add these users to. Even if it will only be you, I would still recommend you do this.
  42
+
  43
+### umask
  44
+
  45
+If you are using Ubuntu 11.10 or 12.04, you can skip this step. The [default umask was changed from 0022 to 0002](https://wiki.ubuntu.com/OneiricOcelot/TechnicalOverview/Alpha2#Default_file_permissions) in Ubuntu 11.10 (Oneiric Ocelot)
  46
+
  47
+Running the `umask` command should show `0002`
  48
+
  49
+{% highlight sh %}
  50
+umask
  51
+# 0002
  52
+{% endhighlight %}
  53
+
  54
+If it does, you **do not** need to do this next step. If it doesn't, you need to edit the `/etc/profile` file and add `umask 0002` at the end. Running this command will do this for you.
  55
+
  56
+{% highlight sh %}
  57
+sudo echo "umask 0002" >> /etc/profile
  58
+{% endhighlight %}
  59
+
  60
+You will need to log out and back in for the changes to take effect.
  61
+
  62
+
  63
+### Permissions
  64
+
  65
+Create a group, we will call it `www-root`.
  66
+
  67
+{% highlight sh %}
  68
+sudo groupadd www-root
  69
+{% endhighlight %}
  70
+
  71
+Then add your user(s) to it.
  72
+
  73
+{% highlight sh %}
  74
+sudo usermod -a -G www-root lukea
  75
+{% endhighlight %}
  76
+
  77
+Log out and back in and out for it to take effect.
  78
+
  79
+Now create the `/var/www` directory, and give the users of this `www-root` group the permission to create folders and write files within it, and for those permissions to always be set for the users of this group. 
  80
+
  81
+{% highlight sh %}
  82
+sudo mkdir /var/www
  83
+sudo chown -R root:www-root /var/www
  84
+sudo chmod 2775 /var/www
  85
+{% endhighlight %}
  86
+
  87
+
  88
+
  89
+
  90
+
  91
+## Nginx
  92
+
  93
+If you want the latest stable version (v1.2.3), you don't have to compile from source, they provide official repositories for RHEL, CentOS, Debian and Ubuntu. Otherwise, if you prefer to use the version packaged by Ubuntu (v1.1.19), you can skip this step.
  94
+
  95
+### Add the official Nginx repository
  96
+
  97
+To use another repository, you need to download the key, and add it to the list of trusted keys.
  98
+
  99
+{% highlight sh %}
  100
+wget http://nginx.org/keys/nginx_signing.key
  101
+sudo apt-key add nginx_signing.key
  102
+{% endhighlight %}
  103
+
  104
+Now you can add the repository by creating this file:
  105
+
  106
+{% highlight sh %}
  107
+sudo vi /etc/apt/sources.list.d/nginx.list
  108
+{% endhighlight %}
  109
+
  110
+And adding this to it:
  111
+
  112
+{% highlight sh %}
  113
+deb http://nginx.org/packages/ubuntu/ precise nginx
  114
+deb-src http://nginx.org/packages/ubuntu/ precise nginx
  115
+{% endhighlight %}
  116
+
  117
+Now you will need to update the package manager for this new repository to be used.
  118
+
  119
+{% highlight sh %}
  120
+sudo apt-get update
  121
+{% endhighlight %}
  122
+
  123
+Update is used to resynchronize the package index files from their sources. The indexes of available packages are fetched from the location(s) specified in /etc/apt/sources.list
  124
+
  125
+
  126
+### Install Nginx
  127
+
  128
+Even if you skipped the step above, the command to install Nginx is the same.
  129
+
  130
+{% highlight sh %}
  131
+sudo apt-get install nginx
  132
+sudo service nginx restart
  133
+{% endhighlight %}
  134
+
  135
+### Test Nginx
  136
+
  137
+Let's see if it works.
  138
+
  139
+[http://localhost/](http://localhost/)
  140
+
  141
+
  142
+
  143
+
  144
+
  145
+## MySQL
  146
+
  147
+The documentation for MySQL specifically states that attempting to compile from source will result in the death of kittens, so for the sake of your sanity, lets not do that.
  148
+
  149
+MySQL do provide `.deb` packages for Debian (v5.5.27), and I would assume they would work for Ubuntu, but there doesn't seem to be anything gained in having the latest version of MySQL as you would have to manually update it. Also, it's not clear what is included in the `.deb`, because there are 6 different packages for RHEL/Oracle, yet only 1 for Debian.
  150
+
  151
+So, to keep things simple, we will be installing the packages provided by Ubuntu (v5.5.22).
  152
+
  153
+
  154
+### Install MySQL
  155
+
  156
+{% highlight sh %}
  157
+sudo apt-get install mysql-client mysql-server
  158
+{% endhighlight %}
  159
+
  160
+It will prompt you to set a password for the administrative "root" user, so set one, do not leave it blank.
  161
+
  162
+
  163
+
  164
+
  165
+
  166
+## PHP
  167
+
  168
+PHP is a strange one.
  169
+
  170
+At a conference I went to earlier this year, [PHP UK Conference 2012](http://2012.phpconference.co.uk/), [Rasmus Lerdorf](http://en.wikipedia.org/wiki/Rasmus_Lerdorf) was pissed off that web developers were using versions of PHP that were 6 years old. Yep, PHP 5.2 is 6 years old. 6 fucking years old, and even web hosts that are supposed to be good, like Rackspace, were using it [until very recently on their Cloud Sites hosting](http://feedback.rackspace.com/forums/71021-product-feedback/suggestions/997049-php-5-3-support-in-cloud-sites).
  171
+
  172
+The reason for this, quite frankly, is that PHP is a bastard to compile from source.
  173
+
  174
+The first time I tried, I remember spending several days trying to install PHP 5.2 on CentOS 5.0 (32-bit). It's not just the first time that is hard, when I tried the same steps on a 64-bit OS I had loads of fun getting the bugger to work.
  175
+
  176
+A simple solution for this, would be for the PHP developers to provide packages *of the latest version* for popular distributions. Unfortunately Zend seem more bothered with trying to sell LAMP stacks to IT managers.
  177
+
  178
+Ubuntu's official repositories come with PHP 5.3.10 - but PHP 5.4 has some nice features you could be using. PHP 5.3 was first released 3 years ago, you should *always* use the latest version of PHP.
  179
+
  180
+So today, we are going to install PHP 5.4.7, by compiling it from source, christ...
  181
+
  182
+
  183
+
  184
+### Dependency Hell
  185
+
  186
+PHP depends on a lot of other things (libraries) to work, you can install PHP on its own, without configuring any extensions, but it wouldn't do very much. It wouldn't even be able to connect to a database. Compiling it requires various odds and sods too, so hopefully this lot will do the trick.
  187
+
  188
+{% highlight sh %}
  189
+sudo apt-get install autoconf automake bison build-essential curl flex gettext imagemagick libbz2-dev libc-client2007e-dev libc6-dev libcurl4-openssl-dev libicu-dev libgraphicsmagick1-dev libgraphicsmagick3 libkldap4 libldb-dev libmagickcore4 libmagickwand4 libmcrypt-dev libmysqlclient-dev libmysqld-dev libreadline6 libreadline6-dev libsqlite3-0 libsqlite3-dev libssl-dev libtool libvips-dev libxml2-dev libxslt-dev libyaml-dev ncurses-dev openssl re2c sendmail sqlite3 wget zlib1g zlib1g-dev
  190
+{% endhighlight %}
  191
+
  192
+
  193
+### Download PHP
  194
+
  195
+Find your nearest mirror on the [PHP Downloads page](http://php.net/downloads.php), but check if a newer version of PHP has been released since I have written this article, and download that instead.
  196
+
  197
+At the moment, PHP 5.4.7 is the latest version, so these are the commands I ran to download from my closest mirror.
  198
+
  199
+{% highlight sh %}
  200
+wget http://php.net/get/php-5.4.7.tar.bz2/from/uk.php.net/mirror
  201
+mv mirror php-5.4.7.tar.bz2
  202
+tar -xvjf php-5.4.7.tar.bz2
  203
+{% endhighlight %}
  204
+
  205
+
  206
+### Compile PHP
  207
+
  208
+{% highlight sh %}
  209
+cd php-5.4.7
  210
+./configure --help
  211
+{% endhighlight %}
  212
+
  213
+This should list all of the options available to you. There are so many they haven't even included them all in this list. I imagine this stems from a time before "it just works". Anyway, enough bitching, this is what I used....
  214
+
  215
+{% highlight sh %}
  216
+./configure --enable-bcmath --with-bz2 --enable-calendar --with-curl --enable-exif --enable-fpm --enable-ftp --with-gd --enable-gd-jis-conv --enable-gd-native-ttf --with-gettext --enable-intl --enable-mbstring --with-mcrypt --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --with-pdo-mysql=mysqlnd --with-pear --with-pspell --enable-soap --enable-sockets --with-tidy --with-xsl --enable-zip --with-zlib
  217
+{% endhighlight %}
  218
+
  219
+Now to actually compile it.
  220
+
  221
+{% highlight sh %}
  222
+make
  223
+{% endhighlight %}
  224
+
  225
+Running `make` is the point where it might fail because it doesn't like something you set in your `configure` command. If you get an error running `make`, it usually means you don't have a library installed, when writing this article I spent a lot of time googling the error messages, and as soon as I got past one error, another would take it's place and the merry dance would continue.
  226
+
  227
+I'm hoping that by writing everything down like this, and testing it, it should also work for you.
  228
+
  229
+Once `make` has succeeded without throwing an error, you should do the PHP developers a favour by running their tests. This only takes a few minutes, and it will only ask for your e-mail address to submit them, but please do it as it could improve PHP for everyone who uses it.
  230
+
  231
+{% highlight sh %}
  232
+make test
  233
+{% endhighlight %}
  234
+
  235
+Now to actually install it.
  236
+
  237
+{% highlight sh %}
  238
+sudo make install
  239
+{% endhighlight %}
  240
+
  241
+Once it is installed, you should run this command to make that hellish experience feel worth it.
  242
+
  243
+{% highlight sh %}
  244
+php -v
  245
+# PHP 5.4.7 (cli) (built: Oct  5 2012 23:40:18) 
  246
+# Copyright (c) 1997-2012 The PHP Group
  247
+# Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
  248
+{% endhighlight %}
  249
+
  250
+
  251
+
  252
+### It worked for me&trade;
  253
+
  254
+If it didn't work for you, you could install [PHP 5.4.6 from an unofficial PPA](https://launchpad.net/~ondrej/+archive/php5).
  255
+
  256
+{% highlight sh %}
  257
+sudo apt-get install python-software-properties
  258
+sudo add-apt-repository ppa:nginx/stable
  259
+sudo apt-get update
  260
+sudo apt-get install php5-cli php5-curl php5-fpm php5-gd php5-imagick php5-mcrypt php5-mysqlnd php-pear php5-sqlite
  261
+{% endhighlight %}
  262
+
  263
+Though if you do this, the rest of this article won't be referencing the same locations for configuration files, so you will need to use some common sense to find your configuration files. Generally they are all floating around in /etc/
  264
+
  265
+### Configure PHP
  266
+
  267
+When you download PHP, it comes with two example settings files. For some reason it doesn't appear to use either of them, nor is it clear where they should be put. Fortunately I have my crystal ball with me today, so I can tell you that this should do the trick...
  268
+
  269
+{% highlight sh %}
  270
+sudo cp ~/php-5.4.7/php.ini-development /usr/local/lib/php.ini
  271
+{% endhighlight %}
  272
+
  273
+
  274
+
  275
+### Configure PHP FPM
  276
+
  277
+We are going to be using php-fpm (FastCGI Process Manager), so we need to configure it.
  278
+
  279
+{% highlight sh %}
  280
+cd /usr/local/etc
  281
+sudo cp php-fpm.conf.default php-fpm.conf
  282
+sudo vi php-fpm.conf
  283
+{% endhighlight %}
  284
+
  285
+These are the settings you need to change. Find them, as they will already be in there. In each pair below, find the first line, and change it to the second line.
  286
+
  287
+{% highlight sh %}
  288
+;pid = run/php-fpm.pid
  289
+pid = run/php-fpm.pid
  290
+
  291
+;listen = run/php-fpm.socket
  292
+listen = run/php-fpm.socket
  293
+
  294
+;pm.start_servers = 20
  295
+pm.start_servers = 20
  296
+
  297
+;user = nobody
  298
+user = www-data
  299
+
  300
+;group = nobody
  301
+group = www-data
  302
+{% endhighlight %}
  303
+
  304
+Now we need to setup a "service" to run PHP FPM on boot, and make it easy to restart when we break things.
  305
+
  306
+{% highlight sh %}
  307
+sudo cp ~/php-5.4.7/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  308
+sudo chmod +x /etc/init.d/php-fpm
  309
+{% endhighlight %}
  310
+
  311
+Now you should be able to run php-fpm.
  312
+
  313
+{% highlight sh %}
  314
+sudo service start php-fpm
  315
+{% endhighlight %}
  316
+
  317
+
  318
+### Configure Nginx
  319
+
  320
+In /etc/nginx, there should be two folders; `sites-available` and `sites-enabled`. `sites-available` is where the configuration files for your _virtual hosts_ live, and `sites-enabled` contains [symbolic links](http://en.wikipedia.org/wiki/Symbolic_link) (aka. symlinks) to those configuration files; so if you want a site to be live, you create a symbolic link, if you want to take a site offline, you delete the symbolic link. That way, you never lose the configuration file. Note that any changes to the nginx configuration, including creating or deleting these symbolic links, requires a reload/restart of nginx.
  321
+
  322
+There is a configuration file called `default` in `/etc/nginx/sites-available`, lets keep a copy of it as it contains useful comments and configuration examples. It references files outside of `/var/www`, so I prefer to just set it aside, and create a new default _virtual host_.
  323
+
  324
+{% highlight sh %}
  325
+sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak
  326
+{% endhighlight %}
  327
+
  328
+In `/etc/nginx`, there is also a `conf.d` folder, that also contains a configuration file for a default host. Lets rename these too.
  329
+
  330
+{% highlight sh %}
  331
+sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
  332
+sudo mv /etc/nginx/conf.d/example-ssl.conf /etc/nginx/conf.d/example-ssl.conf.bak
  333
+{% endhighlight %}
  334
+
  335
+Lets setup our own default virtual host configuration.
  336
+
  337
+{% highlight sh %}
  338
+sudo vi /etc/nginx/sites-available/default
  339
+{% endhighlight %}
  340
+
  341
+Copy and paste the following into this file.
  342
+
  343
+{% highlight sh %}
  344
+server {
  345
+    listen 80;
  346
+    server_name localhost;
  347
+
  348
+    access_log /var/log/nginx/localhost-access.log;
  349
+    error_log  /var/log/nginx/localhost-error.log error;
  350
+
  351
+    root /var/www/default/;
  352
+    index index.php index.htm index.html;
  353
+
  354
+    location ~ \.php {
  355
+        fastcgi_index index.php;
  356
+		fastcgi_pass unix:/usr/local/var/run/php-fpm.socket;
  357
+
  358
+        include fastcgi_params;
  359
+        fastcgi_split_path_info ^(.+\.php)(/.+)$;
  360
+        fastcgi_param PATH_INFO $fastcgi_path_info;
  361
+        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  362
+        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  363
+    }
  364
+}
  365
+{% endhighlight %}
  366
+
  367
+The symlink for this virtual host already exists, but if it didn't, we would need to run these commands.
  368
+
  369
+{% highlight sh %}
  370
+cd /var/nginx/sites-enabled/
  371
+sudo ln -s ../sites-available/default
  372
+{% endhighlight %}
  373
+
  374
+As we have made a configuration change to nginx, it would be wise to test our changes. If you make a mistake, and then restart nginx, it will go down and stay down until you figure out what the problem is and fix it.
  375
+
  376
+{% highlight sh %}
  377
+sudo service nginx configtest 
  378
+# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  379
+# nginx: configuration file /etc/nginx/nginx.conf test is successful
  380
+{% endhighlight %}
  381
+
  382
+We are not yet ready to restart yet. First, we need to create a folder for our virtual host in /var/www.
  383
+
  384
+{% highlight sh %}
  385
+cd /var/www
  386
+mkdir default
  387
+{% endhighlight %}
  388
+
  389
+Now lets create a page, just so we can test our virtual host is working when we restart nginx.
  390
+
  391
+{% highlight sh %}
  392
+cd default
  393
+echo "Hello World" > index.htm
  394
+{% endhighlight %}
  395
+
  396
+Now to restart nginx.
  397
+
  398
+{% highlight sh %}
  399
+sudo service restart nginx
  400
+{% endhighlight %}
  401
+
  402
+If you go to [http://localhost/](http://localhost/) in your browser, then you should see "Hello World".
  403
+
  404
+### Test PHP
  405
+
  406
+As we have created our default virtual host, which contained some configuration for PHP FPM, we could create a php file to be run by PHP. It's quite useful to test PHP like this, as even at this point you can still go back and re-compile PHP with different options.
  407
+
  408
+{% highlight php %}
  409
+vi /var/www/localhost/info.php
  410
+{% endhighlight %}
  411
+
  412
+Copy and paste this into this file.
  413
+
  414
+{% highlight php %}
  415
+<?php
  416
+
  417
+phpinfo();
  418
+{% endhighlight %}
  419
+
  420
+Now if you visit [http://localhost/info.php](http://localhost/info.php) you should see lots of information about your PHP installation. If you scroll down to the section titled "date", you should see this error message:
  421
+
  422
+{% highlight php %}
  423
+Warning: phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /var/www/default/info.php on line 3
  424
+{% endhighlight %}
  425
+
  426
+So we need to edit our `php.ini` file to define this setting, otherwise this error message could be shown every time you try to use the `date()` function.
  427
+
  428
+{% highlight php %}
  429
+sudo vi /usr/local/lib/php.ini
  430
+{% endhighlight %}
  431
+
  432
+Again, find the first line, and replace it with the second line:
  433
+
  434
+{% highlight php %}
  435
+;date.timezone =
  436
+date.timezone = Europe/London 
  437
+{% endhighlight %}
  438
+
  439
+As we have made a configuration change, we need to restart php-fpm.
  440
+
  441
+{% highlight php %}
  442
+sudo service php-fpm restart
  443
+{% endhighlight %}
  444
+
  445
+
  446
+
  447
+## phpMyAdmin
  448
+
  449
+Even though some things you are better off doing on the command line, such as importing/exporting databases, phpMyAdmin can be very useful for web developers. Also, it gives me an opportunity to demonstrate the entire LEMP stack is working together, and how to set up another _virtual host_ for Nginx.
  450
+
  451
+
  452
+### Download phpMyAdmin
  453
+
  454
+At the time of writing, v3.5.2.2 was the latest stable version. But please check the [phpMyAdmin website](http://www.phpmyadmin.net/) to download the latest version. Even though I am english, other people may not be, so be nice and download the all-languages version.
  455
+
  456
+{% highlight sh %}
  457
+cd
  458
+wget http://downloads.sourceforge.net/project/phpmyadmin/phpMyAdmin/3.5.2.2/phpMyAdmin-3.5.2.2-all-languages.tar.bz2
  459
+tar -xvjf phpMyAdmin-3.5.2.2-all-languages.tar.bz2
  460
+cp -a phpMyAdmin-3.5.2.2-all-languages /var/www/phpmyadmin/
  461
+{% endhighlight %}
  462
+
  463
+
  464
+### Setup Nginx Virtual Host
  465
+
  466
+Create a config file.
  467
+
  468
+{% highlight sh %}
  469
+sudo vi /etc/nginx/sites-available/phpmyadmin.dev
  470
+{% endhighlight %}
  471
+
  472
+Copy and paste this into this file. You will notice this is very similar to our default config, so compare it to the default config to understand what has changed. Understanding this will help you when you come to add your own _virtual hosts_.
  473
+
  474
+{% highlight sh %}
  475
+server {
  476
+    listen 80;
  477
+    server_name .phpmyadmin.dev;
  478
+
  479
+    access_log /var/log/nginx/phpmyadmin.dev-access.log;
  480
+    error_log  /var/log/nginx/phpmyadmin.dev-error.log error;
  481
+
  482
+    root /var/www/phpmyadmin.dev/;
  483
+    index index.php;
  484
+
  485
+    location ~ \.php {
  486
+        fastcgi_index index.php;
  487
+		fastcgi_pass unix:/usr/local/var/run/php-fpm.socket;
  488
+
  489
+        include fastcgi_params;
  490
+        fastcgi_split_path_info ^(.+\.php)(/.+)$;
  491
+        fastcgi_param PATH_INFO $fastcgi_path_info;
  492
+        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  493
+        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  494
+    }
  495
+}
  496
+{% endhighlight %}
  497
+
  498
+For this new virtual host to be enabled, we need to create a symlink in the sites-enabled folder.
  499
+
  500
+{% highlight sh %}
  501
+cd /etc/nginx/sites-enabled
  502
+sudo ln -s ../sites-available/phpmyadmin.dev
  503
+{% endhighlight %}
  504
+
  505
+Again, make sure you test your nginx config does not have any syntax errors.
  506
+
  507
+{% highlight sh %}
  508
+sudo service nginx configtest
  509
+# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  510
+# nginx: configuration file /etc/nginx/nginx.conf test is successful
  511
+{% endhighlight %}
  512
+
  513
+Obviously the phpmyadmin.dev domain doesn't exist, so we have to add it to our hosts file.
  514
+
  515
+{% highlight sh %}
  516
+sudo sh -c "echo '127.0.0.1\tphpmyadmin.dev' >> /etc/hosts"
  517
+{% endhighlight %}
  518
+
  519
+Now if we restart the server...
  520
+
  521
+{% highlight sh %}
  522
+sudo service nginx restart
  523
+{% endhighlight %}
  524
+
  525
+...we should see phpMyAdmin in our browser if we go to [http://phpmyadmin.dev/](http://phpmyadmin.dev/)
  526
+
  527
+A login screen should show up, but unfortunately it won't work, the fucker just won't let you log in.
  528
+
  529
+{% highlight sh %}
  530
+#2002 - No such file or directory
  531
+The server is not responding (or the local server's socket is not correctly configured). 
  532
+{% endhighlight %}
  533
+
  534
+To log in we need to edit our php.ini file.
  535
+
  536
+{% highlight sh %}
  537
+sudo vi /usr/local/lib/php.ini
  538
+{% endhighlight %}
  539
+
  540
+These are the settings you need to change. Find them, as they will already be in there. In each pair below, find the first line, and change it to the second line.
  541
+
  542
+{% highlight sh %}
  543
+pdo_mysql.default_socket=
  544
+pdo_mysql.default_socket = /var/run/mysqld/mysqld.sock
  545
+
  546
+mysql.default_socket =
  547
+mysql.default_socket = /var/run/mysqld/mysqld.sock
  548
+
  549
+mysqli.default_socket =
  550
+mysqli.default_socket = /var/run/mysqld/mysqld.sock
  551
+{% endhighlight %}
  552
+
  553
+Now we need to restart PHP as we have changed the configuration.
  554
+
  555
+{% highlight sh %}
  556
+sudo service php-fpm restart
  557
+{% endhighlight %}
  558
+
  559
+Now phpMyAdmin should let you login!
  560
+
  561
+There are lots of configuration options for phpMyAdmin, I would suggest locking it down as much as possible, you can lock it down to your IP, you can force SSL, you can prevent logging in with root, you can setup a whitelist of users that can login. All of which probably belong in an article by itself.
  562
+
  563
+At the very least, remove the test user and the test database.
  564
+
  565
+
  566
+## Future Reading.
  567
+
  568
+Obviously this article is just intended to get you started, I would definitely recommend that you look into all of the things covered in this article to try and gain a better understanding.
BIN  images/fork-me-on-github.png

No commit comments for this range

Something went wrong with that request. Please try again.