Configure Apache and Mysql the easy way

Posted July 18th, 2011 in Php by Florentin

This is a simple way to setup a LAMP environment after you have just installed a Linux (i.e. Ubuntu)
or if you’d like to replicate the setup of another machine (i.e. duplicate your desktop’s environment to your laptop)
The key is to keep all your customizations in separate files and directories.
We start by creating a new folder where all our custom settings will live.

1. prerequisites

Create a directory named "lamp" with the following structure:

├── apache
│   └── elmo.conf
├── install.sh
├── mysql
│   └── elmo.cnf
├── php
│   └── elmo.ini
└── virtualhosts
├── 000-localhost.conf
└── phpmyadmin.conf

2. configure Apache 2

The default apache2.conf (/etc/apache2/apache2.conf) is setup to include every settings file it can find inside the "conf.d" directory (/etc/apache2/conf.d)
so make sure this line is included:
Include conf.d/

We will sym link our Apache customizations into this directory:
sudo ln -s /lamp/apache/elmo.conf /etc/apache2/conf.d/

You may now add your own Apache settings into the elmo.conf file.
The files under "conf.d" are loaded in order of their file names i.e. aaa.conf loads before bbb.conf
All the files inside "conf.d" are loaded at the end of the apache2.conf so your custom settings may override the defaults.

