the LAMP stack

Complete Step by Step Guide – Install LAMP Stack on Windows WSL or Linux

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) as your preferred means of database administration on your LAMP stack server environment, 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. If who’ve done this before, you might recall that dbconfig† will attempt to create a database, and establish MySQL database User Privileges on installation of the phpMyAdmin package. The MySQL server will need to be installed, configured and running for dbconfig to be able to do its job and make your life easier when the time comes!

As always, be sure to update the package manager repo sources prior to executing the installations So your very fist step is to open your command line (aka. BASH), and execute the following command: sudo apt update && sudo apt upgrade

RDBMS: Install MySQL / Install MariaDB

Here we install MariaDB client, server, common and dbconfig-common is used later in the phpMyAdmin installation setup (optional). It is not required to install phpMyAdmin. If you know what it is, then you know whether you need it. Note, depending on your distribution, you might need to swap the command terms “mariadb” with “mysql”, or vice versa. For example, I’ve performed installs where everything but starting the service responded to “mariadb”. (that is, the second line below, I’ve experienced having to change it to “service mysql start”.

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. Beware of making any changes to php.ini in a live, Production environment!

Update: This section has been modified to install the latest version of PHP available in your package manager. If you do not wish to install PHP 8.1, just substitute the corresponding lines below with the version of your choice. You should be able to leave the lines as they are here, but if your package manager (e.g. apt / yum) isn’t cooperating, you might try the following to force for the versions. PHP 7 = “php7.4” or “php7.3”, and PHP 8.x = “php8”. If you’re uncertain, just leave it as “php” and let the package manager decide for you.

Here we install PHP along with a few of the most common additional module options. If you are a WordPress developer, you might find that the extensions listed here should satisfy any missing PHP modules as might be shown under Site Health status information in your WordPress Dashboard.

The lines which begin with sudo nano /etc/... are simply reminders (see below‡) to indicate that it’s necessary to edit a few lines of code in the LAMP configuration files. (e.g. edit dir.conf to allow for setting index.php as the first default page that Apache will serve if no file is specified when browsing to a particular directory; edit php.ini to enable or disable PHP features and extensions, and adjust PHP specific settings like error messages and maximum file upload sizes. If you install Xdebug, you’ll need to edit php.ini to point to the Xdebug module, and configure your desired debugging settings). See Xdebug.org for details on debugging PHP applications.

‡If you’re installing PHP as part of a Development server, you’ll probably want to add Xdebug to the set of PHP extensions included in your configuration. While using Xdebug with PHP, you will also find dbgClient and dbgProxy to be a useful companions.

‡At this step, I always modify the php.ini file to increase upload_max_filesize and post_max_size, because I’ll later be importing at least one database from an existing project, and .sql files are often larger than the PHP default maximum of 2 MB. Observe that by default upload max is 2MB, and post max is 8MB: consider this when setting your new sizes, as you may receive warnings in some software if the new values are remarkably disproportionate

Install the PHP FastCGI Process Manager (php-fpm) for better performance and compatibility with some IDE’s (e.g. IntelliJ PHPStorm benefits from the PHP FastCGI for remote debugging with Xdebug). Add php-fpm to the list of selected packages in the first apt command below to include it in your installation. I initially had it included in the apt command with the packages selected here, but I decided to leave it up to your discretion as including it does further complicate the installation, and you might not require it for your development server. By default, php-fpm is not enabled even after this installation. Use your discretion whether you choose to install and enable FastCGI. Note: in the line below to edit php.ini, change [your-version-number] to either 7.4 , or 8.1 (most likely). You can also type whereis php at the BASH command line, and the stdout will show you the version numbers associated what has been installed on your system.

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

Install phpMyAdmin

Pay close attention to the order of commands here. After phpMyAdmin is installed, you’ll need to set the password for the root user. By default, after MySQL / MariaDB is installed, you’ll be able to log in without a password. The reason we wait until now to set the root password is to accommodate the phpMyAdmin installation wizard. After phpMyAdmin is installed (or if you choose not to install it), you should set the root password, as shown here.

Note: Installing phpMyAdmin is optional. If you don’t need to install phpMyAdmin you can skip this and have a fully functional LAMP stack. If you need to work with databases, but don’t need all of what phpMyAdmin offers, or you simply don’t want to install it, I recommend Adminer as an alternative which requires no installation.

sudo apt install phpmyadmin | tee ~/LAMP_install_phpmyadmin.txt
#
# hit [enter] when prompted for a phpmyadmin password. 
# this allows dbconfig to generate a password for phpmyadmin later
#
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

Update: The following is an alternative initial approach to setting the MySQL/ MariaDB ROOT password. I recommend trying this version first. Either should work for you. Please feel free to send a note if you have any questions. Comments are open and subject to moderation (I get enough traffic that I have time to moderate).

sudo apt install phpmyadmin | tee ~/LAMP_install_phpmyadmin.txt
# 
# hit [enter] when prompted for a phpmyadmin password. let the system choose it randomly
#
# Note the difference from above. Here we are asking the terminal to request a password. But, a root password has not yet been set. The root password will be set with whatever is entered at the prompt to start the MySQL/ MariaDB command line.
mysql -u root -p

MySQL [(MariaDB)]> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('M*y*P*s*w*d');
MySQL [(MariaDB)]> FLUSH PRIVILEGES;
MySQL [(MariaDB)]> exit

Install WordPress on the LAMP Stack

This is the easiest and fastest way to install WordPress under your new LAMP stack server development environment:

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 https://wordpress.org/latest.tar.gz | sudo -u www-data tar zx -C /var/www/html/wpcurl
sudo mv ./wordpress/* . --verbose ## this line moves everything into the wpcurl directory

Install SSH Server and Client (optional)

Optional: Install SSH Server and Client for remote administration via Secure Shell. (e.g. if you have a LAMP stack on your local network, or LAN, you need to install the OpenSSH Server so you can connect to the Linux system and administer Apache, PHP, and MySQL / MariaDB from your preferred development desktop whether that’s Windows, Linux, or Mac). Note: the very last command is optional as it’s simply to test the SSH installation functionality.

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 via Virtual Network Computing (VNC) so that connections can be established remotely, for example, to port 3306 on a remote machine. After all, SSH connects on port 22, but MySQL’s default port is 3306. This is one way we get around it so to speak.

Do you need to tunnel a remote connection to MySQL or MariaDB via SSH? Check out this article for how to configure a VNC connection in Windows. You’ll need PuTTY or KiTTY if you’re a Windows user (note the TTY in the name. Why is that clever?). Linux users might prefer this Youtube video, or this VNC tutorial for Ubuntu.

Related reading: If need to install WordPress with NGINX instead of Apache for your HTTP server, there’s a nice installation guide and configuration tutorial here.

Whatchu do

One response to “Complete Step by Step Guide – Install LAMP Stack on Windows WSL or Linux”

  1. Sylvester DeMouser Avatar
    Sylvester DeMouser

    This is a really nice tutorial for installing a WordPress LAMP development server. Does this also work on Windows using WSL the Windows Subsystem for Linux?

Leave a Reply

Your email address will not be published. Required fields are marked *