Current Location: Home> Latest Articles> Comprehensive Guide to Setting Up PHP on Linux

Comprehensive Guide to Setting Up PHP on Linux

gitbox 2025-08-04

Introduction

With the advancement of internet technology, PHP has become a mainstream server-side scripting language widely used in developing dynamic websites and applications. This guide will walk you through setting up a PHP environment on a Linux system, facilitating smooth development and deployment.

Preparation

Before starting the installation, it's recommended to update your Linux system to the latest version using the following command:

sudo apt-get update && sudo apt-get upgrade

Installing Apache Web Server

Apache is the most popular open-source web server. Install it using the following command:

sudo apt-get install apache2

Starting and Verifying Apache Service

After installation, start the Apache service with this command:

sudo systemctl start apache2

Then open your browser and enter http://localhost. If you see the default Apache welcome page, the server is installed successfully.

Installing PHP and Related Extensions

Next, install PHP and common modules by running:

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

Verifying PHP Installation

Create a test PHP file to confirm successful installation with:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Access http://localhost/info.php in your browser. If you see the PHP info page, PHP is installed correctly.

Installing MySQL Database

If your project requires database support, install MySQL server with:

sudo apt-get install mysql-server

Securing MySQL

After installation, run the security script to improve MySQL's security:

sudo mysql_secure_installation

Follow the prompts to set your password and other security options to protect your database.

Conclusion

By completing the above steps, you will have successfully set up a PHP development environment on your Linux server, including the core components Apache, PHP, and MySQL. This environment is suitable for developing and deploying dynamic websites and applications.

Further Learning Recommendations

To enhance your development efficiency, consider deepening your knowledge of PHP language features, frameworks, and Linux server administration. Continuous practice and learning will help you master web development skills more effectively.

FAQ and Support

If you encounter issues during the setup process, refer to official documentation or developer communities where you can find useful solutions and shared experiences.