Ubuntu 18.04降级至16.04或解决Apache2上PHP运行问题
Hey there! Let's break down your two options clearly—downgrading to Ubuntu 16.04, or fixing the Apache-PHP issue on 18.04. I'd strongly recommend the second option since downgrading carries significant risks, but I'll cover both paths for you.
Option 1: Downgrade Ubuntu 18.04 to 16.04 (Not Recommended)
Ubuntu doesn't officially support downgrading between major releases, so this process is prone to dependency conflicts, broken packages, or even a non-bootable system. Backup all critical data first—this is non-negotiable.
- First, back up your important files (home directory, configs, web content) to an external drive using a tool like
tar:sudo tar -czvf /path/to/external/storage/server-backup.tar.gz /home /etc /var/www - Update your apt sources to point to Ubuntu 16.04 (code name
xenial) instead of 18.04 (bionic):sudo sed -i 's/bionic/xenial/g' /etc/apt/sources.list - Refresh the package list and start the downgrade process:
sudo apt update sudo apt dist-upgrade -y - Once the process finishes, reboot your server:
sudo reboot
Warning: If you encounter package conflicts during the downgrade, you may need to force remove problematic packages or even reinstall the system from scratch and restore your backup.
Option 2: Fix Apache2 PHP Execution on Ubuntu 18.04 (Recommended)
This is the safer, more sustainable path. Let's troubleshoot common issues step by step:
- Install and enable the PHP Apache module: Ubuntu 18.04 ships with PHP 7.2 by default. Install the module and enable it:
sudo apt install libapache2-mod-php7.2 sudo a2enmod php7.2 sudo systemctl restart apache2 - Test PHP parsing: Create a simple test file in your web root to verify PHP works:
Visit this file via your server's URL (e.g.,echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.phphttp://your-server-ip/info.php). If you see a detailed PHP info page, parsing is working. If you see raw PHP code, the module isn't enabled properly. - Check Apache configuration: Ensure Apache is set to handle
.phpfiles. Verify thephp7.2.conffile has this rule:<FilesMatch ".+\.ph(p[3457]?|t|tml)$"> SetHandler application/x-httpd-php </FilesMatch> - If using PHP-FPM: If you prefer using PHP-FPM instead of mod_php, install and configure it:
sudo apt install php7.2-fpm sudo a2enmod proxy_fcgi setenvif sudo a2enconf php7.2-fpm sudo systemctl restart apache2 php7.2-fpm - Check error logs: If problems persist, dig into the logs for clues:
- Apache error log:
sudo tail -n 20 /var/log/apache2/error.log - PHP-FPM log (if applicable):
sudo tail -n 20 /var/log/php7.2-fpm.log
- Apache error log:
内容的提问来源于stack exchange,提问作者pitwo

