Use PHP Scripts with Apache on Ubuntu 26.04
- 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.
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.
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.
To test from a Windows browser, you need the server’s IP address. Run this on the server:
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
- Open a terminal on the server, or connect with
ssh USERNAME@192.0.2.10. - Refresh the package list by running:A list of repositories scrolls past as Ubuntu checks them.Bash / Shell
sudo apt update
- Install PHP and the Apache module:Enter your password if prompted. The installer enables the PHP module for Apache on its own.Bash / Shell
sudo apt -y install php libapache2-mod-php
- Check that PHP installed: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.PHP
php -v
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
- Restart the web server so it loads the module:The command returns to the prompt without output when it works.Bash / Shell
sudo systemctl restart apache2
- Confirm Apache is running:Look for active (running) in green. PressCode
systemctl status apache2
Qto 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
- Open a new file in the nano editor:An empty editor screen opens.Bash / Shell
sudo nano /var/www/html/info.php
- Type this line into the empty file:PHP
<?php phpinfo(); ?>
- Press
Ctrl+O, thenEnter, to save. Nano shows a short “Wrote 1 line” message at the bottom. - Press
Ctrl+Xto 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
- On your Windows PC, open any browser.
- Go to
http://192.0.2.10/info.php. Replace the address with your server’s. - 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
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.- Delete the file:Bash / Shell
sudo rm /var/www/html/info.php
- 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.
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.
- Create the file:Bash / Shell
sudo nano /var/www/html/hello.php
- Enter this content:PHP
<?php
echo "Hello from PHP. Server time: " . date("Y-m-d H:i:s");
?> - Save with
Ctrl+OandEnter, then exit withCtrl+X. - 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.
- Change the owner of a single file, replacing USERNAME with your own account name:The command prints nothing when it works.Bash / Shell
sudo chown USERNAME:www-data /var/www/html/hello.php
- Check the result:The owner column shows USERNAME and the group column showsBash / Shell
ls -l /var/www/html
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.
- List the PHP configuration folders to confirm the exact name:The output shows one folder per installed PHP version.Bash / Shell
ls /etc/php
- Open the Apache copy of the file:Bash / Shell
sudo nano /etc/php/VERSION/apache2/php.ini
- Press
Ctrl+Wto search, type the setting you want (for exampledate.timezone), and pressEnterto jump to it. - Edit the value. Remove the leading semicolon, which marks a line as a comment, if the setting is commented out.
- Save with
Ctrl+O, then exit withCtrl+X. - 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
- List the enabled Apache modules that mention PHP:Expect one line that starts withPHP
apache2ctl -M | grep php
php_module. - If nothing appears, reinstall the module:Bash / Shell
sudo apt -y install --reinstall libapache2-mod-php
- Restart Apache:Bash / Shell
sudo systemctl restart apache2
- 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
- Read the newest lines of the Apache error log:Bash / Shell
sudo tail -n 20 /var/log/apache2/error.log
- Look for a line naming your script and a line number. A missing semicolon or unclosed bracket is the usual cause.
- 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
- Check that Apache is running with
systemctl status apache2. - If the UFW firewall (Ubuntu’s built-in firewall tool) is on, check its rules:Bash / Shell
sudo ufw status
- If port 80 is not allowed, open it:To undo it later, runBash / Shell
sudo ufw allow 80/tcp
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.
/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?
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.
No comments yet — be the first to share your thoughts!