What best practices should be followed when setting up a LAMP environment with PostgreSQL and PHP on a Linux system?

When setting up a LAMP environment with PostgreSQL and PHP on a Linux system, it is important to ensure that all necessary packages are installed, configure the PostgreSQL database connection in the PHP code, and secure the environment by setting proper permissions and firewall rules.

<?php
$host = 'localhost';
$database = 'dbname';
$user = 'username';
$password = 'password';
$dsn = "pgsql:host=$host;dbname=$database;user=$user;password=$password";

try {
    $pdo = new PDO($dsn);
    echo "Connected to PostgreSQL successfully";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>