Install a LEMP Stack (Linux, Nginx, MySQL, PHP)

#!/bin/bash
############################
# nano nginx-installer.sh  //Paste this script
# chmod +x nginx-installer.sh
# bash ./nginx-installer.sh
####################################
set -e
echo "============================================"
echo " One-Click LEMP Installer Script"
echo " by JangSeo | MIT Licensed"
echo "============================================"
if [[ "$EUID" -ne 0 ]]; then
  echo "❌ Please run this script as root"
  exit 1
fi
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$ID
    VER=$VERSION_ID
else
    echo "❌ Unsupported OS"
    exit 1
fi
if [[ "$OS" != "ubuntu" && "$OS" != "debian" ]]; then
    echo "❌ Only Ubuntu and Debian are supported."
    exit 1
fi
echo "🔧 Updating packages..."
apt update -y
apt-get install nano wget perl curl sudo -y
PHP_PACKAGES="php php-mysql php-cli php-curl php-gd php-mbstring php-xml libmcrypt-dev libreadline-dev mcrypt php-common php-mysql php-imagick php-json php-opcache php-soap php-dev php-cgi php-zip php-intl php-bcmath php-pear unzip"
apt install -y $PHP_PACKAGES
curl -o setup-repos.sh https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh
sh setup-repos.sh
apt -y install webmin --install-recommends
echo "✅ Webmin configured."
echo "🌐 Installing Nginx..."
sudo apt install nginx -y
echo "🧠 Installing MySQL..."
sudo apt install mariadb-server -y
mariadb-secure-installation
apt -y purge Apache2* bind* exim* ufw firewalld libapache2-mod-php*
apt -y autoremove
systemctl restart nginx
echo "✅ Nginx, webmin, mariadb installed successfully!"
nginx -t
# Get PHP version
PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')
# Prompt for domain name
read -rp "Enter your domain name (e.g. example.com): " DOMAIN
# Setup web root
sudo mkdir -p /var/www/$DOMAIN
sudo chown -R $USER:$USER /var/www/$DOMAIN
# Create sample PHP file
echo "" | sudo tee /var/www/$DOMAIN/index.php
# Create Nginx server block
NGINX_CONFIG="/etc/nginx/sites-available/$DOMAIN"
echo "📦 Configuring Nginx..."
sudo bash -c "cat > $NGINX_CONFIG" < server {
    listen 80;
    server_name $DOMAIN www.$DOMAIN;

    root /var/www/$DOMAIN;
    index index.php index.html index.htm;

    location / {
        try_files \$uri \$uri/ =404;
    }

    location ~ \.php\$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php${PHP_VERSION}-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}
EOL

sudo ln -s $NGINX_CONFIG /etc/nginx/sites-enabled/

# Unlink default nginx host
sudo unlink /etc/nginx/sites-enabled/default

# Test and reload nginx to effect changes
sudo nginx -t && sudo systemctl reload nginx

echo "✅ LEMP stack installed for $DOMAIN"


# Uncomment out the echo < echo <
# Optional SSL setup
read -p "Do you want to install a free SSL certificate with Let's Encrypt (y/n)? " ssl_choice

if [[ "$ssl_choice" == "y" || "$ssl_choice" == "Y" ]]; then
    echo "🔐 Installing Certbot..."
    sudo apt install certbot python3-certbot-nginx -y
    sudo certbot --nginx -d $DOMAIN -d www.$DOMAIN
fi

echo "🎉 Done! Visit http://$DOMAIN (or https:// if SSL was added)"

EOF


Posted : Friday 15th of May 2026 @ 04:53 AM— Tags : new-nginx