[[howto:web:lamp-development-on-ubuntu-running-as-your-own-user]]

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

howto:web:lamp-development-on-ubuntu-running-as-your-own-user [2018-08-05 04:25] (current)
Line 1: Line 1:
 +~~META:
 +creator = Brendan Kidwell
 +&date created = 2014-09-25
 +~~
 +{{tag>​lamp ubuntu web-development}}
 +~~DISCUSSION~~
 +====== LAMP Development on Ubuntu Running As Your Own User ======
 +
 +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 <wrap file>​www-data</​wrap>​. 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 <wrap file>​chown</​wrap>​ 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 <wrap file>/​home/​$app_name/​www</​wrap>​.
 +
 +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 <wrap file>​proxy_fcgi</​wrap>​ module.
 +
 +===== Setup Apache =====
 +
 +<code bash>
 +# 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
 +</​code>​
 +**Check:** Go to <wrap file>​http:​%%//​%%localhost/</​wrap>​ in your browser and you should see the Apache welcome page.
 +
 +===== Configure Hostname =====
 +
 +It helps to configure a custom hostname in your <wrap file>​hosts</​wrap>​ for your development work, especially if you are going to use more than one virtual host.
 +
 +Add the following to the end of <wrap file>/​etc/​hosts</​wrap>:​
 +
 +<​code>​
 +127.0.0.1 www.local
 +</​code>​
 +
 +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 <wrap file>​http:​%%//​%%www.local/</​wrap>​ in your browser and you should see the Apache welcome page.
 +
 +===== Install PHP =====
 +
 +<code bash>
 +# Install PHP's FastCGI daemon
 +sudo apt-get install php5-fpm
 +</​code>​
 +Edit the daemon'​s config file:
 +
 +<file ini /​etc/​php5/​fpm/​pool.d/​www.conf>​
 +# (Search for and edit these three lines.)
 +user = *** your desktop username ***
 +group = *** your desktop username ***
 +listen = 9000
 +</​file>​
 +
 +The first two values tell the PHP FastCGI daemon to run as //you// instead of <wrap file>​www-data</​wrap>,​ so that the application server and all your editing from the desktop have the same permissions to your working files.
 +
 +You have to change <wrap sym>​listen</​wrap>​ from a socket to a port number because Apache'​s <wrap file>​proxy_fcgi</​wrap>​ 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 <wrap file>​www.conf</​wrap>​ to a new filename in the same folder, change the first line from "​[www]"​ to something else, and fill in new <wrap sym>​user</​wrap>,​ <wrap sym>​group</​wrap>,​ and <wrap sym>​listen</​wrap>​ values.
 +
 +<code bash>
 +# Restart PHP FastCGI daemon(s)
 +sudo service php5-fpm restart
 +</​code>​
 +
 +===== Setup Apache Virtual Host =====
 +
 +Create a folder <wrap file>​~/​www</​wrap>​ to host your LAMP applications.
 +
 +Create an Apache virtual host configuration file:
 +
 +<file text /​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>​
 +</​file>​
 +
 +Note the three <wrap sym>​USERNAME</​wrap>​ instances in the text above -- replace them with your actual username.
 +
 +<code bash>
 +# Enable the new site
 +sudo a2ensite www.local
 +sudo service apache2 reload
 +</​code>​
 +
 +As in the previous section, create additional <wrap file>​.conf</​wrap>​ files in the same folder if you want to run additional applications on different hostnames as different users.
 +
 +**Check:** Create this file
 +
 +<file php ~/​www/​info.php>​
 +<?php phpinfo();
 +</​file>​
 +
 +and view <wrap file>​http:​%%//​%%www.local/​info.php</​wrap>​ . 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:
 +
 +<code bash>
 +sudo apt-get install php5-apcu php5-cli php5-curl \
 +php5-gd php5-imagick php5-imap php5-mcrypt php5-sqlite
 +</​code>​
 +
 +Also, change the maximum upload and post size from the default 8MB or so to something more sane:
 +
 +<file ini /​etc/​php5/​fpm/​php.ini>​
 +# (Search for and edit these two lines.)
 +post_max_size = 200M
 +upload_max_filesize = 200M
 +</​file>​
 +
 +<code bash>
 +# Restart PHP
 +sudo service php5-fpm restart
 +</​code>​
 +
 +**Check:** View <wrap file>​http:​%%//​%%www.local/​info.php</​wrap>​ . You should see the additional modules installed, and the new maximum upload/post size.
 +
 +===== Install MySQL =====
 +
 +<code bash>
 +# Install MySQL
 +sudo apt-get install mysql-server php5-mysql
 +</​code>​
 +
 +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 [[http://​www.phpmyadmin.net/​home_page/​downloads.php|the phpMyAdmin downloads page]], fetch your preferred edition, and extract the archive to <wrap file>​~/​www</​wrap>​. Rename it to remove the version number from the folder name, making it simply <wrap file>​~/​www/​phpmyadmin</​wrap>​.
 +
 +phpMyAdmin has a wizard for creating a config file. Do this:
 +
 +<code bash>
 +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
 +</​code>​
 +
 +**Check:** Access <wrap file>​http:​%%//​%%www.local/​phpmyadmin</​wrap>​ , login as <wrap file>​root</​wrap>​ 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:
 +
 +<code bash>
 +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
 +</​code>​
 +
 +Now create two Bash scripts to start and stop the services:
 +
 +<file bash ~/​bin/​www-up>​
 +#!/bin/bash
 +sudo service apache2 start
 +sudo service mysql start
 +sudo service php5-fpm start
 +</​file>​
 +
 +<code bash>
 +chmod +x ~/​bin/​www-up
 +</​code>​
 +
 +<file bash ~/​bin/​www-down>​
 +#!/bin/bash
 +sudo service apache2 stop
 +sudo service mysql stop
 +sudo service php5-fpm stop
 +</​file>​
 +
 +<code bash>
 +chmod +x ~/​bin/​www-down
 +</​code>​
 +
 +**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 <wrap file>​http:​%%//​%%www.local/</​wrap>​ from <wrap file>​~/​www</​wrap>​ in your home folder, and PHP applications run in the same user context as your desktop applications.