How to install Apache and PHP on Ubuntu 22.04
How to install Apache and PHP on Ubuntu 22.04
Install Apache2
To install Apache2, enter the following command at the terminal prompt:
sudo apt install apache2
implement SSL certificate
edit this file
nano /etc/apache2/sites-available/default-ssl.conf
Replace this:
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
To become like this:
SSLCertificateFile /etc/ssl/certs/server.pem
SSLCertificateKeyFile /etc/ssl/private/server.key
Upload your SSL certificate files to the above directories
Configuring the Apache SSL parameters
sudo nano /etc/apache2/conf-available/ssl-params.conf
Paste the following basic configuration into the newly created file:
SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLHonorCipherOrder On
Header always set X-Frame-Options DENY
Header always set X-Content-Type-Options nosniff
# Requires Apache >= 2.4
SSLCompression off
SSLUseStapling on
SSLStaplingCache "shmcb:logs/stapling-cache(150000)"
# Requires Apache >= 2.4.11
SSLSessionTickets Off
To allow HTTP (Port 80) and HTTPS (Port 443) traffic, use the "Apache Full" profile.
Check the profile information as follows:
sudo ufw app info "Apache Full"
The screen profile description will be displayed :
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web
server.
Ports:
80,443/tcp
After verifying the profile, enable it:
sudo ufw allow in "Apache Full"
How to configure Apache
At this point changes to the Apache configuration can be made.
Enable the mod_ssl and mod_headers modules:
sudo a2enmod ssl
sudo a2enmod headers
Enable reading of the SSL configuration created earlier:
sudo a2enconf ssl-params
Enable the default SSL Virtual Host:
sudo a2ensite default-ssl
Check that you have not made syntax errors in the Apache configuration files:
sudo apache2ctl configtest
If the message "Syntax OK" appears on the screen, proceed by restarting Apache:
sudo systemctl restart apache2
How to install PHP on Ubuntu 22.04
Once you've reviewed the prerequisites section and set up the requirements for this installation guide, let's dive right in. Follow the below steps to learn how to install PHP on Ubuntu 22.04 system.
#Step 1: Install Dependencies on Ubuntu
To get started, log into your server instance via SSH and update the system as follows.
sudo apt update
Next, install the required dependencies, which are essential in the installation of PHP.
sudo apt install software-properties-common apt-transport-https ca-certificates lsb-release
#Step 2: Install PHP on Ubuntu
There are two main ways of installing PHP on Ubuntu. You can install PHP from Ubuntu repositories using the APT package manager, as shown.
sudo apt install php
To confirm that PHP is installed, run the command:
php --version
Install optional packages
The following packages are optional, and can be installed if you need them for your setup.
-
PHP-CLI You can run PHP scripts via the Command Line Interface (CLI). To do this, you must first install the
php-clipackage. You can install it by running the following command:sudo apt install php-cli
-
PHP-CGI You can also execute PHP scripts without installing the Apache PHP module. To accomplish this, you should install the
php-cgipackage via this command:sudo apt install php-cgi
-
PHP-MySQL To use MySQL with PHP you should install the
php-mysqlpackage, like so:sudo apt install php-mysql
-
PHP-PgSQL Similarly, to use PostgreSQL with PHP you should install the
php-pgsqlpackage:sudo apt install php-pgsql