Installing a LAMP server
Problem
You want to play with Linux + Apache + MySQL + PHP. How to install all the necessary stuff easily?
Solution
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 phpmyadmin
and mysql-admin
are not part of this collection, you need to install them separately.
In short:
sudo apt-get install tasksel sudo tasksel sudo apt-get install phpmyadmin
Then visit http://localhost to test if it works. To learn more on how to install LAMP, refer to this page.
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 /home
and /home/user
(where user
is your user name) are also accessible! Check out this script, it sets rights correctly in your ~/public_html
directory.
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:
ini_set('display_errors',1);
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:
error_reporting(E_ALL);
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
Restart Apache.
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
Related posts
Useful links (20140311)
- install phpmyadmin
- create a database and a user with phpmyadmin
- reset your mysql password on localhost (if you happened to forget it)
- phpMyAdmin: No activity within 1440 seconds; please log in again
Manjaro (20150926)
To configure Apache and PHP, refer to this wiki page: https://wiki.archlinux.org/index.php/Apache_HTTP_Server . In short: install the packages “apache
” and “php-apache
“. Add these lines to the end of /etc/httpd/conf/httpd.conf
:
LoadModule php5_module modules/libphp5.so AddHandler php5-script php Include conf/extra/php5_module.conf
Also, do these changes in /etc/httpd/conf/httpd.conf
:
#LoadModule mpm_event_module modules/mod_mpm_event.so LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
Comment out the first and uncomment the mpm_prefork_module
. Enable and start the httpd
service (systemctl enable/start httpd). Visiting http://localhost
should work, visiting a user’s home page should also work (ex.: http://localhost/~jabba
), and opening a PHP file should work too. Visit http://localhost/~jabba/phpinfo.php
, where the content of phpinfo.php
is:
<?php print phpinfo(); ?>
First I tried with “<?
” instead of “<?php
” and it didn’t work. It took me a while to figure out what was wrong…