Skip to main content

Deploy PHP server

1. Create a DigitalOcean Account

  • Go to DigitalOcean and sign up.
  • Add a payment method to enable creating droplets (servers).

2. Create a Droplet

tutorial this is like a VM that host your website

  1. Log in to DigitalOcean.
  2. Click Create > Droplets.
  3. Choose an operating system:
  • Ubuntu (latest LTS version) is recommended.
  1. Choose a plan:
  • The Basic plan (like $4/month) is sufficient for small projects.
  1. Select a data center region.
  2. Add SSH keys (recommended) or set a root password for secure access.
  • I picked root pwd
  1. Click Create Droplet.

3. Connect to Droplet

  • Click on the console text on the right alt text

  • Otherwise, open cmd and use: ssh root@droplet_ip

4. Update the Server

Run the following commands to update the server:

sudo apt update
sudo apt upgrade -y

5. Install LAMP Stack (Linux, Apache, MySQL, PHP)

a) Install Apache

sudo apt install apache2 -y

Test by visiting http://droplet_ip. You should see the Apache default page.

b) Install PHP

sudo apt install php libapache2-mod-php php-mysql -y

c) Install MySQL

sudo apt install mysql-server -y

d) Check Apache status

sudo systemctl status apache2

If running, should see some green

If it's not running, you can start it with:

sudo systemctl start apache2

e) Check PHP

  1. Version
php -v
  1. Test Apache and PHP
sudo vim /var/www/html/info.php

Add the following content:

<?php
phpinfo();
?>

6. Access web

Visit http://droplet_ip/yourFolderName in a browser. Hello World should show up.

7. Copy PHP Project from Local to Droplet

scp -r .\MyWeb\ root@<droplet_ip>:/var/www/html/

8. Set Permissions (Optional)

Sometimes, you may need to adjust permissions to make sure Apache can read and execute your files:

sudo chown -R www-data:www-data /var/www/html/<your folder>
sudo chmod -R 755 /var/www/html/<your folder>

Make sure Apache running (see 5d)

9. Enable a Domain Name (Optional)

  1. Buy a domain and point it to your droplet's IP by creating an A record in your domain registrar's DNS settings.
  2. Update Apache configuration to recognize your domain:
sudo nano /etc/apache2/sites-available/000-default.conf

Change ServerName and DocumentRoot as needed, then restart Apache:

Copy code
sudo systemctl restart apache2

Troubleshooting MySQL (If needed)

  1. Are the connection data with db correct?

  2. Check Logs for More Information

sudo tail -n 50 /var/log/mysql/error.log
  1. Check System Memory Usage free -h you might need to free up memory by closing unnecessary applications or adding more swap space.

  2. Add Swap Space (If needed)

  • Create a swap file:
sudo fallocate -l 2G /swapfile

This will create a 2GB swap file. Adjust the size (2G) based on your available disk space.

  • Set the correct permissions:
sudo chmod 600 /swapfile
  • Make the file a swap space:
sudo mkswap /swapfile
  • Enable the swap file:
sudo swapon /swapfile
  • Make the swap file permanent by adding it to /etc/fstab:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
  1. Restart MySQL After adding swap space, try restarting the MySQL service again:
sudo systemctl restart mysql

Check the status afterward:

sudo systemctl status mysql
  1. If memory is still an issue, review MySQL Configuration Edit sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf For example, you could adjust the following settings:
innodb_buffer_pool_size = 128M  # Reduce InnoDB buffer pool size
key_buffer_size = 64M # Reduce key buffer size
max_connections = 100 # Reduce the number of allowed connections

After saving the changes, restart MySQ as above

10. Secure Your Website with HTTPS

  1. Install Certbot for Let's Encrypt:
sudo apt install certbot python3-certbot-apache -y

Get an SSL certificate:

sudo certbot --apache

Follow prompts to secure your domain.