Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
########################################################################
# Installing and Configuring Apache & Nginx with Multiple PHP Versions #
# Apache will run on port 8080 and will be behind Nginx #
# Nginx will serve as a reverse proxy for Apache Sites #
# Nginx will act as the primary web and caching server #
########################################################################
# Update Packages
sudo apt update && sudo apt upgrade -y
# Install Apache2 Server
sudo apt install apache2 -y
# Modify Apache Ports to Listen on Port 80 (http) and 8443 (https)
sudo sed -i 's/80/8080/' /etc/apache2/ports.conf
sudo sed -i 's/443/8443/' /etc/apache2/ports.conf
# Restart Apache service to listen on the new ports
sudo systemctl restart apache2
# Now, let's install Nginx as the primary web server to listen on Ports 80 & 443 for Non-SSL and SSL respectively
sudo apt install nginx -y
# Add the PHP Repository for Multi-PHP Version Installation
sudo add-apt-repository ppa:ondrej/php -y
# Install PHP Versions and their respective extensions
# PHP 5.6
sudo apt install -y \
libapache2-mod-php5.6 \
php5.6 \
php5.6-bcmath \
php5.6-bz2 \
php5.6-cli \
php5.6-common \
php5.6-curl \
php5.6-enchant \
php5.6-fpm \
php5.6-gd \
php5.6-intl \
php5.6-json \
php5.6-mbstring \
php5.6-mcrypt \
php5.6-mysql \
php5.6-opcache \
php5.6-readline \
php5.6-soap \
php5.6-sqlite3 \
php5.6-tidy \
php5.6-xml \
php5.6-xmlrpc \
php5.6-zip
# PHP 7.0
sudo apt install -y \
libapache2-mod-php7.0 \
php7.0 \
php7.0-bcmath \
php7.0-bz2 \
php7.0-cli \
php7.0-common \
php7.0-curl \
php7.0-enchant \
php7.0-fpm \
php7.0-gd \
php7.0-intl \
php7.0-json \
php7.0-mbstring \
php7.0-mcrypt \
php7.0-mysql \
php7.0-opcache \
php7.0-readline \
php7.0-soap \
php7.0-sqlite3 \
php7.0-tidy \
php7.0-xml \
php7.0-xmlrpc \
php7.0-xsl \
php7.0-zip
# PHP 7.1
sudo apt install -y \
libapache2-mod-php7.1 \
php7.1 \
php7.1-bcmath \
php7.1-bz2 \
php7.1-cli \
php7.1-common \
php7.1-curl \
php7.1-enchant \
php7.1-fpm \
php7.1-gd \
php7.1-intl \
php7.1-json \
php7.1-mbstring \
php7.1-mcrypt \
php7.1-mysql \
php7.1-opcache \
php7.1-readline \
php7.1-soap \
php7.1-sqlite3 \
php7.1-tidy \
php7.1-xml \
php7.1-xmlrpc \
php7.1-xsl \
php7.1-zip
# PHP 7.2
sudo apt install -y \
libapache2-mod-php7.2 \
php7.2 \
php7.2-bcmath \
php7.2-bz2 \
php7.2-cli \
php7.2-common \
php7.2-curl \
php7.2-enchant \
php7.2-fpm \
php7.2-gd \
php7.2-intl \
php7.2-json \
php7.2-mbstring \
php7.2-mysql \
php7.2-opcache \
php7.2-readline \
php7.2-soap \
php7.2-sqlite3 \
php7.2-tidy \
php7.2-xml \
php7.2-xmlrpc \
php7.2-xsl \
php7.2-zip
# PHP Version 7.3
sudo apt install -y \
libapache2-mod-php7.3 \
php7.3 \
php7.3-bcmath \
php7.3-bz2 \
php7.3-cli \
php7.3-common \
php7.3-curl \
php7.3-enchant \
php7.3-fpm \
php7.3-gd \
php7.3-intl \
php7.3-json \
php7.3-mbstring \
php7.3-mysql \
php7.3-opcache \
php7.3-readline \
php7.3-soap \
php7.3-sqlite3 \
php7.3-tidy \
php7.3-xml \
php7.3-xmlrpc \
php7.3-xsl \
php7.3-zip
# PHP Version 7.4
sudo apt install -y \
libapache2-mod-php7.4 \
php7.4 \
php7.4-bcmath \
php7.4-bz2 \
php7.4-cli \
php7.4-common \
php7.4-curl \
php7.4-enchant \
php7.4-fpm \
php7.4-gd \
php7.4-intl \
php7.4-json \
php7.4-mbstring \
php7.4-mysql \
php7.4-opcache \
php7.4-readline \
php7.4-soap \
php7.4-sqlite3 \
php7.4-tidy \
php7.4-xml \
php7.4-xmlrpc \
php7.4-xsl \
php7.4-zip
# PHP 8.0
sudo apt install php8.0 \
libapache2-mod-php8.0 \
php8.0-bcmath \
php8.0-bz2 \
php8.0-cli \
php8.0-common \
php8.0-curl \
php8.0-enchant \
php8.0-fpm \
php8.0-gd \
php8.0-imagick \
php8.0-intl \
php8.0-mbstring \
php8.0-memcache \
php8.0-memcached \
php8.0-mongodb \
php8.0-mysql \
php8.0-oauth \
php8.0-opcache \
php8.0-pgsql \
php8.0-phpdbg \
php8.0-pspell \
php8.0-psr \
php8.0-readline \
php8.0-redis \
php8.0-snmp \
php8.0-soap \
php8.0-solr \
php8.0-sqlite3 \
php8.0-tidy \
php8.0-uuid \
php8.0-xdebug \
php8.0-xml \
php8.0-xmlrpc \
php8.0-xsl \
php8.0-yaml \
php8.0-zip
# Miscellanous PHP Packages
sudo apt install -y \
php-pear \
php-xdebug
# Install Development Packages
sudo apt install php5.6-dev \
php7.0-dev \
php7.1-dev \
php7.2-dev \
php7.3-dev \
php7.4-dev \
php8.0-dev
# Restart all PHP FPM services
sudo systemctl restart php5.6-fpm
sudo systemctl restart php7.0-fpm
sudo systemctl restart php7.1-fpm
sudo systemctl restart php7.2-fpm
sudo systemctl restart php7.3-fpm
sudo systemctl restart php7.4-fpm
sudo systemctl restart php8.0-fpm
# Disable PHP 5.6 Apache Module & Enable PHP 7.4-FPM Module
sudo a2dismod php5.6
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.4-fpm
# To Switch PHP FPM Version for Apache the following commands should be used
# sudo a2disconf php<ver>-fpm && sudo a2enconf php<ver>-fpm
sudo systemctl reload apache2
# To enable Nginx Reverse Proxy for Apache, add the following to the Nginx specific site configuration
# location ~ \.php$ {
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_set_header Host $host;
# proxy_pass http://127.0.0.1:8080;
# }
# To switch CLI PHP version to another than the default use the following syntax
# sudo update-alternatives --set php /usr/bin/php<version>
# For Example (Replace 7.4 with your PHP version)
# sudo update-alternatives --set php /usr/bin/php7.4
# sudo update-alternatives --set phar /usr/bin/phar7.4
# sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4
# sudo update-alternatives --set php-config /usr/bin/php-config7.4
# sudo update-alternatives --set phpize /usr/bin/phpize7.4
########################################################################
# Now configure your Apache and Nginx Virtual Hosts / Sites #
# Enjoy! #
########################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment