Install and run a ZF2 application on CentOS with Vagrant
"Well it works on my machine..." is a common expression within development teams when new code is tested and doesn't work on other dev environments and configurations. With developers now writing code on Windows, Linux and Mac, the need for a consistent development environment is ever more important. Enter Vagrant.
Vagrant is an application for managing virtual machines using configuration scripts. In a nutshell, here's how it works.
- Configuration file with instructions on how your VM (virtual machine) should be configured (RAM, applications installed etc) is created.
- This config file is shared amongst the development team (usually via Git).
- Developers then use this config file to start their VM with the knowledge that their VM will be the same as everyone elses.
In this tutorial, we will be using Vagrant to get a base Centos 6.5 virtual machine installed and ready to install a LAMP environment to host our ZF2 application.
Why Centos?
Whereas Ubuntu has gained a lot more popularity on the server scene recently, Centos continues to dominate the web server space due to its impressive 10 year support cycle.
Guest? Host?
We will be doing work on both the computer that has the virtual machine and the virtual machine itself. To ensure that it's clear which system we will be doing work on, I will refer to the computer that has Vagrant installed as the "host" and the virtual machine as "guest".
Let's get started
First things first, you will need to download and install VirtualBox which will be our virtual machine software.
Next, download and install Vagrant. Once installed, you should reboot your host. To confirm its installation, open a terminal or command prompt and type:
vagrant -v
The command should return the version of Vagrant you are running.
We will be installing the ZF2 Skeleton app using Composer which you can download and install here. Another reboot of your host will ensure that the "composer" command is available.
With Vagrant and Composer ready, it's time to create the folder that will contain your ZF2 application. I'm on a Windows host and created my folder in C:\zf2. So we will now download the ZF2 Skeleton app using Composer. Open a new terminal/prompt window, browse to your folder and enter (make sure you keep the period on the end):
composer create-project -n -sdev zendframework/skeleton-application .
You should now have the ZF2 Skeleton application on your host. It's now time to get a Vagrant guest configured to run the application.
Starting Vagrant
The ZF2 you just downloaded will already come with its own Vagrantfile in the root of the folder. This will contain a script that will provision an Ubuntu server image for the application. As we will be using CentOS, please delete the Vagrant file and open a new terminal/command window in the app directory. Run these commands:
vagrant box add centos67 https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box vagrant init centos67
These commands will download the CentOS image and initialise it with Vagrant. Now open the newly created Vagrantfile and replace its contents with this:
Vagrant.configure(2) do |config| config.vm.box = "centos67" config.vm.network "private_network", ip: "192.168.33.10" config.vm.provider "virtualbox" do |v| v.memory = 1024 end # ZF2 config.vm.synced_folder "", "/srv/zf2" end
In the Vagrantfile we have told Virtualbox to boot the CentOS image with 1GB of RAM, its own IP address and to sync our ZF2 folder to the directory '/srv/zf2' on the image. We are now ready to boot our guest box. Run:
vagrant up
It will take a couple of minutes for the guest to boot up and load. Once loaded, you can SSH into the box using an SSH client such as Putty. The IP address to connect to should be the home address of your host (127.0.0.1) and the port should be 2222 (check the messages outputted from the vagrant up command if yours are different). The username to login is vagrant and the password is vagrant.
Once SSH'd into the guest, we can check if our application directory (C:\zf2) is visible.
cd /srv/zf2 ls -la
With the directory /srv/zf2 now pointing to our folder on the host, we can write our code using our favourite editor/IDE and then have the CentOS server execute our code as if the application was running on a production server.
Installing LAMP services
We are now ready to install the applications required to run our ZF2 application, these are:
- Apache
- MySQL
- PHP
But first, we need to remove the mail server that comes with the guest box as we don't want our application sending emails to unsuspecting users:
sudo service postfix stop sudo yum remove postfix
Now would also be a good time to update CentOS:
sudo yum update
MySQL
Every application almost always needs a database. Install MySQL using:
sudo yum -y install mysql mysql-server
We want the MySQL service to start on boot so we don't have to manually start it everytime we load our box:
sudo chkconfig --levels 235 mysqld on sudo service mysqld start
To configure some basic options for the MySQL service, run the installation:
mysql_secure_installation
Answer the questions like so:
- Enter current password for root: (blank)
- Set root password?: y
- New password: reverse
- Re-enter new password: reverse
- Remove anonymouse users?: y
- Disallow root login remotely?: n
- Remove test database and access to it?: y
- Reload privilege tables?: y
Apache
We need a web server to serve and process requests.
sudo yum -y install httpd
Like MySQL the Apache service should be started on boot:
sudo chkconfig --levels 235 httpd on sudo service httpd start
PHP
At the time of writing PHP version 5.5 is a recent stable release and modern enough for most frameworks and packages. The default software repositories however, point to the 5.3 versions. So lets add the repo for version 5.5:
sudo rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
Install it:
sudo yum install php55w
Install some common PHP moudules:
sudo yum install php55w-common php55w-pdo php55w-cli php55w-gd php55w-ldap php55w-mbstring php55w-mcrypt php55w-mysql php55w-opcache php55w-pdo php55w-pecl-geoip php55w-pecl-gearman php55w-pecl-memcache php55w-process php55w-recode php55w-snmp php55w-soap php55w-tidy php55w-xml php55w-xmlrpc php55w-bcmath
Restart Apache:
sudo service httpd restart
Setting some PHP configuration values
To ensure all PHP errors are sent to the browser amongst other things we need to make some PHP config changes. Open the php.ini file:
sudo vi /etc/php.ini
Find and set the following values (you can change them to suit your requirements):
display_errors = On date.timezone = "Europe/London" date.default_latitude = 53.5448 date.default_longitude = -2.6318 memory_limit = 512M error_reporting = E_ALL & ~E_NOTICE
Save your changes and exit the editor. Restart Apache.
sudo service httpd restart
Setting the path to the application
Before we configure the application in Apache, we need to create a symlink in the usual Apache /var/www directory to point to our application files currently located at /srv/zf2.
cd /var/www sudo ln -sfn /srv/zf2 zf2
The symlink we just created now means that the directory /var/www/zf2 actually points to /srv/zf2 where our application files are. With the symlink in place we can now create our VirtualHost configuration for Apache. Using your preferred editor create the following file, I have used vi as my editor:
sudo vi /etc/httpd/conf.d/zf2.conf
Paste this into the file and save it:
<VirtualHost *:80> ServerName zf2.vm ServerAlias zf2.vm DocumentRoot /var/www/zf2/public # This will enable the Rewrite capabilities RewriteEngine On EnableSendfile off <Directory /var/www/zf2/public> Options FollowSymLinks AllowOverride all Order allow,deny Allow from all </Directory> <Directory /> Options FollowSymLinks AllowOverride None </Directory> DirectoryIndex index.php index.html </VirtualHost>
Now restart Apache for the configuration changes to take effect:
sudo service httpd restart
Adding a DNS entry
The ZF2 application should now be accessible via Apache however, we now need to create a DNS entry on the host machine to so we can access it at http://zf2.vm
Editing the host file on your computer differs between Windows, Mac and Linux. Here is a guide that covers most operating systems. You should create a hosts entry that looks like this:
192.168.33.10 zf2.vm
Ready to go
If you have followed the instructions above you should now have a skeleton ZF2 application that is hosted on a CentOS server but can be developed on using your favourite IDE and operating system. You should no longer have to run your application on an operating system that doesn't match your production one therefore, eliminating any cross platform issues.