LNMP is a software bundle that stands for Linux, Nginx, MySQL/MariaDB, and PHP. It is a popular stack for building web applications and hosting websites on Linux-based operating systems, particularly on Debian and Ubuntu.
If you need to set up a web server to host and serve dynamic websites, web applications, or content management systems (CMS) like WordPress, Drupal, or Joomla, the LNMP stack provides the necessary components for a robust hosting environment. There are several one key script to create the LNMP environment. However, most of the script will compile the source code on your system. It cost a lot of time. It can also runs in docker. However it needs lots of memory. Here I provide a method to install LNMP and transfer data very quickly without lots of memory.
Install LNMP
Here are the detailed steps to install the LNMP (Linux, Nginx, MySQL, PHP) stack on Debian Linux:
Step 1: Update the package lists
Before installing anything, it’s a good practice to update the package lists to ensure you have the latest package information:
sudo apt update && apt upgrade
Step 2: Install Nginx Web Server
sudo apt install nginx
After installation, you can verify that Nginx is running:
sudo systemctl status nginx
Step 3: Install MySQL/MariaDB Database Server
You can choose to install either MySQL or MariaDB (a compatible fork of MySQL). Here’s how to install MariaDB:
sudo apt install mariadb-server
During the installation, you’ll be prompted to set the root password for the MariaDB server. Make sure to choose a secure password.
After installation, run the following command to secure the installation:
sudo mysql_secure_installation
Step 4: Install PHP and PHP-FPM
PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation for Nginx. Install it along with the necessary PHP extensions:
sudo apt install php7-fpm php7-mcrypt php7-soap php7-openssl php7-gmp php7-pdo_odbc php7-json php7-dom php7-pdo php7-zip php7-mysqli php7-sqlite3 php7-apcu php7-pdo_pgsql php7-bcmath php7-gd php7-odbc php7-pdo_mysql php7-pdo_sqlite php7-gettext php7-xmlreader php7-xmlrpc php7-bz2 php7-iconv php7-pdo_dblib php7-curl php7-ctype
This will install PHP and many useful PHP modules.
Step 5: Configure Nginx to Work with PHP-FPM
Edit the Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
Find the location block for PHP files and uncomment the line that includes fastcgi-php.conf
:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
Save and exit the file.
Step 6: Restart Nginx and PHP-FPM
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
Step 7: Test the Installation
Create a PHP test file:
sudo nano /var/www/html/info.php
Add the following PHP code:
<?php
phpinfo();
Save and exit the file.
In your web browser, navigate to http://your_server_ip/info.php
. You should see the PHP information page, indicating that the LNMP stack is installed and working correctly.
That’s it! You now have the LNMP stack (Linux, Nginx, MySQL/MariaDB, PHP) installed and configured on your Debian system. You can proceed to install and configure your desired web applications or start developing your PHP projects.
Note: Make sure to remove the info.php
file after testing, as it can pose a security risk if left accessible on a production server. The total script is
sudo -i
apt update
apt upgrade
apt install nginx
apt install mariadb-server
apt install php7-fpm php7-mcrypt php7-soap php7-openssl php7-gmp php7-pdo_odbc php7-json php7-dom php7-pdo php7-zip php7-mysqli php7-sqlite3 php7-apcu php7-pdo_pgsql php7-bcmath php7-gd php7-odbc php7-pdo_mysql php7-pdo_sqlite php7-gettext php7-xmlreader php7-xmlrpc php7-bz2 php7-iconv php7-pdo_dblib php7-curl php7-ctype
mysql_secure_installation
Transfer web pages and databases.
Create a backup of the web pages and databases on the old server.
cd /usr/share/nginx/html/
tar -czvf /root/web.tar.gz ./
cd /var/lib/mysql
tar -czvf /root/db.tar.gz ./
#transfer them to the new server
cd
scp -P your_port web.tar.gz root@new_server:/root/
scp -P your_port web.tar.gz root@new_server:/root/
scp -P your_port /etc/nginx/nginx.conf root@new_server:/etc/nginx/nginx.conf
# restore the web pages and databases on the new server
cd /usr/share/nginx/html/
tar -xzvf /root/web.tar.gz ./
cd /var/lib/mysql/
tar -xzvf /root/db.tar.gz ./
#restart the web server
systemctl restart nginx
#it's done.