You want to set up a LAMP (Linux, Apache, MySQL, Python) environment at home for testing purposes.
In addition, we want to create a
public_html directory in our HOME directory and we want to put our Python web scripts in there.
I suppose Apache2 is installed and works correctly. If you visit http://localhost/, you should see the text “It works!”.
Now, create the directory
~/public_html and put in this folder a basic
index.html file with some greetings context (like “<h1>hello from index.html<h1>”). Enable the user directories and restart the web server:
$ sudo a2enmod userdir $ sudo service apache2 restart
http://localhost/~%5Busername%5D/index.html. You should see the greetings.
Install and enable WSGI in user directories
Create the file
def application(environ, start_response): """"Simplest possible application object""" status = '200 OK' response_headers = [('Content-type', 'text/plain')] start_response(status, response_headers) return ['Hello, World!\n']
Point your browser to this file. It’s very likely that your browser will try to download this file, which means that WSGI is not yet configured.
Install the WSGI module and enable it in Apache2:
$ sudo apt-get install libapache2-mod-wsgi $ sudo a2enmod wsgi
/etc/apache2/apache2.conf and add this line to the end:
For each wsgi user on the system, add the following lines to the end of
WSGIDaemonProcess [user name] user=[user name] home=/home/[user name]/public_html <Directory /home/[user name]/public_html> WSGIProcessGroup [user name] </Directory>
Add the following lines to
/etc/apache2/sites-available/default above the closing
<Directory /home/*/public_html> Options Indexes FollowSymLinks MultiViews ExecCGI AddHandler wsgi-script .wsgi Order allow,deny Allow from all </Directory>
Restart Apache2 with “
sudo service apache2 restart” and point your browser to
hello.wsgi. It should work fine.
Get Flask work with Apache2 + mod_wsgi
Put in the ~/public_html/hello2.wsgi file the following:
from flask import Flask app = Flask(__name__) application = app # The trick is HERE! Add this extra line! @app.route("/") def hello(): return "Hello Flask!" if __name__ == "__main__": app.run()
You want to play with Linux + Apache + MySQL + PHP. How to install all the necessary stuff easily?
There is a tool called tasksel that can install multiple related packages as a co-ordinated “task”. Launch
tasksel as root and choose the task you need. There is an option called LAMP server. The packages
mysql-admin are not part of this collection, you need to install them separately.
sudo apt-get install tasksel sudo tasksel sudo apt-get install phpmyadmin
Enabling user directories
My home directory is located at
/home/jabba. Here I want to create a directory called
public_html and I want to play with PHP in this folder. The contents of this directory is accessible via http://localhost/~jabba. However, by default it’s not enabled.
Steps to follow:
First, create the directory
$HOME/public_html. Then enable user directories:
cd /etc/apache2/mods-enabled sudo ln -s ../mods-available/userdir.conf userdir.conf sudo ln -s ../mods-available/userdir.load userdir.load
Update (20110107): There is a script called
a2enmod to create and manage these symlinks. Its opposite is
a2dismod, which disables Apache modules. See
man a2enmod. (credits go to Jesse)
And restart Apache2:
sudo /etc/init.d/apache2 restart
This tip is from here. Now HTML files are enabled in the user directory, but PHP files are still disabled!
Open the file
/etc/apache2/mods-enabled/php5.conf and locate these lines:
# To re-enable php in user directories comment the following lines # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it # prevents .htaccess files from disabling it. <IfModule mod_userdir.c> <Directory /home/*/public_html> php_admin_value engine Off </Directory> </IfModule>
As the comments suggest, you need to put those five lines in comments. Then restart the web server again:
sudo /etc/init.d/apache2 restart
This tip is from here. Now it should work. If it doesn’t work, you will have to delete the browser cache! Now it should really work :)
If you get the error message “You don’t have permission to access … on this server“, then it means there is a problem with file permissions. Directories (from
public_html inwards) must have have permission 755, files 644. Make sure that
user is your user name) are also accessible! Check out this script, it sets rights correctly in your
PHP: no error messages, just a blank page
By default, PHP is configured for production, thus error messages are disabled. This is not good for development, so you will have to modify your
php.ini file a bit (located at
/etc/php5/apache2/php.ini). Find these variables and change their values:
error_reporting = E_ALL & ~E_NOTICE | E_STRICT display_errors = On
Restart Apache and you are done. This tip is from here. It is also possible to change these values from a .php file (see the code below), but it didn’t work for me on PHP5.
This line had no effect in my case:
Update: as pointed out here in a comment, this line doesn’t catch any parsing errors “because the entire script is parsed before any of it is executed”. So the best solution is to modify
php.ini. However, the following line should work from a script:
Fatal error: Maximum execution time of 30 seconds exceeded
By default, your PHP scripts shouldn’t run longer than 30 seconds, otherwise the scripts will be terminated and you get this error message. This time limit can be increased in
php.ini. Modify this variable:
; Maximum execution time of each script, in seconds ; http://php.net/max-execution-time ; Note: This directive is hardcoded to 0 for the CLI SAPI #max_execution_time = 30 # setting 3 minutes for instance: max_execution_time = 180
After this modification, don’t forget to restart Apache.
This tip is from here.
Requirements for Symfony
If you want to use Symfony, you should install these packages too (otherwise Symfony will complain):
sudo apt-get install php5-xsl sudo apt-get install php-apc
Restart Apache. Then edit
/etc/php5/apache2/php.ini and perform this modification:
# set it to "Off" value short_open_tag = Off
Symfony provides a PHP script that checks all these requirements. It can be run from the web browser and from the command line. However, the command line version uses a different
php.ini file (in my case the CLI version is located at
/etc/php5/cli/php.ini). So run
check.php in the command-line and in the browser too.
Requirements for Yii
For Yii you might need to install these packages to pass all the tests:
sudo apt-get install php5-sqlite sudo apt-get install php5-memcache sudo apt-get install php5-pgsql