Skip to content
Ubuntu Linux 🟡 Intermediate

Use PHP Scripts with Apache on Ubuntu 26.04

Richard
Written by
Richard
7 min read
Laptop beside a server rack showing an Ubuntu desktop with a terminal and browser, illustrating PHP scripts running on Apache.
Laptop beside a server rack showing an Ubuntu desktop with a terminal and browser, illustrating PHP scripts running on Apache.
Before you start
  • 37 steps
  • Needs administrator rights

Running PHP scripts with Apache on Ubuntu 26.04 takes three moves. Install Apache’s PHP module with sudo apt install php libapache2-mod-php, restart Apache, and put a .php file in /var/www/html. Apache then runs the script and sends the output to the browser.

PHP is a scripting language that builds web pages on the server before they reach the visitor. This guide assumes Apache is already installed and serving its default page on your Ubuntu 26.04 machine.

⚡ Quick Answer

To run PHP scripts with Apache on Ubuntu 26.04, install the Apache PHP module with sudo apt -y install php libapache2-mod-php, then restart Apache with sudo systemctl restart apache2. Save your script in /var/www/html, such as info.php, and open http://YOUR-SERVER-IP/info.php in a browser. A PHP Version table means PHP is working.

Advertisement

Before you start

Every command below needs administrator rights, so each one starts with sudo. Run them in the server’s terminal, or from Windows 11 over SSH (a secure remote login). Windows Terminal, PowerShell and Command Prompt all include the ssh client.

If you run Ubuntu inside WSL (Windows Subsystem for Linux), you can test there too. Nothing in this guide changes Windows itself.

Advertisement

To test from a Windows browser, you need the server’s IP address. Run this on the server:

💻Code
hostname -I

The command prints one or more addresses, such as 192.0.2.10. Use the first one that belongs to your local network, and write it down for the browser tests later.

Install PHP and the Apache PHP module

The libapache2-mod-php package is the Apache module, a plug-in that lets the web server run PHP code directly. The php package pulls in the PHP command-line program and the common files PHP needs.

Install the packages

  1. Open a terminal on the server, or connect with ssh USERNAME@192.0.2.10.
  2. Refresh the package list by running:
    🐧Bash / Shell
    sudo apt update
    A list of repositories scrolls past as Ubuntu checks them.
  3. Install PHP and the Apache module:
    🐧Bash / Shell
    sudo apt -y install php libapache2-mod-php
    Enter your password if prompted. The installer enables the PHP module for Apache on its own.
  4. Check that PHP installed:
    🐘PHP
    php -v
    The terminal prints the installed PHP version number and build details. Note the first two parts of the version, because a later section needs them.

That’s it. PHP and the Apache module are now installed, but Apache has not loaded the module yet.

Apache can run PHP only through certain processing modes. These modes, called MPMs, control how Apache handles incoming connections. The module package may switch Apache to the mode it needs during installation. That is normal, and you do not need to act on it.

Restart Apache

  1. Restart the web server so it loads the module:
    🐧Bash / Shell
    sudo systemctl restart apache2
    The command returns to the prompt without output when it works.
  2. Confirm Apache is running:
    💻Code
    systemctl status apache2
    Look for active (running) in green. Press Q to leave the status screen.

That’s it. Apache is running again with the PHP module loaded, so it is ready to run scripts.

Create a test PHP script

A short script that calls phpinfo() proves the setup works. That built-in function prints PHP’s settings as a web page. Apache serves files from /var/www/html by default, so the script goes there.

Write the file

  1. Open a new file in the nano editor:
    🐧Bash / Shell
    sudo nano /var/www/html/info.php
    An empty editor screen opens.
  2. Type this line into the empty file:
    🐘PHP
    <?php phpinfo(); ?>
  3. Press Ctrl + O, then Enter, to save. Nano shows a short “Wrote 1 line” message at the bottom.
  4. Press Ctrl + X to close nano.

That’s it. The test script now exists in the document root, and you can request it from a browser.

Open it in a browser

  1. On your Windows PC, open any browser.
  2. Go to http://192.0.2.10/info.php. Replace the address with your server’s.
  3. Check the page. A purple-and-grey table headed PHP Version means Apache is running your PHP code.

You should now have a working PHP setup on Apache. If the browser instead downloads the file or shows the raw <?php text, Apache is not processing PHP. Jump to the troubleshooting section below.

Delete the test file

⚠️WarningThe phpinfo() page exposes your PHP version, file paths and loaded modules. Anyone who finds it learns a lot about your server. Remove it once you have confirmed everything works.
  1. Delete the file:
    🐧Bash / Shell
    sudo rm /var/www/html/info.php
  2. Reload the address in your browser. A Not Found error appears, which is what you want.

