How To Build Working Ubuntu Server with Apache and PHP
| |You may be wondering why it is only Apache and PHP we are going to install? As I said previously that the main goal of ServerMom.com is to helping newbies to build a working server to host their websites. For your information, not all websites are requiring database, Rapidleech for instance. Besides, as I believe there are many newbies out there who are really new to Linux, VPS or Server, so here it is I post easier way to setup a working live server to host websites in it. I promise I’ll post another tutorial about how to also install MySQL.
Q: What are we going to do in this tutorial?
A: In this tutorial we will install and setup Apache web server and PHP. By installing Apache itself you can already host your files and accessible via Internet but that’s only static files like .html, .css, and media files like .jpg, .gif, .png and so on without ability to run php-based scripts. Hence, we will also install PHP5 (the latest one).
Requirements
- You’ll need Putty and ability to use it. Linux and Mac users can simply use Terminal.
Read:
Using Putty To SSH To Your Server From Windows
Most common Unix commands used to SSH - A VPS or Dedicated server running Ubuntu OS but Debian may also be similar. In this example I am using Ubuntu 12.04 LTS (see my playground server)
- A computer with Internet connection to access your server with it
- About 15 minutes of your time and..
- A cup of tea
p.s: When I say “server” it can refer to both VPS and Dedicated one.
1. Login to your server
Once you logged in, make sure you do some basic optimization tasks for a brand new created Ubuntu server. Read our guide about what have to do with a brand new server. Following that guide is basically optional but really recommended.
2. Install Apache Web Server
Apache is really a default web server which is licensed free and opensource. It has been used by many servers around the world. There are few other web server like Litespeed and Nginx but Apache is still dominating because its easiness.
Use following command:
sudo apt-get -y update sudo apt-get -y upgrade
Wait for the process to finish then type this command followed by hitting enter:
sudo apt-get -y install apache2-prefork-dev apache2-utils apache2.2-bin apache2.2-common apache2
note about using commands: If you decided to follow our previous guide about initial server setup, then you already disabled root login which means you have to add “sudo” each time executing a command. You can simply remove the “sudo” part from each command if you didn’t disable root login.
3. Install PHP5
There are many custom builds of PHP but this time you can simply use the one I use and follow the steps like what I did.
Use following command to install PHP5 on your Ubuntu server:
sudo apt-get -y install php5 php5-cgi php5-cli php5-common php5-curl php5-dev php5-gd php5-tidy php5-xmlrpc php5-xsl php5-suhosin php5-mcrypt php5-imap php5-imagick libapache2-mod-php5
4. Add php to Directory Index (optional)
Normally Apache will start looking at index.html file before index.php. You can change that order by editing “dir.conf” file. Use following command:
sudo nano /etc/apache2/mods-enabled/dir.conf
You should see something like this:
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
Now move “index.php” into the beginning part so it will look like this:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
Once done hit Control+O to save then Control+X to to exit nano editor screen.
5. Test It
You can put all your files and php scripts in “/var/www” directory. So go there by using this command..
cd /var/www
To test whther the PHP works as expected or not, simply create a new file to display php info:
sudo nano viewinfo.php
Once nano screen displayed, enter this php script:
<?php phpinfo(); ?>
Now hit Control+O to save and Control+X to exit nano editor.
Do not forget restart Apache2 service, type this command:
sudo service apache2 restart
or
sudo /etc/init.d/apache2 restart
Then open your most favorite web browser then try accessing that viewinfo.php file by typing your IP in address bar:
e.g: http://141.0.170.101/viewinfo.php
That is it. Now you have a working server to host your PHP-based scripts and run it on your browser. But remember your server currently doesn’t support MySQL because we have not installed it yet. I’ll write about how to install MySQL server in next post.
It was great
Salaam mom,
This is awesome for my school project!
Hello!
After five or six tries to setup my VPS I found your tutorials and started following them. I must say, they are well commented and I begin to understand things better now (I have read through them all before I started typing).
I have a question though before I continue building my VPS. If I plan to add multiple websites to my VPS with different domain names, databases etc. should I still follow this specific guide?
Thank you!
You don’t have to follow all the steps, all you need to do is to create new hosts entries in Apache’s config file
Let me start by saying I really enjoy your easy to read and easy to follow tutorials!
One thing I did notice, as you’re trying to go absolutely step-by-step for the inexperienced users:
If following your directions verbatim, some may have an issue viewing the viewinfo.php file they are creating. Apache default directory is /var/www/html, and they are creating it in /var/www. When you type “your domainORip/viewinfo.php” into the browser, it will be unable to find the file unless it is located in the /html/ directory.
Doesn’t seem anyone really has an issue with it as I’m sure most who visit have at least a cursory understanding of how to set things up, just thought I’d mention it!