How do I manually install a LAMP stack 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.

Resolution

Note: The following resolution is for most of the major Linux distributions that are 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 about how to install a LAMP stack on Amazon Linux 2, see Install LAMP web server on Amazon Linux 2.

For information about how to install a LAMP stack on Amazon Linux, see Install LAMP web server on Amazon Linux.

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 the latest package version, PHP 8.0. By default, PHP 8.0 isn't present in most distributions. To install PHP 8.0, turn on 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

Turn on the PHP-FPM module

The PHP-FPM module provides a faster website. Although PHP-FPM is optional, it's a best practice to use it for most website creation and management.

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 with the MariaDB repository.

  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. Store this password in a safe place.
    Press Y to remove the anonymous user accounts.
    Press Y to turn off the remote root login.
    Press Y to remove the test database.
    Press Y to reload the permissions tables and save your changes.

Add permissions for your website's folders to the default user

To transfer your website's instance with SCP/SFTP, as a best practice, set permissions for the other user to access the /var/www directory. For instructions on how to set permissions, see the To set file permissions section of Step 1: Prepare the LAMP server. The following steps give the required permissions for the default user in each Linux distribution. You can add the user to the Apache group, and then change the ownership of the directory to the default user and Apache group.

Complete the following steps:

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, run the group command to verify your membership:

    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 to pick up the new group. Then, run the group command to verify your membership:

    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 to pick up the new group. Run the group command to verify your membership:

    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 {} \;

View the default webpage

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

AWS OFFICIAL
AWS OFFICIALUpdated 9 months ago