Question: How do I install a LAMP stack manually on my Lightsail instance?

I want to manually install a Lamp (Linux, Apache, MySQL, PHP) stack on my Amazon Lightsail instance. How do I do this?ResolutionNote: The resolution steps cover most of the major Linux distributions available under the OS Only option in Amazon Lightsail. These include Ubuntu 20.04, Ubuntu 18.04, Debian 10 and 9, and CentOS 8 and 7.For information on installing a LAMP stack on Amazon Linux 2, see Tutorial: Install a LAMP web server on Amazon Linux 2.For information on installing a LAMP stack on Amazon Linux, see Tutorial: Install a LAMP web server on the Amazon Linux AMI.Install ApacheUbuntu 20.04 and 18.04, Debian 9 and 10Run the following commands to install the Apache web server:sudo apt update -ysudo apt install apache2 -yCentOS 7 and CentOS 8Run the following commands to install the Apache web server:sudo yum update -ysudo yum install httpd wget -ysudo systemctl enable httpdsudo systemctl start httpdInstall PHPNote: The following steps install PHP 8.0, which is the latest supported PHP package version. PHP 8.0 isn't present by default in most distributions. You install PHP 8.0 by enabling third-party repositories.Ubuntu 20.04 and Ubuntu 18.04Run the following commands to install PHP:sudo apt-get install software-properties-commonsudo add-apt-repository ppa:ondrej/phpsudo apt-get update -ysudo apt install php8.0 php8.0-{fpm,mysql,curl,gd,mbstring,mysql,xml,mcrypt,zip,ldap} libapache2-mod-php8.0 -yDebian 10 and Debian 9Run the following commands to install PHP:sudo apt install -y gnupg2 ca-certificates apt-transport-https software-properties-commonwget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.listsudo apt updatesudo apt install php8.0 php8.0-{fpm,mysql,curl,gd,mbstring,mysql,xml,mcrypt,zip,ldap} libapache2-mod-php8.0 -yCentOS 7Run the following commands to install PHP:sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpmsudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpmsudo yum install -y yum-utilssudo yum-config-manager --enable remi-php80sudo yum install php php-{fpm,mysql,curl,gd,mbstring,mysql,xml,mcrypt,zip,ldap} -yCentOS 8Run the following commands to install PHP:sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpmsudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpmsudo dnf module enable php:remi-8.0 -ysudo yum install php php-{fpm,mysql,curl,gd,mbstring,mysql,xml,mcrypt,zip,ldap} -yEnable the PHP-FPM moduleThe PHP-FPM module provides a much faster website. Although installing PHP-FPM is optional, it's a best practice to use it with most website building and management websites.Ubuntu 20.04 and 18.04, Debian 10 and 9Run the following commands to install PHP-FPM:sudo a2enmod proxy_fcgi setenvifsudo a2enconf php8.0-fpmsudo a2dismod php8.0sudo systemctl enable php8.0-fpmsudo service apache2 restart;sudo service php8.0-fpm restartCentOS 71.    Update the Apache configuration to use the mpm_event_module instead of the mpm_prefork_module:[centos@ip ~]# sudo vi /etc/httpd/conf.modules.d/00-mpm.conf # LoadModule mpm_prefork_module modules/mod_mpm_prefork.soLoadModule mpm_event_module modules/mod_mpm_event.so2.    Back up the current PHP configuration file in Apache and then create a new one with the following content:sudo mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf_baksudo vi /etc/httpd/conf.d/php.conf<Files ".user.ini">    <IfModule mod_authz_core.c>        Require all denied    </IfModule>    <IfModule !mod_authz_core.c>        Order allow,deny        Deny from all        Satisfy All    </IfModule></Files><FilesMatch \.php$>    SetHandler "proxy:fcgi://127.0.0.1:9000"</FilesMatch>AddType text/html .phpDirectoryIndex index.php3.    Restart Apache and PHP-FPM for the changes to take effect:sudo systemctl restart php-fpmsudo systemctl restart httpdCentOS 81.    Update the PHP-FPM configuration file so that the service listens to port instead of socket.sudo sed 's#listen = /run/php-fpm/www.sock#listen = 127.0.0.1:9000#g' -i /etc/php-fpm.d/www.conf2.    Back up the current PHP configuration file in Apache and then create a new one with the following content:sudo mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf_baksudo vi /etc/httpd/conf.d/php.conf<Files ".user.ini">    <IfModule mod_authz_core.c>        Require all denied    </IfModule>    <IfModule !mod_authz_core.c>        Order allow,deny        Deny from all        Satisfy All    </IfModule></Files><FilesMatch \.php$>    SetHandler "proxy:fcgi://127.0.0.1:9000"</FilesMatch>AddType text/html .phpDirectoryIndex index.php3.    Run the following command on the server to allow Apache to make outbound connections:sudo /usr/sbin/setsebool -P httpd_can_network_connect 14.    Restart Apache and PHP-FPM for the changes to take effect:sudo systemctl restart php-fpmsudo systemctl restart httpdInstall MariaDBInstall MariaDB version 10.5 using the MariaDB repository directly.1.    Run the following command to add the MariaDB yum repository (For all Linux distributions):curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash -s -- --mariadb-server-version="mariadb-10.5"2.    Install the MariaDB package:Ubuntu 20.04, Ubuntu 18.04, Debian 10 and Debian 9sudo apt install mariadb-server -yCentOS 8 and CentOS 7sudo yum install MariaDB-server MariaDB-client -ysudo systemctl start mariadb.servicesudo systemctl enable mariadb.service3.    Run the following command to complete the MariaDB secure installation to set the root password:sudo mysql_secure_installation4.    When prompted, set a password for the root account:Enter the current root password. By default, the root account doesn't have a password set.Press Enter.Press N to switch to unix_socket authentication.Press Y to set a password, and then enter a secure password twice. Make sure to store this password in a safe place.Press Y to remove the anonymous user accounts.Press Y to disable the remote root login.Press Y to remove the test database.Press Y to reload the privilege tables and save your changes.Add permissions for your website's folders to the default userTo transfer your website's instance using SCP/SFTP, it's a best practice that the corresponding user has permissions to access the /var/www directory. The following steps give the appropriate permissions for the default user in each Linux distribution. You can add the user to the Apache group, and then change the ownership of that directory to the default user and Apache group.Ubuntu 20.04 and 18.041.    Run the following command:sudo usermod -a -G www-data ubuntu2.    Log out and then log back in to pick up the new group. Then verify your membership by running "group":sudo chown -R ubuntu:www-data /var/wwwsudo chmod 2775 /var/wwwfind /var/www -type d -exec sudo chmod 2775 {} \;find /var/www -type f -exec sudo chmod 0664 {} \;Debian 10 and 91.    Run the following command:sudo usermod -a -G www-data admin2.    Log out and then log back in again to pick up the new group. Then verify your membership by running "group".sudo chown -R admin:www-data /var/wwwsudo chmod 2775 /var/wwwfind /var/www -type d -exec sudo chmod 2775 {} \;find /var/www -type f -exec sudo chmod 0664 {} \;CentOS 8 and 71.    Run the following command:sudo usermod -a -G apache centos2.    Log out and then log back in again to pick up the new group. Verify your membership by running "group":sudo chown -R centos:apache /var/wwwsudo chmod 2775 /var/wwwfind /var/www -type d -exec sudo chmod 2775 {} \;find /var/www -type f -exec sudo chmod 0664 {} \;Your site is readyAccess your instance's public IP address on a web browser to see the default webpage. Your website is now ready for customization.