“The last time I used Ctl-c to get out of trouble was
on a PDP-11 at high school.”
As we move closer to the release of Loiter, we need to move away from our very modest development server to something more robust and scalable. We choose Amazon AWS EC2. While Amazon offers a service called LightSail — a preinstalled LAMP stack on what is essentially is a EC2 server — we went with the economy, flexibility and scalability of a vanilla-flavour EC2 server; T2-size to start with.
Another reason we needed a raw LAMP stack was to run a bespoke Apple Push Notifications Service (APNS) server. Having not been able to find a suitable outfit that offers an APNS backend for start-ups like us, we decided to write one ourselves – in C – I mean how hard can it be..!? Up to this point our APNS notifications were handed in the individual server-side PHP files, with each individual client-initiated PHP call opening and the closing a connection to the APNS gateway. While doing that a couple of hundred times a day on the sandbox gateway during development might be acceptable, doing this post-release on the production APNS gateway would very quickly be deemed a Denial of Service (DOS) attack and our IP address would be banned (as Apple explain here).
We choose to go with an Amazon Linux AMI instance, for no other reason than this flavour of LINUX seemed to be very well supported by Amazon with lots of online tutorials, guides and documentation. For instance these step-by-step instructions for installing a LAMP stack on the server are excellent.
PS : As a post script to this post, here is a short list of “tricks” that we learned along the way, many a result of doing our LINUX/C development on a Ubuntu box and then having to transfer it all to this(Red Hat-like) Amazon Linux AMI instance.
#1. Change permission on the key pair PEM file that is used to secure your SSH to your EC2 instance.
chmod 600 MyKeyPair.pem
#2. Set the Apache server to start automatically after the machine reboots:
sudo chkconfig httpd on
#3. Give Apache ownership of directories into which you want it to save upload files. We use this to upload Loiter user profile photo and location photos :
sudo chown apache myUploadDestinatioDirectory
#4. Our APNS server reads Loiter notifications from a MySQL database and sends them to the APNS gateway using SSL. The server is written in C and required installing both the OpenSSL and MySQL C API libraries. In the end this was straight forward:
sudo yum install libssl-dev
sudo yum install mysql-devel
Discovering where exactly the include files and object libraries had been installed was a challenge. Here is the gcc build command that worked:
gcc -Wall -std=c99 -o "apnsServer" "apnsServer.c" -L /usr/local/ssl/lib -lssl -lcrypto -I/usr/include/mysql55 -L /usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lrt -ldl
-std=c99 switch is there so that the C code can declare variables within functions, such as ‘for‘ loop initial declarations like this:
#5. We need our APNS Server to run in the background like a service (and not quit when the SSH terminal session closed. So
nohup with the final
& did the trick
nohup ./apnsServer > apnsServer.log 2>&1 &
> apnsServer.log directs any stdout to this log file and the
2>&1 does the same for any error messages.
The server process and its PID can be rediscovered in subsequent terminal sessions using
and can be terminated ungracefully using its PID number:
kill -9 <PID>
Currently we are working on a method for automating the monitoring of the APNS server service and leaning towards using monit. I’ll do a blog post on that is it turns out to be usefully interesting.