Setup web server
LAMP (linux, apache, mysql and php) is a common web server stack. I use Raspberry Pi OS, apache2, sqlite3, python3. Any combination can be used. The choice is up to you.
Raspberry Pi OS is a distribution of linux debian
apache2 is a webserver
sqlite3 is a lightweight database
python3 is my language of choice
At times, I will also use php instead of python
Parts:
Raspberry Pi 3 running latest version of Raspberry PI OS (nee raspbian)
MacBook but a PC can be used (On a PC use putty to ssh and to open a terminal window)
Step 1. Open terminal window and Login
Step 2. Install apache2
Step 3. Install sqlite3
Some useful directories:
/var/www webpages
/var/www/cgi-bin python scripts
/etc/lighttpd conf files
/var/log/lighttpd log files
Step 4. [skip if using Raspberry Pi OS] Check if python3 is installed
Raspbian comes with python installed. Run to check if python3 is installed
$ which python3
/usr/bin/python3
If "which" doesn't return the above and instead just a pomrpt, then install python3
$ sudo apt-get install python3 -y
Step 5. Check if flask and pip3 are installed
Flask is a framework for building python-based web pages.
$ sudo apt-get install python3-flask -y
pip3 is a package manager for python.
$ which pip3
/usr/bin/pip3
If nothing comes back, then install pip3 using
$ sudo apt-get install python3-pip -y
Step 6. Create a simple home page
Run the command:
$ nano hello.py
and add:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello world'
if __name__ == '__main__':
app.run(debug=True, port=5000, host='0.0.0.0')
Step 7. Check if it works
Start the python script running
$ python3 hello.py
In a browser open:
Hello World
Type CTRL-c to exit the python script
If it doesn't work, then it is most likely due to security precautions done earlier
$ sudo ufw disable
$ sudo reboot
Retry the commands above and it should work.
Next reenable ufw and reboot
$ sudo dfw enable
Step 8. Install latest version of php
Find latest version of PHP on this link
On 27NOV2020, the latest ♣version♣ = 7.4
Find a version that will install. Run a command like:
$ sudo apt install --dry-run php♣version♣
7.4 won't install. So, drop down one version to 7.3, and retry
$ sudo apt install --dry-run php7.3
Repeat until a version is found. Do the same for sqlite3
As of 27NOV2020, this works:
$ sudo apt install php7.3 php7.3-sqlite3 -y
Step 9. Edit apache2.conf
On Raspbian aand Raspberry PI OS, apache's config file is apache2.conf (not httpd.conf) and it is located in: /etc/apache2/apache2.conf
Without flask, apache2.conf would need to be modified to make apache aware or able to run python scripts.
Raspberry PI OS and later use systemd (so don't use init.d).
Here are some common commands for the apache service.
$ sudo systemctl start apache2.service
$ sudo systemctl stop apache2.service
$ sudo systemctl restart apache2.service
To see status and logs
$ sudo systemctl status apache2.service
$ journalctl -xe