# Setting Up a Web Server on Raspberry Pi
#### This tutorial use a headless approach (via wireless and SSH), you will only need a wi-fi to connect with your device.
#### You might want to find other tutorial to connect your device first for a wired connection,.
## Pre-request: Setting up raspberry Pi
### Step 1: Create image file for raspberry Pi
The easiest way I can found, is to download a easy-setup tool from their official website:
> [Raspberry Pi Imager](https://www.raspberrypi.com/software/)
Download the software for your host system, now open it up!

Now let's prepare the disk for pi to use.
> Be aware the following steps will format the choosen storage.
a) From here, choose the device you are using and the desired system to use.
In my case, I have a ***Raspberry Pi Zero 2 W*** and willing to run ***ubuntu*** with it.
> Find which OS works best with you, I had a hard time establishing connection with raspberry pi OS, and I find ubuntu to be more consisten on my device.
b) Next, find your storage to use as disk, and select "NEXT"

c) I'm connecting my device via LAN wi-fi with SSH, check settings to know how you're going connect. You may find more detailed setting tutorial here: [Connecting Raspberry Pi via Wireless Lan](https://www.raspberrypi.com/documentation/computers/getting-started.html#install-using-imager)
d) Click "YES" and wait until imager done its job. Take a cup of tea or see what we're going to do, all the "fun" comes after it!
### Step 2: Connecting to Raspberry Pi
#### I had kept my laptop and pi in the same LAN, you'll need to tweak more other settings to remote control it.
a) To use SSH connection more convenient, I use PuTTy as my tool, you can find their installation page here: [PuTTy Release Page](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html)
b) (optional) If you want to find your device using its hostname, download [Bonjour Print Services](https://support.apple.com/en-us/106380) first so your computer can identify it. (yeah... so weird it needs an app from Apple)
c) Now enter the IP/hostname of your Raspberry Pi, you may add `<username>@` before it to login with certain user. You may change font size or modify configuration before openning connection.
> If you have access to the admin page of your router, you might be able to find list of your devices IP there, or try connecting using hostname with both `raspberrypi` or `raspberrypi.local`

d) Enter the password and you're in the system!


### Quick mention on port forwarding
extra) To enable your device to being able to access anywhere on the internet, see [Port Forwarding](https://en.wikipedia.org/wiki/Port_forwarding), also remember to [let your application pass the firewall](https://ubuntu.com/server/docs/firewalls), this might need some basic knowledge on networking to do it.
> As I failed to setup port forwarding probably because my network provider denies it, I am going to keep this section short.
#### In the first lab, we will use Apache, PHP, SQLite to make a web server running.
#### You can choose any database system you want to run on your device, since my [raspberry pi zero 2 w](https://www.raspberrypi.com/products/raspberry-pi-zero-2-w/) have very limited computing power, I choosed SQLite for minimun impact on performance and better portability.

> MySQL is too harsh for Pi zero 2...
## Lab 1: Running a PHP server with database (SQLite)
### Step 1: Install packages
Before we do anything, let's install the packages we are going to use.
1. `(optional) sudo apt-get upgrade` *:upgrade existing packages*
2. `sudo apt install apache2` *:install apache2 for hosting server*
3. `sudo apt install php libapache2-mod-php` *:install php & mod to run apache with php*
4. `sudo apt install sqlite3` *:database system to use*
5. `sudo apt install php8.3-sqlite3` *:classes for php to use sqlite*
Your apache service should be running automatically after installation.
If not, check out `service` or `systemctl` commands to restart your services or check their statues.
Fire up a browser and check if you can connect to your device:
e.g. Type in your IP/domain name of your device in the search bar, or use `localhost` if you're not performing headless setup.


If you see this page, that means you're apache works like a charm!
### Step 2: Setting permissions
Permissions plays a huge role on linux system. To make files modifiable by our web server, I changed the permission settings of the host directory.
> You can always check current permission settings by `ls -l`
1. `sudo chgrp -R www-data /var/www/`
> this will change group owner of all files recursively, as [www-data is the default group of our web server](https://askubuntu.com/questions/873839/what-is-the-www-data-user), this will then give it access to modify the database
2. `sudo chmod -R 664 /var/www/`
> this will change permission level to 664
3. `sudo find /var/www/ -type d -exec chmod 775 {} +`
> this will change permission of level to 775 for only directories
Now, the directory and the files under should be accessiable to the web server.
c) (optional) To test it out, I used `git clone` to fetch [this web project](https://github.com/BillWang1018/online-forum/tree/sqlite) by my classmate and me on another course. (The original database runs on MySQL, so I had modified the code to makes it works on SQLite)
Use `cp` / `mv` / `rm` to cloned project to `/var/www/html/` and replace `index.html` so apache will now use our files instead of default welcome page!
<div style=display:flex;align-item:center;justify-content:center;background:#444;height:auto>
  
</div>
Now you can see all of my devices on the network can access the web page!
### Congratulations on having your web server running!
> But if anything on your web goes wrong, check `/var/log/apache2/error.log` to debug.
> (Useful reading tools like `less` may help)
## Lab 2: Building a FTP server
#### [File Transfer Protocol]("https://en.wikipedia.org/wiki/File_Transfer_Protocol") works like a bridge for file between computer. And it is still well supported across most of the system.
### Step 1: Install packages
1. `sudo apt install vsftpd` this will download package for FTP server.
### Step 2: Setting permissions
Here we back agian. But this time we're changing the user owner into ourselves.
1. `sudo chown -R pi /var/www`
### Step 3: Setting FTP config
Use any of your liked editor to change config file:
1. `sudo vi /etc/vsftpd.conf`
or `sudo nano /etc/vsftpd.conf`
2. change or add following settings:
```
anonymouns_enable=NO
local_enable=YES
write_enable=YES
force_dot_files=YES
```
always remember to save your file!
3. `sudo service vsftpd restart`
restart service to make our settings take effect.
### Step 4: Create a new user account
#### Login with FTP will take you to the home directory, which you might want to change where home is and never set it to root directory.
1. `sudo useradd <username>`
replace \<username> to any name you want.
this will create a new user to your system.
2. `sudo passwd <username>`
this will set the password for that user
3. `sudo usermod -d <path> <username>`
replace \<path> to the actual path,
e.g. if you want to make this user read the files in our web server, set it to `/var/www`
this will assign where the home dir is (`-d`) for that user
4. `sudo service vsftpd restart`
restart again to take effect.
#### Now let's take us back to our host machine
Connect the FTP server just like how you login with SSH, here I had created an account called `ftpacc`.
I am using windows file explorer to connect, you might be prompt to enter your username/password.
`ftp://ftpacc@192.168.213.16/`

#### Voila! We're in!
#### Now you had learned how to build a web server and how to easily exchange files with your device!