Current Location: Home> Latest Articles> How to Set Up a PHP Development Environment on Ubuntu (Apache + PHP) – A Complete Guide

How to Set Up a PHP Development Environment on Ubuntu (Apache + PHP) – A Complete Guide

gitbox 2025-06-18

1. Install Apache Server

On Ubuntu, use the following commands to install Apache server:

sudo apt update
sudo apt install apache2

Once installed, you can check the status of the Apache service with the following command:

sudo systemctl status apache2

2. Configure Apache Server

Apache's configuration files are primarily found in two directories: `/etc/apache2` and `/var/www/html`. The former contains Apache’s configuration files, while the latter is the default web root directory for Apache.

To navigate to the configuration file directory, use the following command:

cd /etc/apache2

2.1 Change Apache's Default Homepage

By default, Apache’s homepage is located in the `/var/www/html` directory. You can either change this directory or create a custom `index.html` file in the directory.

To change the default homepage directory, edit the configuration file with the following command:

sudo nano /etc/apache2/sites-available/000-default.conf

Locate the following line:

DocumentRoot /var/www/html

Modify `/var/www/html` to your desired directory, then save and exit.

Reload the Apache configuration:

sudo systemctl reload apache2

3. Install PHP

Use the following command to install PHP and its related modules:

sudo apt install php libapache2-mod-php php-mysql

After installation, check the PHP version with the following command:

php -v

4. Configure PHP

The PHP configuration file is located at `/etc/php/7.4/apache2/php.ini`. You should adjust the path based on the PHP version you have installed.

Edit the PHP configuration file with the following command:

sudo nano /etc/php/7.4/apache2/php.ini

In the file, you can adjust settings such as memory limits, file upload sizes, and other PHP configurations. After making changes, save and exit.

4.1 Restart Apache

After modifying the PHP configuration, restart the Apache service to apply the changes:

sudo systemctl restart apache2

5. Verify PHP Installation

In your browser, enter your server’s IP address or domain. If everything is configured correctly, you should see a page displaying PHP information, including version, configuration, and installed modules.

To test PHP, create a simple PHP file and place it in Apache’s default root directory. Use the following commands to create the file:

cd /var/www/html
sudo nano info.php

In the opened file, add the following content:

phpinfo();

Save and exit, then access `http://your-server-ip/info.php` in your browser. If everything is set up correctly, you will see the PHP information page.

After completing these steps, you have successfully set up the PHP development environment on Ubuntu (Apache + PHP). This will provide you with a powerful platform for developing and deploying PHP applications.