home, PHP, updated

Ubuntu LAMP + Domain + SSL setup guide

1. Update and Upgrade System

Always start with updating your server to ensure all packages are current.

sudo apt update && sudo apt upgrade -y

2. Install Apache Web Server

Apache serves your web pages.

sudo apt install apache2 -y

Enable and start Apache:

sudo systemctl enable apache2
sudo systemctl start apache2

Check status:

sudo systemctl status apache2

Allow Apache through firewall:

sudo ufw allow 'Apache Full'
sudo ufw enable
sudo ufw status

3. Install PHP

PHP is the server-side scripting language used by many web apps.

sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-json php-cgi php-xml php-mbstring php-zip php-bcmath php-intl php-gd php-common -y

Verify PHP installation:

php -v

Restart Apache to load PHP:

sudo systemctl restart apache2

4. Install MySQL Server

MySQL is the database server used to store and manage data.

sudo apt install mysql-server -y

Run secure installation:

sudo mysql_secure_installation

Login to MySQL shell:

sudo mysql -u root -p

Create a new database and user (example):

CREATE DATABASE mydb;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

5. Install phpMyAdmin

phpMyAdmin is a web-based MySQL management tool.

sudo apt install phpmyadmin -y

Enable PHP extensions:

sudo phpenmod mbstring
sudo systemctl restart apache2

Link phpMyAdmin to Apache root:

sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

Access phpMyAdmin at:

http://your_server_ip/phpmyadmin

6. Configure Domain

After purchasing a domain, add these DNS records in your domain panel:

TypeHostValue (Your Server IP)TTL
A@xx.xx.xx.xxAuto
Awwwxx.xx.xx.xxAuto

Create web root and permissions:

sudo mkdir -p /var/www/yourdomain.com/public_html
sudo chown -R $USER:$USER /var/www/yourdomain.com/public_html
sudo chmod -R 755 /var/www/yourdomain.com

Create Apache config file:

sudo nano /etc/apache2/sites-available/yourdomain.com.conf

Paste this:

<VirtualHost *:80>
    ServerAdmin admin@yourdomain.com
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/yourdomain.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the site and reload Apache:

sudo a2ensite yourdomain.com.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Test configuration:

sudo apache2ctl configtest

7. Install SSL (Let’s Encrypt)

Let’s Encrypt provides free SSL certificates using Certbot.

sudo apt install certbot python3-certbot-apache -y

Obtain and install SSL certificate:

sudo certbot --apache -d yourdomain.com -d www.yourdomain.com

Test auto-renewal:

sudo certbot renew --dry-run

8. Restart All Services

sudo systemctl restart apache2
sudo systemctl restart mysql

9. Verify Setup

  • Open your domain: https://yourdomain.com
  • Access phpMyAdmin: https://yourdomain.com/phpmyadmin
  • Check SSL lock icon in browser

Related Articles

post a comment