Setting Up a PHP Development Environment on Ubuntu 24.04

Post Stastics

  • This post has 1382 words.
  • Estimated read time is 6.58 minute(s).

Introduction: Why PHP?

This article will guide you through setting up a PHP development environment on Ubuntu 24.04. We will start with a simple setup using the built-in PHP server and then move on to a more robust environment with Apache2. We will use Visual Studio Code (VS Code) or Notepad++ as our text editor.

Step-by-Step Tutorial

1. Preparing the Development Environment

First, ensure your system is up to date:

sudo apt update
sudo apt upgrade

2. Optional: Installing VS Code, Notepad++, Jed, or Micro Editor

Installing VS Code:

Setting Up a PHP Development Environment on Ubuntu 24.04

Introduction: Why PHP?

No matter what you think of PHP (Hypertext Preprocessor), it has been instrumental in powering a significant portion of the web. Its ease of use, extensive library support, and robust community make it an excellent choice for both beginners and experienced developers. Despite the rise of newer languages and frameworks, PHP continues to be relevant due to its continuous evolution and widespread adoption in web development.

This article will guide you through setting up a PHP development environment on Ubuntu 24.04. We will start with a simple setup using the built-in PHP server and then move on to a more robust environment with Apache2. We will use Visual Studio Code (VS Code) or Notepad++ as our text editor.

Step-by-Step Tutorial

1. Preparing the Development Environment

First, ensure your system is up to date:

sudo apt update
sudo apt upgrade

2. Installing VS Code or Notepad++

Installing VS Code:

sudo apt install software-properties-common apt-transport-https wget
wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
sudo apt update
sudo apt install code

Installing Notepad++:
Notepad++ is primarily a Windows application, but it can be run on Ubuntu using Wine.

  1. Install Wine: sudo dpkg --add-architecture i386 sudo apt update sudo apt install wine64 wine32
  2. Download and install Notepad++:
    sh wget https://notepad-plus-plus.org/repository/7.x/7.8.6/npp.7.8.6.Installer.exe wine npp.7.8.6.Installer.exe

3. Creating a Directory for PHP Projects

Let's create a directory to hold our PHP projects:

mkdir -p ~/projects/php
cd ~/projects/php

4. Installing PHP 8.x

Install PHP along with some common extensions:

sudo apt install php php-cli php-common php-mbstring php-xml php-zip

Verify the installation:

php -v

5. Creating and Serving an HTML File

Create a simple HTML file using nano:

nano hello.html

Add the following content to hello.html:

<!DOCTYPE html>
<html>
<head>
    <title>Hello, World!</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

Serve the HTML file using PHP's built-in server:

php -S localhost:5500

Open your browser and navigate to http://localhost:5500/hello.html to see your HTML page.

6. Creating and Serving a PHP File

Create a simple PHP file:

nano hello.php

Add the following content to hello.php:

<?php
echo "Hello, PHP World!";
?>

Serve the PHP file using PHP's built-in server:

php -S localhost:5500

Open your browser and navigate to http://localhost:5500/hello.php to see your PHP page.

7. Installing and Configuring Apache2

Install Apache2:

sudo apt install apache2

8. Adding PHP Support to Apache2

Install the PHP module for Apache:

sudo apt install libapache2-mod-php

Restart Apache to load the PHP module:

sudo systemctl restart apache2

9. Creating a Virtual Host

Create a new configuration file for your virtual host:

sudo nano /etc/apache2/sites-available/php-project.conf

Add the following content to the configuration file:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /home/your_username/projects/php

    <Directory /home/your_username/projects/php>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the new virtual host and rewrite module:

sudo a2ensite php-project.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

10. Updating the Hosts File

Add an entry to your hosts file to map a domain to your local server:

sudo nano /etc/hosts

Add the following line:

127.0.0.1   php-project.local

11. Serving an HTML File with Apache

Move your hello.html file to the project directory:

sudo mv hello.html /home/your_username/projects/php

Open your browser and navigate to http://php-project.local/hello.html to see your HTML page served by Apache.

12. Serving a PHP File with Apache

Move your hello.php file to the project directory:

sudo mv hello.php /home/your_username/projects/php

Open your browser and navigate to http://php-project.local/hello.php to see your PHP page served by Apache.

Conclusion

By following these steps, you've set up a PHP development environment on Ubuntu 24.04 using both PHP's built-in server and Apache2. This setup allows you to quickly develop and test your PHP applications. Whether you prefer the simplicity of PHP's built-in server or the robust features of Apache2, you have the tools necessary to start building dynamic web applications.

Should I continue?

sudo apt install software-properties-common apt-transport-https wget
wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
sudo apt update
sudo apt install code

Installing Notepad++:
Notepad++ is primarily a Windows application, but it can be run on Ubuntu using Wine.

  1. Install Wine: sudo dpkg --add-architecture i386 sudo apt update sudo apt install wine64 wine32
  2. Download and install Notepad++:
    sh wget https://notepad-plus-plus.org/repository/7.x/7.8.6/npp.7.8.6.Installer.exe wine npp.7.8.6.Installer.exe

3. Creating a Directory for PHP Projects

Let's create a directory to hold our PHP projects:

mkdir -p ~/projects/php
cd ~/projects/php

4. Installing PHP 8.x

Install PHP along with some common extensions:

sudo apt install php php-cli php-common php-mbstring php-xml php-zip

Verify the installation:

php -v

5. Creating and Serving an HTML File

Create a simple HTML file using nano:

nano hello.html

Add the following content to hello.html:

<!DOCTYPE html>
<html>
<head>
    <title>Hello, World!</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

Serve the HTML file using PHP's built-in server:

php -S localhost:5500

Open your browser and navigate to http://localhost:5500/hello.html to see your HTML page.

6. Creating and Serving a PHP File

Create a simple PHP file:

nano hello.php

Add the following content to hello.php:

<?php
echo "Hello, PHP World!";
?>

Serve the PHP file using PHP's built-in server:

php -S localhost:5500

Open your browser and navigate to http://localhost:5500/hello.php to see your PHP page.

7. Installing and Configuring Apache2

Install Apache2:

sudo apt install apache2

8. Adding PHP Support to Apache2

Install the PHP module for Apache:

sudo apt install libapache2-mod-php

Restart Apache to load the PHP module:

sudo systemctl restart apache2

9. Creating a Virtual Host

Create a new configuration file for your virtual host:

sudo nano /etc/apache2/sites-available/php-project.conf

Add the following content to the configuration file:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /home/your_username/projects/php

    <Directory /home/your_username/projects/php>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the new virtual host and rewrite module:

sudo a2ensite php-project.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

10. Updating the Hosts File

Add an entry to your hosts file to map a domain to your local server:

sudo nano /etc/hosts

Add the following line:

127.0.0.1   php-project.local

11. Serving an HTML File with Apache

Move your hello.html file to the project directory:

sudo mv hello.html /home/your_username/projects/php

Open your browser and navigate to http://php-project.local/hello.html to see your HTML page served by Apache.

12. Serving a PHP File with Apache

Move your hello.php file to the project directory:

sudo mv hello.php /home/your_username/projects/php

Open your browser and navigate to http://php-project.local/hello.php to see your PHP page served by Apache.

Conclusion

By following these steps, you've set up a PHP development environment on Ubuntu 24.04 using both PHP's built-in server and Apache2. This setup allows you to quickly develop and test your PHP applications. Whether you prefer the simplicity of PHP's built-in server or the robust features of Apache2, you have the tools necessary to start building dynamic web applications.

Should I continue?

Leave a Reply

Your email address will not be published. Required fields are marked *