Current Location: Home> Latest Articles> Complete Guide to Setting Up a PHP Environment on Linux and Building a Simple Website

Complete Guide to Setting Up a PHP Environment on Linux and Building a Simple Website

gitbox 2025-06-27

Setting up a PHP development environment on a Linux system is a crucial first step for any web developer. Linux offers an open-source, stable, and efficient foundation for running PHP and deploying websites. This article provides a step-by-step guide to help you configure your environment and build a simple website using PHP on Linux.

Step 1: Install a Linux Distribution

First, choose and install a Linux distribution such as Ubuntu, CentOS, or Debian. The installation process typically involves:

# Download the ISO file for your chosen Linux distribution
# Create a bootable USB or DVD
# Install the Linux system and complete setup

Once installed, you’ll be ready to proceed with environment configuration using either a graphical interface or command-line terminal.

Step 2: Update the System

Before beginning any development work, it’s best to update the system to ensure it’s running the latest software versions. On Ubuntu, use the following commands:

sudo apt update
sudo apt upgrade

This will install the latest security patches and software updates to maintain system stability and compatibility.

Step 3: Install PHP and Required Extensions

Next, install PHP and commonly used modules using your system’s package manager. For Ubuntu:

sudo apt install php php-mysql php-cli php-curl

For CentOS:

sudo yum install php php-mysql php-cli php-curl

Once installed, verify the PHP version by running:

php -v

If the terminal displays the PHP version, the installation was successful.

Step 4: Configure a Web Server (Apache)

To run PHP files in your browser, you’ll need a web server. Apache is a widely used choice on Linux systems. To install and configure Apache:

sudo apt install apache2
sudo apt install libapache2-mod-php

Then start Apache and enable it to run at system boot:

sudo systemctl start apache2
sudo systemctl enable apache2

You can now open http://localhost in your browser to confirm Apache is running.

Step 5: Build a Simple PHP Website

With everything set up, let’s create a basic PHP page as a demo. Use the following command to open a new file:

sudo nano /var/www/html/index.php

Enter the following code in the file:

<?php
echo "Welcome to my simple website!";
?>

Save and close the file. Then, go to http://localhost/index.php in your browser, and you should see the message "Welcome to my simple website!"

Conclusion

By following these steps, you've successfully set up a PHP development environment on Linux and created a basic website. This process not only helps you understand how to install and configure PHP and Apache, but it also builds a solid foundation for deeper learning in web development.

As your skills grow, consider exploring advanced topics such as database integration, using frameworks like Laravel, or securing your web server for production deployment.