LAMP Stack Installation Guide
I posted this tutorial to have my own quick reference, so I can copy and paste the installation steps into the command line. Having this step-by-step guide to refer to makes it easy for me to quickly install the Linux, Apache, MySQL, and PHP (LAMP) development environment on Windows Subsystem for Linux (WSL), or a Linux system. Not only are the commands present, but I've made a few notes for various options to consider.
This guide assumes you are setting up a development server configuration. LAMP configuration for a production server; a live, publicly accessible website server configuration should be considered with much caution for enhanced security, and a production configuration should not be based on the LAMP configuration recommendations herein.
Order of Operations
Sometimes, it's important to install software in a specific sequence, one before the other, when a subsequent option depends on previously installed software. Let's start with the database. I've found it is useful to install the database server first, so it can be setup prior to any other installations that might require it be present, running, and configured.
For example, if you plan to install phpMyAdmin using your default package manager (e.g. Apt on Ubuntu / Yum on CentOS), you might find it advantageous to install and configure the MySQL / MariaDB first. The database server must be configured and ready when it's time for the package manager to perform database operations.
As always, be sure to update the package manager repo sources prior to executing the installations. Open your command line (aka BASH) and execute:
sudo apt update && sudo apt upgrade
RDBMS: Install MySQL / MariaDB
Here we install MariaDB client, server, common, and dbconfig-common (used later in phpMyAdmin installation, optional). Note: depending on your distribution, you might need to swap "mariadb" with "mysql" in commands.
sudo apt install mariadb-client mariadb-server mariadb-common dbconfig-common | tee ~/LAMP_install_mysql.txt
sudo service mariadb start
sudo mysql -u root
MySQL [(MariaDB)]> exit
Install Apache HTTP Server
sudo apt install apache2 --install-recommends -f | tee ~/LAMP_install_apache.txt
sudo service apache2 start
Install PHP
The recommendations here are intended for a Development environment. Do not make changes to php.ini in a live Production environment!
Update: This section installs the latest PHP available in your package manager. Substitute version numbers if needed.
Here we install PHP and common modules:
sudo apt install php-curl php-imagick php-zip php-xml php-mysqli php-pdo php-gd php-mbstring php-xdebug php | tee ~/LAMP_install_php.txt
sudo nano /etc/apache2/mods-enabled/dir.conf
sudo nano /etc/php/[your-version-number]/apache2/php.ini
sudo service mariadb restart
sudo service apache2 restart
Optional: Xdebug Setup & PHP Notes
If installing PHP as part of a Development server, consider adding Xdebug and related tools (dbgClient, dbgProxy) for step-debugging.
Also, modify upload_max_filesize and post_max_size in php.ini to accommodate large .sql imports (default max: 2MB / 8MB).
Install PHP FastCGI Process Manager (php-fpm) optionally for IDE compatibility:
sudo apt install php-fpm
Install phpMyAdmin
Installing phpMyAdmin is optional. Set the root password after installation:
sudo apt install phpmyadmin | tee ~/LAMP_install_phpmyadmin.txt
# hit [enter] to let dbconfig generate password
mysql -u root -p
MySQL [(MariaDB)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'y*o*u*r*p*a*s*s';
MySQL [(MariaDB)]> FLUSH PRIVILEGES;
MySQL [(MariaDB)]> exit
Install WordPress on the LAMP Stack
Install WordPress under your new LAMP server:
sudo mkdir -p /var/www/html/wpcurl --verbose
sudo chown www-data: /var/www/html/wpcurl --verbose
sudo chmod 755 -R /var/www/html/wpcurl --verbose
cd /var/www/html/wpcurl
curl http://wordpress.org/latest.tar.gz | sudo -u www-data tar zx -C /var/www/html/wpcurl
sudo mv ./wordpress/* . --verbose
Install SSH Server and Client (optional)
Optional: Install OpenSSH Server/Client to manage your LAMP server remotely:
sudo apt install openssh-client
sudo apt install openssh-server
sudo service ssh start && sudo service ssh status
ssh localhost
VNC using SSH for Remote Database Connection (optional)
Configure SSH tunneling to access MySQL/MariaDB remotely. See this guide for Windows. Linux users can refer to this tutorial.
Related: If using NGINX instead of Apache, check out this installation guide.