Installing Apache + PHP | 文具屋さんネット
Feed

Installing Apache + PHP

Posted on 1月 6th, 2009 in PC関連 by 店長

Apache is one of the most famous web server which runs on most linux based servers. With just few commands you can configure apache to run with PHP 4 or PHP 5.

If you want to install PHP 4, just apt-get

apt-get install apache2 php4 libapache2-mod-php4

To install PHP5, just run the following on linux shell. Note that if you dont specify packages with '4', PHP5 will be automatically installed.

apt-get install apache2 php5 libapache2-mod-php5

Apache configuration file is located at: /etc/apache2/apache2.conf and your web folder is /var/www

To check whether php is installed and running properly, just create a test.php in your /var/www folder with phpinfo() function exactly as shown below.

nano /var/www/test.php

# test.php

< ?php phpinfo(); ?>

Point your browser to http://ip.address/test.php or http://domain/test.php and this should show all your php configuration and default settings.

You can edit necessary values or setup virtual domains using apache configuration file.
Enabling GD Library with PHP

If you want to use CAPTCHA or for dynamic image generation with php scripts for image verification to stop SPAM or automated robots, then it is absolutely necessary to get php gd library installed with php. Here is the command

apt-get install php5-gd

Thats it!! Point your browser to http://domain/test.php and the php configuration settings will show GD library will be enabled for PNG, GIF, JPG etc.
Enabling Mod Rewrite with .htaccess

Do you use mod-rewrite from apache to rewrite friendly URLs ?? This must be absolutely necessary for the rewrite module to get enabled in your apache, especially if your blog, forum script uses rewriting engine to generate friendly URLs in your site. Note that default apache2 installation does not come with mod-rewrite. Here is how you enable it. Issue the following command

# a2enmod rewrite

Once you run this command, apache will tell you that this rewrite module is enabled. You can find mod_rewrite enabled and show up in your test.php file.

I often experienced page not found 404 error with debian/ubuntu versions eventhough your apache runs with mod-rewrite. To fix this, you will need to edit the following file to make some changes.

nano /etc/apache2/sites-enabled/000-default

Find the following and change AllowOverride from None to All
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/

Upload the .htaccess file to your server and restart apache. /etc/init.d/apache2 restart

Make sure your .htaccess file has 644 permission as otherwise you get permission denied error.

NOTE: I have often experienced .htaccess remaining invisible or disappearing problem when i uploaded any dotted files in the server (like .htaccess .ftpquota). Actually FTP clients do write .htaccess file but they do not show up when you upload. This happened with me using VSFTP. Here is how you fix