# 🖥️ Local LAMP Stack + WordPress Setup Guide (Ubuntu/Debian)

This guide walks you through installing:

* **Apache** (Web Server)
* **MariaDB** (Database Server)
* **PHP** (Server-side language)
* **WordPress** (CMS)
* **phpMyAdmin** (Database GUI)

---

# 1️⃣ Install LAMP Stack

## 1. Install Apache

```bash
sudo apt update
sudo apt install apache2
```

Verify Apache is running:

```
http://localhost
```

---

## 2. Install MariaDB

```bash
sudo apt install mariadb-server
```

Secure your installation:

```bash
sudo mariadb-secure-installation
```

---

## 3. Install PHP + Required Extensions

```bash
sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip php-intl
```

Restart Apache:

```bash
sudo systemctl restart apache2
```

---

# 3️⃣ Create WordPress Database

## 1. Login to MariaDB

```bash
sudo mysql -u root -p
```

## 2. Create Database

```sql
CREATE DATABASE wordpress_db;
EXIT;
```

## 3. (Optional) Create Dedicated WordPress User

```sql
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```

⚠️ Recommended: Do NOT use root for WordPress.

---

# 2️⃣ Install WordPress

## 1. Go to Apache Web Root

```bash
cd /var/www/html
```

## 2. Download WordPress

```bash
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzf latest.tar.gz
sudo rm latest.tar.gz
```

## 3. Set Proper Permissions

```bash
sudo chown -R www-data:www-data wordpress
sudo find wordpress -type d -exec chmod 755 {} \;
sudo find wordpress -type f -exec chmod 644 {} \;
```

## 4. Open WordPress Installer

```
http://localhost/wordpress
```

---

# 4️⃣ Increase PHP Upload Limits

## 1. Find Your php.ini File

```bash
php --ini
```

Edit it (example for PHP 8.x):

```bash
sudo nano /etc/php/8.x/apache2/php.ini
```

Increase values:

```ini
upload_max_filesize = 256M
post_max_size = 256M
memory_limit = 512M
max_execution_time = 300
max_input_time = 300
```

Restart Apache:

```bash
sudo systemctl restart apache2
```

---

## Optional: Increase WordPress Memory

Inside `wp-config.php`:

```php
define('WP_MEMORY_LIMIT', '512M');
```

---

## Verify PHP Configuration

Create test file:

```bash
sudo nano /var/www/html/info.php
```

Add:

```php
<?php phpinfo(); ?>
```

Visit:

```
http://localhost/info.php
```

Search for:

* `upload_max_filesize`
* `post_max_size`

⚠️ Delete this file after testing for security:

```bash
sudo rm /var/www/html/info.php
```

---

# 5️⃣ Disable Apache Default Page

Remove default file:

```bash
sudo rm /var/www/html/index.html
```

---

# 6️⃣ Disable Directory Index Browsing

## 1. Edit Apache Config

```bash
sudo nano /etc/apache2/apache2.conf
```

Find:

```apache
<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
```

Change to:

```apache
<Directory /var/www/>
    Options -Indexes +FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
```

`-Indexes` disables directory browsing.

To enable Directory index browsing for sub-directory add:

```apache
<Directory /var/www/wordpress/SUB-DIRECTORY>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
```

`SUB-DIRECTORY` is the sub-directory to browse.

Restart Apache:

```bash
sudo systemctl restart apache2
```

Now visiting:

```
http://localhost
```

Will show:

```
Forbidden
You don't have permission to access this resource.
```

Your WordPress site will still work at:

```
http://localhost/wordpress
```

---

# 7️⃣ Fix Divi / Permalink / REST API Issues

## 1. Enable mod_rewrite

```bash
sudo a2enmod rewrite
sudo systemctl restart apache2
```

Allow `.htaccess` overrides:

Edit:

```bash
sudo nano /etc/apache2/apache2.conf
```

Change:

```apache
<Directory /var/www/>
    AllowOverride None
```

To:

```apache
<Directory /var/www/>
    AllowOverride All
```

Restart Apache:

```bash
sudo systemctl restart apache2
```

---

## 2. Reset WordPress Permalinks

Go to:

```
Settings → Permalinks
```

Click **Save Changes**.

---

## 3. Test REST API

```bash
curl http://localhost/wordpress/wp-json/
```

Results:

| Status | Meaning           |
| ------ | ----------------- |
| 404    | mod_rewrite issue |
| 403    | Permission issue  |
| 500    | PHP error         |

---

## 4. Verify Site URLs (After Migration)

Inside database:

```sql
SELECT option_name, option_value
FROM wp_options
WHERE option_name IN ('siteurl','home');
```

They must match exactly:

```
http://localhost/wordpress
```

---

# 8️⃣ Install phpMyAdmin

## 1. Install

```bash
sudo apt update
sudo apt install phpmyadmin
```

---

## 2. Verify Apache Config

Check:

```bash
ls /etc/apache2/conf-available/
```

If `phpmyadmin.conf` does not exist, We need to create the link manually:

```bash
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
```

Enable it:

```bash
sudo a2enconf phpmyadmin
sudo systemctl reload apache2
```

Access:

```
http://localhost/phpmyadmin
```

---

# ✅ Final Checklist

* Apache running
* MariaDB secured
* WordPress installed
* Proper permissions set
* Upload limits configured
* mod_rewrite enabled
* Directory browsing disabled
* phpMyAdmin accessible
