Ubuntu Hardy - installing Apache and PHP5

Ubuntu Hardy Heron comes with Apache v.2.2.8 and PHP v.5.2.4 and they are easily installed via the default Ubuntu Package Manager 'aptitude'.

The advantage of using aptitude is that you will get any security updates from Ubuntu (if and when distributed) and dependencies are automatically taken care of.


Apache Install

A basic Apache install is very easy:

sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert

ServerName

Towards the end of the install you will see this warning:

apache2: Could not reliably determine the server's fully qualified domain name,
using 127.0.0.1 for ServerName

Although I'll be going into some detail about the options and settings available in the main apache configuration file, let's fix that warning straight away.

Open the main apache config:

sudo nano /etc/apache2/apache2.conf

At the bottom of the file add the following:

ServerName demo

Change the ServerName to your Slice hostname or a FQDN (remember this demo Slice has a hostname of 'demo').

Once done, save apache2.conf and gracefully restart Apache (this method of restarting won't kill open connections):

sudo apache2ctl graceful

Now the warning has gone. Nice.

Default Page

If you navigate to your Slice IP address:

http://123.45.67.890

You will see the default 'It works!' screen:

Apache Welcome Screen

Now we have the base Apache install completed, we can move onto installing PHP. If you don't require PHP then please feel free to skip the next section.

PHP5 Install

In this example, I'm not going to install all the modules available. Just some common ones.

To see what modules are available try a:

sudo aptitude search php5-

Note the '-' at the end of 'php5'. This will show any packages that start with 'php5-' and shows the available modules.

As before, due to using aptitude to install PHP5, any dependencies are taken care of:

sudo aptitude install libapache2-mod-php5 php5 php5-common php5-curl php5-dev php5-gd \
php5-imagick php5-mcrypt php5-memcache php5-mhash php5-mysql php5-pspell php5-snmp \
php5-sqlite php5-xmlrpc php5-xsl

Once done, do a quick Apache reload:

sudo /etc/init.d/apache2 reload

Done.

Almost

Well, almost done.

We do need to configure Apache for our setup so we can host multiple sites and so on.

The next articles will go through the Ubuntu Hardy Apache layout as it differs slightly from the 'classic' layout you might expect if you had installed from source.

PickledOnion.

Article Comments:

Enrike Brujo commented Fri May 23 23:55:46 UTC 2008:

THANK YOU !!! i was like 6 hours trying to install apache in my hardy... now i can !!!

Dylan commented Thu May 29 02:06:02 UTC 2008:

Thankyou very much.

Brad Pitt commented Tue Jun 03 11:37:36 UTC 2008:

I was trying for months to get apache running in my windows computer. I can't believe how easy it is here. Thanks a bunch.

PeterG commented Sun Jun 08 04:08:47 UTC 2008:

Where does one install Zend Optimizer OR Ioncube Loader

Either of which will work for me other than disabling PHP Safe Mode which is not recommended. Not much in the forms or articles about this. I can find info on the web to do it but cant find where on the slice to put the Zend Lib stuff

Hunter commented Thu Jun 26 22:49:10 UTC 2008:

These tutorials are great. Thank you much!

MA commented Tue Jul 01 10:07:49 UTC 2008:

Thanks

prism commented Fri Jul 04 04:04:32 UTC 2008:

I get this error. The install completes, but apache2.conf isn't made and httpd.conf is empty:

Setting up apache2-mpm-prefork (2.2.8-1ubuntu0.3) ... grep: /etc/apache2/mods-enabled/*.load: No such file or directory Module cgid does not exist! This module does not exist! It looks like you've deleted /etc/apache2/mods-available/cgi.load, so cgi can not be enabled. To fix this, please purge and reinstall apache2.2-common.

  • Starting web server apache2 apache2: Could not open configuration file /etc/apache2/apache2.conf: No such file or directory ...fail! invoke-rc.d: initscript apache2, action "start" failed.

prism commented Fri Jul 04 04:10:44 UTC 2008:

nvm, I had to do a seperate sudo aptitude purge * and ... remove * instead of ... --purge remove *.

art commented Fri Jul 04 17:59:07 UTC 2008:

my httpd.conf file is empty what should i do?

WS commented Mon Jul 07 19:10:51 UTC 2008:

Thanks.

Ben commented Sun Jul 13 06:00:18 UTC 2008:

Run 'sudo apt-get update' if you are getting errors with packages not being found.

Gordon commented Sun Jul 20 18:26:52 UTC 2008:

I followed the tutorial exactly and everything seemed to have worked, however whenever I serve a file with a .php file extension it does not parse the file but just serves the raw php source after prompting the user for a download dialog. Is there something I am missing?

Stephen commented Thu Jul 24 14:59:54 UTC 2008:

I have the same problem as Gordon.

gerard commented Thu Jul 24 23:40:39 UTC 2008:

Ok, I had the same problems as gordon and stephen, and i found this on: [https://help.ubuntu.com/community/ApacheMySQLPHP]

"Does your browser ask if you want to download the php file instead of displaying it? If Apache is not actually parsing the php after you restarted it, install libapache2-mod-php5. It is installed when you install the php5 package, but may have been removed inadvertently by packages which need to run a different version of php.

You may also need to actually enable it, by doing sudo a2enmod php5 followed by sudo /etc/init.d/apache2 restart. If sudo a2enmod php5 returns "$ This module does not exist!", you should purge (not just remove) the libapache2-mod-php5 package and reinstall it.

Be sure to clear your browser's cache before testing your site again."

It worked perfectly =) ...

Eric commented Sun Jul 27 18:01:27 UTC 2008:

After installing the php5-cli package, i was seeing this error from "php -v"

php -v PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20060613/imagick.so' - libWand.so.9: cannot open shared object file: No such file or directory in Unknown on line 0

I found this on the ubuntu forums: http://ubuntuforums.org/showthread.php?t=573878&page=2#12

which basically instructs you to

sudo apt-get install make php5-dev php-pear sudo apt-get remove php5-imagick sudo apt-get install libmagick9-dev sudo pecl install imagick

James commented Fri Aug 08 02:17:11 UTC 2008:

From the forums - if you are having problems installing php5 try this before the install:

sudo aptitude update

zhasharel commented Sat Aug 09 18:44:51 UTC 2008:

Thanks for the great articles! They are great time savers and extremely helpful.

jmurphy commented Sun Aug 17 00:23:21 UTC 2008:

I followed the install steps and had the same problem as Gordon.

I tried sudo a2enmod php5 and got: This module does not exist!

I purged the package: sudo aptitude purge libapache2-mod-php5

reinstalled it: sudo aptitude install libapache2-mod-php5

tried sudo a2enmod php5 (again) and got: This module does not exist!

I'd appreciate anyone's help with this installation problem.

jmurphy commented Sun Aug 17 00:43:07 UTC 2008:

It looks like I'm getting erros when connecting to the updates server:

Reading package lists... Done Building dependency tree
Reading state information... Done Reading extended state information
Initializing package states... Done Writing extended state information... Done Building tag database... Done
The following NEW packages will be automatically installed: libapache2-mod-php5 php5-common The following NEW packages will be installed: libapache2-mod-php5 php5 php5-common 0 packages upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 3003kB of archives. After unpacking 7168kB will be used. Do you want to continue? [Y/n/?] y Writing extended state information... Done Err http://archive.ubuntu.com hardy-updates/main php5-common 5.2.4-2ubuntu5.1 404 Not Found [IP: 91.189.88.31 80] Err http://archive.ubuntu.com hardy-updates/main libapache2-mod-php5 5.2.4-2ubuntu5.1 404 Not Found [IP: 91.189.88.31 80] Err http://archive.ubuntu.com hardy-updates/main php5 5.2.4-2ubuntu5.1 404 Not Found [IP: 91.189.88.31 80] E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/p/php5/php5-common5.2.4-2ubuntu5.1amd64.deb: 404 Not Found [IP: 91.189.88.31 80]

So it seems that I'm getting those errors (This module does not exist!) because the update server wasn't sending the files.

Has anyone had this problem?

Sidharth commented Sun Aug 17 15:18:33 UTC 2008:

I am also getting this error!

Sidharth commented Sun Aug 17 15:38:11 UTC 2008:

typing

"sudo aptitude update"

and then reissuing the command makes the install work

homayoon taheri commented Sat Aug 23 11:48:50 UTC 2008:

Thanks!

david commented Mon Sep 08 01:20:47 UTC 2008:

I have the same issue that prism & art had, but don't understand how to correct. Is there an answer here I am missing?

I tried a sudo aptitude purge of apache followed by a sudo aptitude install and the file reappeared still empty :(

any suggestions? instructions?

david commented Fri Sep 12 05:08:38 UTC 2008:

ok - dolt here... the httpd.conf file has been replaced with the apache2.conf file... it lingers to tantalize and confuse those of us who remember 1.3 when it was new and we used CLI every day :)

anush commented Mon Oct 13 13:29:23 UTC 2008:

Considering that apache2-mpm-worker is supposedly touted as being much faster than prefork, has anyone tried out running this with php5 (with thread safe mode enabled). Plenty of forums say "enter at your own risk" but was curious to see if anyone has actually figured out a set of modules that do work well in this configuration.

Pete commented Tue Oct 21 22:36:56 UTC 2008:

Great guide, got me up and running quick. Thanks, really starting to like slicehost you guys are making my first VPS experience uncomplicated :D

bohoo commented Tue Oct 28 06:19:09 UTC 2008:

Hi, The php installation went thru successfully. But if I just do this, it doesnt work.

php -v

-bash: php: command not found

whereis php

php:

So, looks like the php wasnt actually installed? Any help please.

Thanks

PickledOnion commented Tue Oct 28 11:16:12 UTC 2008:

bohoo,

If you want to run php from the command line you need to install the cli component (command line component):

php-cli

Once you have done that you can use the terminal.

PickledOnion

KFCSpike commented Tue Oct 28 19:29:22 UTC 2008:

I needed to run some maintenance scripts for my MediaWiki that needed php to be run in command line mode.

A quick

sudo aptitude install php5-cli

fixed it.

Thanks again Pickled :)

Kim Pepper commented Thu Nov 13 06:44:41 UTC 2008:

I spent hours trying to work out why the external modules were not loading.

Turns out my php.ini was pointing to /usr/lib/php/modules instead of /user/lib/php5/20060613

I'd tried so many things to get this working including copying a php.ini file from another server so I'm not entirely sure what caused this. I thought I'd share my experience in case anyone else has the same problems.

Kim Pepper commented Thu Nov 13 06:47:37 UTC 2008:

Also, to install a drupal site, I needed to bump up the memory to 64M. :-\

Kurt commented Tue Dec 02 19:14:53 UTC 2008:

I am working on a redesign for our site, and was having an issue getting the PHP to run on my dev server. sudo a2enmod php5 fixed the problem immediately.

Now, all I have to do is drastically improve on the current site design.

ben commented Mon Dec 15 16:59:00 UTC 2008:

Great tutorial. I had to do this before starting this tutorial...

sudo apt-get update sudo apt-get install build-essential

sizar commented Sun Dec 21 10:14:29 UTC 2008:

Hello everybody Im a newbie to Ubuntu and wanted to install php5 and apache2 + mysql I think I installed alot of things there :) and nothing worked :( (lack of experience). So can any body help me to show to install all of these packges and make them work perfectly from the scratch .

Thank you all

carlos commented Sat Dec 27 03:52:13 UTC 2008:

hello, i followed the steps and made a test.php file and put it in /var/www/ but when i try to open it in browser it justs try to download it instead of open it.

what can i do?

srb commented Sun Feb 08 15:46:38 UTC 2009:

sudo apt-get update

is critical Should be mentioned at the top of the tutorial

anurag commented Fri Feb 13 20:18:21 UTC 2009:

Very nice, informative, helpful and precise article.

Thanks

Andrei Erdoss commented Sun Feb 22 10:59:58 UTC 2009:

Hello,

I am using mod_rails with ruby enterprise edition. After doing some research I noticed that the worker version of Apache is faster than the prefork one. What is the easiest/safest ways of reinstalling apache with the worker option?

Thank you,

karthik commented Mon Mar 23 02:52:40 UTC 2009:

Hi, I just set up my new slice with the instructions given. I have set up Apache, PHP and Mysql.

I have doubts regarding the following.

1) I want to know how to install PEAR packages for PHP (I Am new to Ubuntu)

2) Where are the PEAR (.inc) modules for PHP located. I Am not finding them anywhere...

3) Where can I search for a list of PEAR packages for PHP that I want to install. I have the same problem for PErl packages also.

Thanks,

Karthik

Murthy Kavali commented Fri Apr 10 10:33:55 UTC 2009:

Excellent...It works!

Thanks alot..

Murthy Kavali commented Fri Apr 10 11:18:46 UTC 2009:

Awesome...I am trying from 2 days to setup these things..You made my job easy..Thank you Murthy Kavali

Sean Brown commented Sat May 02 16:57:09 UTC 2009:

A few people have asked how to install the Zend optimizer on Hardy. After some hair-pulling, I figured it out. Here you go.

  1. Follow the instructions above to get PHP5 and Apache2 installed.
  2. Install the PHP client. sudo aptitude install php5-cli
  3. The Zend optimizer installation will look for httpd, but the install above names the Apache binary apache2, so let's symbolic link to it. sudo ln -s /usr/sbin/apache2 /usr/sbin/httpd
  4. Download the correct version of Zend. Remember that our slices are 64-bit. wget http://downloads.zend.com/optimizer/3.3.3/ZendOptimizer-3.3.3-linux-glibc23-x86_64.tar.gz
  5. Untar and unzip tar xvfz ZendOptimizer-3.3.3-linux-glibc23-x86_64.tar.gz
  6. Run the installer cd ZendOptimizer-3.3.3-linux-glibc23-x86_64 sudo ./install.sh
  7. During the install, Zend will ask you where your php.ini file is. If you followed the directions in this article, it will be in /etc/php5/apache2/

Dave commented Fri May 22 08:44:30 UTC 2009:

Again, sweet article! Couldn't be anymore straight forward!

Tony commented Fri Jul 03 22:12:12 UTC 2009:

I keep getting the following error: Warning: pregreplace() [function.preg-replace]: Compilation failed: this version of PCRE is not compiled with PCREUTF8 support at offset 0 in /var/www/pop/public/wp-includes/shortcodes.php on line 227 .... I don't understand why the php5 package does not include a utf8 compatible version of PCRE. Is there any way to remedy this by installing a different package or specifying an option? Installing from source is annoying. Thanks!

Tim commented Mon Aug 17 16:56:14 UTC 2009:

@Tony:

I'm having a similar issue, and it looks like installing from source isn't that bad, here's an article that has a nice step-by-step for Ubuntu. it's essentially one change in the build configuration.

http://pullmonkey.com/2009/3/10/compilation-failed-this-version-of-pcre-is-not-compiled-with-pcre_utf8-support

jGc commented Thu Nov 12 20:35:33 UTC 2009:

I had the same/similar question as karthik when I try to send email from PHP.

sudo install php-pear

and then from http://markstechstuff.blogspot.com/2009/04/installing-pear-mail-for-php-on-ubuntu.html

sudo pear install mail

sudo pear install Net_SMTP

sudo pear install Auth_SASL

sudo pear install mail_mime

The exact order may need to be adjusted. The dependency was not correct. I received some slap on the wrist. Eventually, I was able to fire email from PHP (via Rackspace email).

lawrence commented Sat Jan 23 13:11:06 UTC 2010:

i setup php,apache and mysql and tested the localhost It Works! runs ok, where does my application folder reside in and how do i test it? i put it in the /var/www and when i point to it, it gives me the option of either opening to view or save it else where. What am doing wrong please.

Chase commented Sun Jan 31 18:11:30 UTC 2010:

Ok, I've followed this tutorial and it seems to work wonderfully, but I'm having a disconnect here - how can I start transferring files onto my apache server to make them viewable on the web?

I've found the default index.html at /var/www

From there, I've tried to sftp into the /var/www folder, but I didn't have permissions to write to the folder. did a chmod 777 on the folder and I'm still having problems.

I don't know if there is some internal restriction with apache here in that it has its own way of keep permissions or something like that.

Getting this to work with ftp would be super handy. I don't want to have to build a whole site using vim :)

Chase commented Sun Jan 31 18:22:34 UTC 2010:

Rookie mistake on my above post. I did chmod 777 on the /var/www folder, then i sftp'd into the folder and tried modifying the index.html folder. turns out the index.html needed to be chmod'd or deleted too :D

Quick question - should I chmod 777 on my /var/www folder, or should i use something a bit more strict that will still allow me to work with it via sftp?

Thanks!

Jered commented Tue Feb 02 16:30:24 UTC 2010:

We have an article on Apache Virtual Host permissions, that covers the topic of setting file permissions pretty well. Check it out here.

Barry commented Sun Feb 07 01:00:59 UTC 2010:

Any tips for updating PHP from 5.2.4 to 5.2.12 on Hardy?

jday commented Sat Jun 05 03:24:30 UTC 2010:

I too would like to upgrade from 5.3.4 to 5.2.12. Is there a tutorial for that out there?

Jered commented Mon Jun 07 06:33:15 UTC 2010:

You can check out this forum post for some help on updating to 5.3 - that could likely be adapted to 5.2.12 if you grab different PHP source instead.

Charlie commented Mon Mar 07 13:47:14 UTC 2011:

Why not use this task command to install and configure them all:

sudo tasksel install lamp-server

Jered commented Mon Mar 07 16:04:52 UTC 2011:

Flexibility, really. Using aptitude to install apache and PHP doesn't take that much more typing, but it allows you to choose which apache or PHP modules you install up front. This approach also covers the install instructions for people who may be using apache and PHP either without a database or with different database software (like PostgreSQL or CouchDB).

It's also worth mentioning that tasksel can work well on a fresh install (like a new VPS instance), but it doesn't handle the resolution of package conflicts nearly as well as aptitude does on a system that's seen some use already.

Corey Salinas commented Thu Aug 08 04:32:00 UTC 2013:

Greetings, I believe your blog could possibly be having internet browser compatibility problems. When I look at your blog in Safari, it looks fine but when opening in I.E., it has some overlapping issues. I merely wanted to provide you with a quick heads up! Aside from that, fantastic blog!

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)