How to configure apache for a new website and configure php in Ubuntu

2 min

This tutorial will teach you how to configure apache web server to run php scripts and create a new site in Ubuntu.

This also assumes that you have already installed php and apache in Ubuntu. If you didn’t then here is a step by step guide on –

How to install apache in Ubuntu
How to install php in Ubuntu

To install PHP 5 in Ubuntu – follow below steps:

a) Open terminal and type below commnad:

sudo apt-get install libapache2-mod-php5

b) The installer itself would create the php configuration file i.e. php.ini in the installation directory /etc/php5

How to configure Apache to run PHP scripts

Follow below steps to configure apache to run PHP scripts:

a) Copy the default conf file to a new file named say “my_first_site.conf” using below command.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/my_first_site.conf

b) Create a folder inside your home folder named “public_html”.
mkdir /home/

c) Open the my_first_site.conf that you just created using a text editor as root. You can try below command:
sudo gedit /etc/apache2/sites-available/my_first_site.conf

d) Change the Document ROOT value to the new created directory. Find a statement in open file my_first_site.conf as below:
DocumentRoot /var/www/html

…and change it to as:
DocumentRoot /home/

e) Save and close the editor.

f) Now, its time to activate the new created site and deactivate the default one. To do that run this command:
sudo a2dissite 000-default && sudo a2ensite my_first_site

g) Open the apache2.conf file and modify the Directory tag to your public_html_folder. Run this command from the terminal:
sudo gedit apache2.conf

Find statements as below in apache2.conf –

Options Indexes FollowSymLinks
AllowOverride None
Require all granted

…and change it to:

Options Indexes FollowSymLinks
AllowOverride None
Require all granted

h) Save and close the editor.

i) Restart apache using below command.
sudo service apache2 restart

j) To test whether php is installed correctly with apache server follow these steps:

k) Create a new file named index.php inside your public_html folder. Put this below line inside index.php and save the file.

l) Open web browser and type this in your address bar :

If your apache and php installed perfectly, you would see php information as below.


apache php configure complete

Congratulations! You have successfully installed apache and php in Ubuntu.


Creator and author of Connect with me via Telegram, 𝕏 (Twitter), or send us an email.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments