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

6 minute read
0

I want to manually install a Lamp (Linux, Apache, MySQL, PHP) stack on my Amazon Lightsail instance. How do I do this?

Resolution

Note: 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 Apache

Ubuntu 20.04 and 18.04, Debian 9 and 10

Run the following commands to install the Apache web server:

sudo apt update -y
sudo apt install apache2 -y

CentOS 7 and CentOS 8

Run the following commands to install the Apache web server:

sudo yum update -y
sudo yum install httpd wget -y
sudo systemctl enable httpd
sudo systemctl start httpd

Install PHP

Note: 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.04

Run the following commands to install PHP:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update -y
sudo apt install php8.0 php8.0-{fpm,mysql,curl,gd,mbstring,mysql,xml,mcrypt,zip,ldap} libapache2-mod-php8.0 -y

Debian 10 and Debian 9

Run the following commands to install PHP:

sudo apt install -y gnupg2 ca-certificates apt-transport-https software-properties-common
wget -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.list
sudo apt update
sudo apt install php8.0 php8.0-{fpm,mysql,curl,gd,mbstring,mysql,xml,mcrypt,zip,ldap} libapache2-mod-php8.0 -y

CentOS 7

Run the following commands to install PHP:

sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum install -y yum-utils
sudo yum-config-manager --enable remi-php80
sudo yum install php php-{fpm,mysql,curl,gd,mbstring,mysql,xml,mcrypt,zip,ldap} -y

CentOS 8

Run the following commands to install PHP:

sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm
sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo dnf module enable php:remi-8.0 -y
sudo yum install php php-{fpm,mysql,curl,gd,mbstring,mysql,xml,mcrypt,zip,ldap} -y

Enable the PHP-FPM module

The 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 9

Run the following commands to install PHP-FPM:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.0-fpm
sudo a2dismod php8.0
sudo systemctl enable php8.0-fpm
sudo service apache2 restart;sudo service php8.0-fpm restart

CentOS 7

1.    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.so
LoadModule mpm_event_module modules/mod_mpm_event.so

2.    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_bak
sudo 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 .php
DirectoryIndex index.php

3.    Restart Apache and PHP-FPM for the changes to take effect:

sudo systemctl restart php-fpm
sudo systemctl restart httpd

CentOS 8

1.    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.conf

2.    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_bak
sudo 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 .php
DirectoryIndex index.php

3.    Run the following command on the server to allow Apache to make outbound connections:

sudo /usr/sbin/setsebool -P httpd_can_network_connect 1

4.    Restart Apache and PHP-FPM for the changes to take effect:

sudo systemctl restart php-fpm
sudo systemctl restart httpd

Install MariaDB

Install 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 9

sudo apt install mariadb-server -y

CentOS 8 and CentOS 7

sudo yum install MariaDB-server MariaDB-client -y
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

3.    Run the following command to complete the MariaDB secure installation to set the root password:

sudo mysql_secure_installation

4.    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 user

To 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.04

1.    Run the following command:

sudo usermod -a -G www-data ubuntu

2.    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/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;

Debian 10 and 9

1.    Run the following command:

sudo usermod -a -G www-data admin

2.    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/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;

CentOS 8 and 7

1.    Run the following command:

sudo usermod -a -G apache centos

2.    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/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;

Your site is ready

Access your instance's public IP address on a web browser to see the default webpage. Your website is now ready for customization.


AWS OFFICIAL
AWS OFFICIALUpdated 2 years ago