LAMP Development on Ubuntu Running As Your Own User

Published: 

There are a lot of guides for setting up Apache, MySQL and PHP on an Ubuntu desktop or server but all the guides I've seen assume you're going to run PHP scripts inside Apache as the user www-data. Then they gloss over the issue of running the desktop and editing application files as one user, and running PHP scripts as another user. Files and folders might get created by the PHP application and not writeable by your personal user, and then you have to constantly chown things before editing them.

This guide will show you another way to setup a host for LAMP development, running PHP applications using PHP's built-in FastCGI daemon as your own desktop user. This method will also work on a production server where you might want to have each application running as its own user with its own home folder under /home/$app_name/www.

All you need is Ubuntu Linux or Linux Mint running on your desktop or server. (Debian might work, but I'm not familiar with how different the Ubuntu packages mentioned here are from Debian.) If your desktop is running Windows or OS X or another OS, this guide should work just fine if you run Ubuntu in a virtual machine; in that case I recomment Linux Mint MATE edition on VirtualBox.

You must be using an OS based on or equivalent to Ubuntu 14.04 (released in April 2014) because of recent changes/updates in the Apache proxy_fcgi module.

Setup Apache

# Install Apache
sudo apt-get install apache2
# Enable proxy_fcgi module
sudo a2enmod proxy_fcgi
# Enable rewrite module, needed by many PHP applications
sudo a2enmod rewrite
# Fix "Couldn't determine hostname" error during Apache startup
echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/fqdn.conf && sudo a2enconf fqdn
# Restart Apache
sudo service apache2 restart

Check: Go to http://localhost/ in your browser and you should see the Apache welcome page.

Configure Hostname

It helps to configure a custom hostname in your hosts for your development work, especially if you are going to use more than one virtual host.

Add the following to the end of /etc/hosts:

127.0.0.1 www.local

For any additional virtual hosts you will need, add more lines with the same IP address and some other $name.local hostname.

Check: Go to http://www.local/ in your browser and you should see the Apache welcome page.

Install PHP

# Install PHP's FastCGI daemon
sudo apt-get install php5-fpm

Edit the daemon's config file:

/etc/php5/fpm/pool.d/www.conf

# (Search for and edit these three lines.)
user = *** your desktop username ***
group = *** your desktop username ***
listen = 9000

The first two values tell the PHP FastCGI daemon to run as you instead of www-data, so that the application server and all your editing from the desktop have the same permissions to your working files.

You have to change listen from a socket to a port number because Apache's proxy_fcgi module doesn't support sockets yet.

If you are setting up a production server, you might want to have a separate username and separate PHP FastCGI daemon for each application. To setup additional usernames and ports, copy www.conf to a new filename in the same folder, change the first line from "[www]" to something else, and fill in new user, group, and listen values.

# Restart PHP FastCGI daemon(s)
sudo service php5-fpm restart

Setup Apache Virtual Host

Create a folder ~/www to host your LAMP applications.

Create an Apache virtual host configuration file:

/etc/apache2/sites-available/www.local.conf

<virtualhost *:80>
    ServerName www.local
    ServerAdmin webmaster@localhost
    DocumentRoot /home/USERNAME/www
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/home/USERNAME/www/$1
    <directory "/home/USERNAME/www">
        Order allow,deny
        Allow from all
        AllowOverride FileInfo All
        Require all granted
    </directory>
</virtualhost>

Note the three USERNAME instances in the text above -- replace them with your actual username.

# Enable the new site
sudo a2ensite www.local
sudo service apache2 reload

As in the previous section, create additional .conf files in the same folder if you want to run additional applications on different hostnames as different users.

Check: Create this file

~/www/info.php

<?php phpinfo();

and view http://www.local/info.php . You should get a PHP configuration dump.

More PHP Stuff

You're probably going to need these additional packages to get work done in PHP:

sudo apt-get install php5-apcu php5-cli php5-curl \
php5-gd php5-imagick php5-imap php5-mcrypt php5-sqlite

Also, change the maximum upload and post size from the default 8MB or so to something more sane:

/etc/php5/fpm/php.ini

# (Search for and edit these two lines.)
post_max_size = 200M
upload_max_filesize = 200M
# Restart PHP
sudo service php5-fpm restart

Check: View http://www.local/info.php . You should see the additional modules installed, and the new maximum upload/post size.

Install MySQL

# Install MySQL
sudo apt-get install mysql-server php5-mysql

During installation, you'll be asked for a root password. For security, especially if you're planning to use the root user in your web app configurations, you should choose something different from your desktop login. Don't lose it.

Install phpMyAdmin

You'll probably want to use everyone's favorite MySQL administration front-end. Go to the phpMyAdmin downloads page, fetch your preferred edition, and extract the archive to ~/www. Rename it to remove the version number from the folder name, making it simply ~/www/phpmyadmin.

phpMyAdmin has a wizard for creating a config file. Do this:

mkdir ~/www/phpmyadmin/config
chmod o+rw ~/www/phpmyadmin/config
# Access http://www.local/phpmyadmin/setup/ in your browser.
# Click 'New Server'.
# Browse through the settings and change any defaults if you need to.
# Click 'Apply'.
# Click 'Save'.
# Close your browser.
cp ~/www/phpmyadmin/config/config.inc.php ~/www/phpmyadmin/
rm -rf ~/www/phpmyadmin/config

Check: Access <http://www.local/phpmyadmin , login as root with the MySQL root password you set in the previous section.

Disable Automatic Service Startup

This last section is optional. If you want to save memory and only run Apache, PHP, and MySQL when you are doing development, disable automatic startup of their services:

echo manual | sudo tee /etc/init/apache2.override
echo manual | sudo tee /etc/init/mysql.override
echo manual | sudo tee /etc/init/php5-fpm.override

Now create two Bash scripts to start and stop the services:

~/bin/www-up

#!/bin/bash
sudo service apache2 start
sudo service mysql start
sudo service php5-fpm start
chmod +x ~/bin/www-up

~/bin/www-down

#!/bin/bash
sudo service apache2 stop
sudo service mysql stop
sudo service php5-fpm stop
chmod +x ~/bin/www-down

Check: Run the two scripts from the command prompt and check in your process list utility that the servers are started and stopped.

Conclusion

You now are serving http://www.local/ from ~/www in your home folder, and PHP applications run in the same user context as your desktop applications.

Comments

Add Comment

* Required information
5000
Powered by Commentics

Comments (4)

this is a comment

no wiki syntax

http://www.glump.net

Ok thank you. I will try it again.

This guide is really promising. This is exactly the kind of set-up I want... but it didn't work for me. I have a fresh install of Kubuntu 14.04. Followed this guide very carefully, even when I wanted to change the name of things (e.g. I'd call the 'www' directory 'dev'). Resisted and stuck to the guide.

When I got as far as creating the php.info file, it didn't show up, just an error message saying file doesn't exist. Definitely does. After restarting php and apache a few times, I moved on.

Unsurprisingly, had the same problem when it comes to running phpmyadmin.

I went back over the tutorial, double checking everything - no stray typos, put in my username, etc., but everything was in place.

Restarted PHP and Apache a few times. Now it says I don't have permission to access / on this server.

I'm stumped. Would appreciate any advice.

Cathal, I'm sorry I took so long to reply. If you don't have a file called '/etc/php5/fpm/php.ini', you must have either missed installing the package 'php5-fpm', or had some failure when you installed it.