# Guide: Install Apache, PHP, and Deploy PHP Service with SQLite on Ubuntu VPS

This guide will walk you through the steps to install Apache, PHP, and deploy a PHP service using SQLite on your new Ubuntu VPS.

---

# 1. Update and Upgrade the System & installaion Code Server
Before starting, update the package lists and upgrade existing packages:
To install code-server (a self-hosted version of Visual Studio Code) on Ubuntu, follow these steps. Code-server allows you to run VS Code in the browser and access it remotely.

## 1.1 Update and Install Dependencies
First, ensure your system is up to date and install any required dependencies:

```bash
sudo apt update
sudo apt upgrade -y
sudo apt install curl -y
```
## 1.2 install.sh
The easiest way to install code-server is to use our install script for Linux, macOS and FreeBSD. The install script attempts to use the system package manager if possible.

To install, run:
```bash
curl -fsSL https://code-server.dev/install.sh | sh
```

Now visit http://127.0.0.1:8080. Your password is in ~/.config/code-server/config.yam
```bash
sudo systemctl enable --now code-server@$USER
```
Change the options in the config.yaml. 0.0.0.0:8888, password, and etc
```bash 
nano ~/.config/code-server/config.yaml
```
After change the configuration, restart the service
```bash
sudo systemctl restart code-server@$USER
```
## 1.3.1. Install UFW
First, install UFW if it's not already installed on your system:

```bash
sudo apt update
sudo apt install ufw -y
```
## 1.3.2. Check UFW Status
After installation, you can check the status of UFW:

```bash
sudo ufw status
```
By default, UFW is inactive, so it will show Status: inactive.

## 1.3.3. Allow SSH Connections
Before enabling UFW, allow SSH connections to avoid locking yourself out of the server:

```bash
sudo ufw allow ssh
```
Alternatively, you can specify the port for SSH (default is 22):

```bash
sudo ufw allow 22/tcp
```
## 1.3.4. Allow HTTP and HTTPS
Next, allow HTTP (port 80) and HTTPS (port 443) traffic through the firewall:

```bash
sudo ufw allow http
sudo ufw allow https
```
Or, you can manually specify the ports:

```bash
sudo ufw allow 80/tcp  # For HTTP
sudo ufw allow 443/tcp  # For HTTPS
```
## 1.3.5. Enable UFW
Once the necessary ports are allowed, you can enable UFW to start filtering traffic:

```bash
sudo ufw enable
```
## 1.3.6. Check UFW Status
To verify that UFW is active and the correct ports are open, run:

```bash
sudo ufw status verbose
```
This will show the list of allowed services and their associated ports. You should see something like:

```bash
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)
```

# 2. Install Apache Web Server
Install Apache by running the following command:

```bash
sudo apt install apache2 -y
```
## 2.1. Start and Enable Apache
Start the Apache service and enable it to start on boot:

```bash
sudo systemctl start apache2
sudo systemctl enable apache2
```
## 2.2. Check Apache Status
Verify that Apache is running:

```bash
sudo systemctl status apache2
Visit http://<your_server_ip> in your browser, and you should see the default Apache welcome page.
```

# 3. Install PHP and Required Modules
Install PHP and the required modules to run PHP with Apache:

```bash
sudo apt install php libapache2-mod-php php-sqlite3 -y
```
3.1. Check PHP Installation
Verify that PHP is installed by running:

```bash
php -v
```
This will display the installed PHP version.

# 4. Configure Apache to Use PHP
Ensure that Apache is configured to serve PHP files. The libapache2-mod-php package should automatically enable PHP, but you can confirm this by running:

```bash
sudo systemctl restart apache2
```

# 5. Clone the Repository to Your VPS
## 5.1: Clone from GitHub
If you want to clone the repository directly from GitHub, follow these steps:

Install Git (if not already installed):

```bash
sudo apt install git -y
```
Clone the GitHub repository to /var/www/html:

```bash
sudo git clone https://github.com/username/repository.git /var/www/html/
```
Replace https://github.com/username/repository.git with the URL of your GitHub repository.

# 6. Set Correct Permissions for Apache (www-data)
After cloning the repository, you need to ensure that Apache (which runs as the www-data user) can access and serve the files.

## 6.1 Change Ownership to www-data:

```bash
sudo chown -R www-data:www-data /var/www/html
```
This command changes the ownership of all files in the /var/www/html directory to the www-data user and group, which is the user under which Apache runs.

## 6.2 Set Correct Permissions for Files:

Optionally, you can ensure the correct read/write/execute permissions for the files:

```bash
sudo find /var/www/html -type d -exec chmod 755 {} \;  # Directories - rwxr-xr-x
sudo find /var/www/html -type f -exec chmod 644 {} \;  # Files - rw-r--r--
```
- Directories: 755 (read, write, and execute for the owner, read and execute for others).
- Files: 644 (read and write for the owner, read-only for others).

# 7. Configure Apache (Optional)
You may want to set up a virtual host if you need a custom domain or to ensure proper configuration for your service.

Example of a Virtual Host Configuration:
## 7.1 Create a Virtual Host File:

```bash
sudo nano /etc/apache2/sites-available/your-site.conf
```
## 7.2 Add the Virtual Host Configuration:

Replace your-site.com with your domain or IP address:

```bash 
apache
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ServerName your-site.com

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
```
## 7.3 Enable the Virtual Host:

```bash
sudo a2ensite your-site.conf
```
Enable mod_rewrite (if necessary):

If you're using .htaccess or need URL rewriting, enable the mod_rewrite module:

```bash
sudo a2enmod rewrite
```
Restart Apache:

```bash
sudo systemctl restart apache2
```