Complete LNMP Stack Setup Guide: Linux + Nginx + MySQL + PHP from scratch
LNMP (Linux + Nginx + MySQL + PHP) is the classic server architecture for PHP websites. WordPress, Laravel, and most PHP applications run on LNMP. This guide walks through complete setup on a clean Linux server.
1. System Initialization
OS Selection
Ubuntu 22.04 LTS or 24.04 LTS recommended. LTS versions provide 5+ years of security updates.
Initial Security
# Update system
apt update && apt upgrade -y
# Create regular user
adduser deploy
usermod -aG sudo deploy
# Configure SSH key auth
ssh-keygen -t ed25519 -C "[email protected]"
# Add public key to ~/.ssh/authorized_keys
# Disable root login
sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
systemctl restart sshd
# Configure firewall
ufw allow OpenSSH
ufw allow 'Nginx Full'
ufw enable
2. Install Nginx
apt install nginx -y
nginx -v
systemctl status nginx
3. Install MySQL
apt install mysql-server -y
mysql_secure_installation
Create database and user:
CREATE DATABASE example_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON example_db.* TO 'example_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
4. Install PHP
# Ubuntu 24.04 default PHP 8.3
apt install php8.3-fpm php8.3-cli php8.3-mysql php8.3-curl \
php8.3-gd php8.3-mbstring php8.3-xml php8.3-bcmath php8.3-zip \
php8.3-redis php8.3-opcache -y
php -v
5. One-click Alternatives
LNMP.org一键包
wget https://soft.lnmp.com/lnmp/lnmp2.0.tar.gz
tar zxf lnmp2.0.tar.gz
cd lnmp2.0
./install.sh lnmp
Oneinstack
wget https://oneinstack.com/download/oneinstack-full.tar.gz
tar xzf oneinstack-full.tar.gz
cd oneinstack
./install.sh --nginx_option 1 --php_option 9 --mysql_option 2
6. Security Hardening
# Install Fail2Ban
apt install fail2ban -y
cat > /etc/fail2ban/jail.local << 'EOF'
[sshd]
enabled = true
maxretry = 5
bantime = 3600
[nginx-http-auth]
enabled = true
maxretry = 5
bantime = 3600
EOF
systemctl restart fail2ban
# Auto security updates
apt install unattended-upgrades -y
dpkg-reconfigure --priority=low unattended-upgrades
16IDC Takeaway
LNMP is the foundation of PHP website hosting. For beginners, use Oneinstack or LNMP.org one-click scripts for quick setup. 1GB RAM is sufficient for basic LNMP, 2GB+ for smooth WordPress operation. Check 16IDC server recommendations for best performance options.