After the settings are in place, you may want to automatically configure some virtual hosts.
Optionally, you could delete the default/old virtual hosts:
rm /etc/apache2/sites-enabled/*

Now new hosts are ready to be enabled:
ln -s /lamp/virtualhosts/000-localhost.conf /etc/apache2/sites-enabled/

3. configure Mysql 5

Mysql does the same configuration loading as Apache.
The last my.cnf directive is "!includedir /etc/mysql/conf.d/" which loads every settings file from the "conf.d" directory.
Let’s do the symbolic linking:
sudo ln -s /lamp/mysql/elmo.cnf /etc/mysql/conf.d/

Place all your Mysql customizations inside the elmo.cnf file.

4. configure Php / Zend Server CE 5.2

My Php environment is provided by Zend Server Community Edition.
The php.ini file located under /usr/local/zend/etc/ uses this directive to include the settings file from inside "conf.d":
[Zend]
zend.ini_scandir=conf.d

Obviously, we will create a symbolic link to our custom Php settings:
sudo ln -s /lamp/mysql/elmo.ini /usr/local/zend/etc/conf.d/

5. other

If you wish to automate things further add all the commands inside a bash script:

#!/bin/bash
# USAGE: sudo ./install.sh

ROOT="/lamp"

# mysql
ln -s $ROOT/mysql/elmo.cnf /etc/mysql/conf.d/

# apache
ln -s $ROOT/apache/elmo.conf /etc/apache2/conf.d/

# remove old virtual hosts
rm /etc/apache2/sites-enabled/*

# add my virtual hosts
ln -s $ROOT/virtualhosts/000-localhost.conf /etc/apache2/sites-enabled/
ln -s $ROOT/virtualhosts/phpmyadmin.conf /etc/apache2/sites-enabled/

# php configuration
ln -s $ROOT/php/elmo.ini /usr/local/zend/etc/conf.d/

Here you can download a sample

Host based PHP settings

Posted July 18th, 2011 in Php by Florentin

How do you deploy a php settings file on multiple hosts which require different configurations ?
Let’s use the wp-config.php (WordPress configuration file) as an example.
The following snippets are going to be used inside your project’s config file (wp-config.php in this case)

Solution 1

1.a

if($_SERVER[‘HTTP_HOST’]==’example.com’ || $_SERVER[‘HTTP_HOST’]==’www.example.com’) {
# settings for the host example.com
} else {
# default settings
}

1.b

if(strpos($_SERVER[‘HTTP_HOST’], ‘example.com’)!==false) {
# settings for example.com
} else {
# default settings
}

If you need to define settings for multiple hosts, use a "switch" statement instead of "if"

Instead of "HTTP_HOST" one can also use "SERVER_NAME"

Alternatively to HTTP_HOST one can set the following in the apache’s virtual host
SetEnv APPLICATION_ENV "development"
then check for $_SERVER[‘APPLICATION_ENV’]

Pros
- light on the CPU and no disk access

Cons
- only works when the PHP file (wp-config.php in our example) runs though a web server environment, i.e. $_SERVER[‘HTTP_HOST’] is available.
- all settings live in the same file, if the file is added to a version control system everyone can view those settings.

Running the code though command line won’t work as expected.

Solution 2

if($_SERVER[‘REMOTE_ADDR’]==’127.0.0.1′) {
# localhost settings, host1
} else {
# any other settings, host2
}

Cons
- only works for 2 hosts, a localhost host (host1) and a remote host (host2)
- the settings for the host considered remote (host2) only work when the visitors come from a non-local IP.
If one uses SSH or VCN to access the remote machine (host2) then load the site/script locally, the host1’s settings will get loaded.

Solution 3

Create a file named "wp-config-local.php" in the same directory as wp-config.php then add the following to wp-config.php

if ( file_exists( dirname( __FILE__ ) . ‘/wp-config-local.php’ ) ) {
# the current host’s settings
} else {
# default settings
}

Every host would then manage it’s own wp-config-local.php without the need to share it though a version control system.

Pros
- privacy, it remains private to the host’s users
- works fine even if the script is called though the command line

Cons
- disk access

Zend Server Community Edition on Ubuntu

Posted July 16th, 2010 in Php, Ubuntu by Florentin

What is Zend Server CE (Community Edition)

You can think of Zend Server as a bundle of software and Php modules.
For Linux, ZSCE has it’s own php-*-zend-server packages which replace the ones offered by the host Os, usually php5-*
ZSCE automatically installs lighttpd-zend-server used for the web-based administration.

Why to use Zend Server CE

  • Easy to start with specially useful for Php newcomers
  • Production-ready, integrated PHP stack for non-critical applications
  • PHP bytecode caching (Zend Optimizer+) – increases performance with no application changes
  • Data caching – a set of functions that allow developers to cache data in shared memory or to disk )
  • Zend Debugger available out of the box but possible to disable it and install Xdebug
  • Available on Windows, Linux and Mac
  • All in one install taking care of all the dependencies and needed libraries
  • Integrated administration console for setting up Apache and Php configurations
  • Free (the CE version) and supported by Zend which takes care of bug fixes and future improvements
  • Easy way to install Php 5.2 on Ubuntu Lucid or other distros which offer Php 5.3 by default
  • Easy to uninstall with no traces left on the system
  • Ability to install both Php 5.2 and Php 5.3 on the same system as described here http://ireallylikesushi.com/blog/2009/12/21/installing-both-zend-server-ce-php-5-2-php-5-3-on-snow-leopard/

Sources

http://www.zend.com/en/products/server-ce/

http://devzone.zend.com/article/4295

Installation (Ubuntu Lucid 10.04)

echo deb http://repos.zend.com/zend-server/deb server non-free > /etc/apt/sources.list.d/zend.list
wget http://repos.zend.com/deb/zend.key -O- | sudo apt-key add -
apt-get update
apt-get install zend-server-ce-php-5.2 # for Php 5.2
apt-get install zend-server-ce-php-5.3 # for Php 5.3

Please find the official installation details here:

http://files-source.zend.com/help/Zend-Server-Community-Edition/zend-server-community-edition.htm#deb_installation.htm

Installing Xdebug on Zend Server (an alternative to Zend Debug)

Option 1 (recommended)
# get archive, configure and compile it. First check the latest version on xdebug.org
wget http://www.xdebug.org/files/xdebug-2.1.0.tgz # replace it with the latest version
tar -xzf xdebug-2.1.0.tgz
cd xdebug-2.1.0/
/usr/local/zend/bin/phpize
./configure --enable-xdebug --with-php-config=/usr/local/zend/bin/php-config
make


# move the xdebug.so somewhere on the server
cp modules/xdebug.so /work/xdebug/xdebug.so
# edit zend-server's php.ini to enable xdebug
sudo gedit /usr/local/zend/etc/php.ini
# add to the end of the file
zend_extension=/work/xdebug/xdebug.so


# optionally, add some xdebug directives to that php.ini
[xdebug]
xdebug.profiler_output_dir = /tmp
xdebug.profiler_enable_trigger = 1
xdebug.remote_enable=1
xdebug.remote_host=localhost

xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.var_display_max_data=10000
xdebug.var_display_max_depth=20

Option 2

ZSCE comes with a package named php-5.2-xdebug-zend-server. At this moment, the Xdebug version offered by ZSCE is older (2.0.4) than the official one (2.1.0)

# install xdebug.so in /usr/local/zend/lib/php_extensions/
apt-get install php-5.2-xdebug-zend-server
# Add the following line to /usr/local/zend/etc/php.ini
zend_extension=/usr/local/zend/lib/php_extensions/xdebug.so
# You may also add the xdebug directives described in Option 1.

Controlling Zend Server

# show all commands
sudo /usr/local/zend/bin/zendctl.sh -h

# restart zend server, including apache server and lighttpd server
sudo /usr/local/zend/bin/zendctl.sh restart

The end

# My bookmarks regarding Zend Server

http://delicious.com/florentin/zend-server

# Other bookmarks

http://delicious.com/search?p=zend%20server&u=&chk=&context=main&fr=del_icio_us&lc=0