Sling Academy
Home/PHP/How to set up PHP in Ubuntu

How to set up PHP in Ubuntu

Last updated: January 10, 2024

Introduction

Setting up PHP on an Ubuntu server is a crucial step for developers aiming to create dynamic websites or work with PHP-based applications. This tutorial will guide you through the process of installing PHP, configuring your environment, and testing your setup, giving you a robust foundation for your PHP projects.

Installing PHP

To begin, open your terminal, and execute the following command to update your package list:

sudo apt update 

With your packages up to date, you can install PHP by typing:

sudo apt install php 

Once the installation is complete, you can verify the PHP version using:

php -v 

Server Setup

Running PHP requires a web server. Apache2 is widely-used and can be installed with:

sudo apt install apache2 

After the installation, start the Apache2 service:

sudo systemctl start apache2 

Enable the service to start on boot:

sudo systemctl enable apache2 

PHP Configuration

Edit the PHP configuration file, php.ini, by finding it with:

sudo updatedb 
sudo locate php.ini 

Open the file with a text editor and make your required changes. For instance, to increase the maximum upload size:

upload_max_filesize = 10M 
post_max_size = 10M 

Testing PHP

Create a test PHP file info.php in the web server’s root directory:

sudo echo '
' > /var/www/html/info.php 

Now navigate to http://your-server-ip/info.php. You should see the PHP information page.

Database Management

Most PHP applications require a database. Install MySQL with:

sudo apt install mysql-server 
sudo mysql_secure_installation 

Connect to the MySQL console using:

sudo mysql -u root -p 

Advanced: PHP-FPM and Nginx

For better performance, consider using PHP-FPM with Nginx. Install Nginx and PHP-FPM:

sudo apt install nginx php-fpm 

Edit the Nginx configuration to use PHP-FPM by adding the following to your server block:

location ~ \.php$ { 
    include snippets/fastcgi-php.conf; 
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; 
} 

Restart Nginx:

sudo systemctl restart nginx 

Conclusion

Setting up PHP on Ubuntu involves a series of steps tailoring your server environment. By following this guide, you now have PHP installed and configured for web development, ready to build robust PHP-based applications.

Next Article: How to upgrade PHP to the latest version in Windows

Previous Article: How to set up PHP in MacOS

Series: Basic PHP Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array