That’s it. The test page is gone and no longer leaks details about your server.

Advertisement

Run your own PHP scripts

Any file ending in .php inside the document root runs as a script. Try a small page that prints the date.

  1. Create the file:
    🐧Bash / Shell
    sudo nano /var/www/html/hello.php
  2. Enter this content:
    🐘PHP
    <?php
    echo "Hello from PHP. Server time: " . date("Y-m-d H:i:s");
    ?>
  3. Save with Ctrl + O and Enter, then exit with Ctrl + X.
  4. Browse to http://192.0.2.10/hello.php. The page shows the greeting and the current server time. Refresh the page and the time changes, which shows the script runs on every request.

You should now have a dynamic page that Apache and PHP build fresh each time someone loads it.

Set file ownership for your own pages

Files you create with sudo belong to root. That is fine for testing. For a real site, give the web files to a regular account so you can edit them without sudo.

  1. Change the owner of a single file, replacing USERNAME with your own account name:
    🐧Bash / Shell
    sudo chown USERNAME:www-data /var/www/html/hello.php
    The command prints nothing when it works.
  2. Check the result:
    🐧Bash / Shell
    ls -l /var/www/html
    The owner column shows USERNAME and the group column shows www-data, the account Apache runs as.

That’s it. Your account now owns the file and can edit it directly, while Apache keeps read access through the group.

Change PHP settings

PHP reads its settings from a file called php.ini. The copy used by Apache sits in a folder named after your PHP version. Replace VERSION with the number that php -v printed, using the first two parts of the version.

  1. List the PHP configuration folders to confirm the exact name:
    🐧Bash / Shell
    ls /etc/php
    The output shows one folder per installed PHP version.
  2. Open the Apache copy of the file:
    🐧Bash / Shell
    sudo nano /etc/php/VERSION/apache2/php.ini
  3. Press Ctrl + W to search, type the setting you want (for example date.timezone), and press Enter to jump to it.
  4. Edit the value. Remove the leading semicolon, which marks a line as a comment, if the setting is commented out.
  5. Save with Ctrl + O, then exit with Ctrl + X.
  6. Restart Apache to apply the change:
    🐧Bash / Shell
    sudo systemctl restart apache2

That’s it. Apache now runs PHP with your new setting.

To undo a change, put the semicolon back, or restore the old value, and restart Apache again. Copy the file first with sudo cp /etc/php/VERSION/apache2/php.ini /etc/php/VERSION/apache2/php.ini.bak if you want a safe fallback.

Fix PHP files that do not run

The browser shows PHP code or offers a download

  1. List the enabled Apache modules that mention PHP:
    🐘PHP
    apache2ctl -M | grep php
    Expect one line that starts with php_module.
  2. If nothing appears, reinstall the module:
    🐧Bash / Shell
    sudo apt -y install --reinstall libapache2-mod-php
  3. Restart Apache:
    🐧Bash / Shell
    sudo systemctl restart apache2
  4. Hard-refresh the page in your browser with Ctrl + F5. This skips the cached copy of the page.

You should now see the PHP page render instead of downloading or showing raw code.

The page shows a blank screen or error 500

  1. Read the newest lines of the Apache error log:
    🐧Bash / Shell
    sudo tail -n 20 /var/log/apache2/error.log
  2. Look for a line naming your script and a line number. A missing semicolon or unclosed bracket is the usual cause.
  3. Fix the script and reload the page.

That’s it. Once the mistake is corrected, the page should load normally.

The browser cannot connect at all

  1. Check that Apache is running with systemctl status apache2.
  2. If the UFW firewall (Ubuntu’s built-in firewall tool) is on, check its rules:
    🐧Bash / Shell
    sudo ufw status
  3. If port 80 is not allowed, open it:
    🐧Bash / Shell
    sudo ufw allow 80/tcp
    To undo it later, run sudo ufw delete allow 80/tcp.

You should now be able to reach the server from your Windows browser.

What to do next

If scripts still fail, check the newest lines of the Apache error log, then compare them with the script’s line numbers. For a busier site, the next step is running PHP through PHP-FPM, a separate process manager that handles PHP outside Apache. That keeps PHP and the web server apart.

⚠️WarningAfter a major PHP upgrade, the folder under /etc/php changes name. Re-apply any custom php.ini edits to the new folder.

Have a question about this guide?

Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.

Did this guide fix your problem?

Richard

About the Author

Richard

Tech Writer, IT Professional

Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.

Advertisement

No comments yet — be the first to share your thoughts!

Leave a Comment

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