JustPaste.it

Lighttpd and python on VPS or linux box

User avatar
Jon Cooper @Jon_Cooper · Aug 18, 2024 · edited: Aug 19, 2024
sudo apt update
sudo apt upgrade

Install Numpy, SciPy and Matplotlib (sudo apt-get install ...).

sudo apt install python3-numpy
sudo apt install python3-scipy
sudo apt install python3-matplotlib

Install lighttpd.

sudo apt-get install lighttpd

All the html, jpg, png files should be copied to /var/www/html and create a folder there called /temp and another called /cgi-bin and copy .py files to the latter. 


Chmod all files to 755 including the folders.

 

It may be necessary to 'sudo adduser www' since lighty runs as user 'www' or on debian/ubuntu as 'www-data'. It probably will say user already exists.

 

As root go to /var to change group ownership of the www folder and the files inside it to 'www' or 'www-data' or type:

 

chown -R .www-data www

 

The last www is the folder, the first is the owner. On non-debian/ubuntu linux it's: 

 

chown -R .www www

 

Also, need to make sure that the /temp folder has write permission for the group, i.e. 'www-data' since the images and log files are written here by lighty running the programs as this user. Hence in /var/www/html type:

 

sudo chmod g+w temp

 

To set up lighttpd, in /etc/lighttpd/conf-available is a file called README which explains how to get lighty to execute programs in cgi-bin. Also in this folder is a file called 10-cgi.conf which has to be edited to say cgi.assign=(""=>"") so that it executes all file types, not just cgi's. Type:

 

/usr/sbin/lighty-enable-mod 

 

and say 'cgi' which makes a symlink in the conf-enabled folder to the new 10-cgi.conf. Then need to stop and restart lighty. 


/etc/init.d/lighttpd stop
/etc/init.d/lighttpd start

 

Any other changes can be made by going to folder /etc/lighttpd and editing the file 'lighttpd.conf', then stopping and restarting lighttpd as above. 

 

Lighty should look for index.html in /var/www/html and find the Python files in /var/www/html/cgi-bin. One problem is that I found the 10-cgi.conf file had this line in it:


alias.url += ( "/cgi-bin/" => "/usr/lib/cgi-bin/" )

 

which makes it look for your cgi files in the wrong place. You can either correct the folder to "/var/www/html/cgi-bin/" or just delete the whole line and lighty looks in the right place by default.


Note that lighttp can seem challenging to set-up, so look at the .conf files in /etc/lighttpd/* that give plenty of clues, remembering to stop and restart lighty to make sure any changes are applied. Haven't tried setting up Apache yet, but maybe will try installing a local copy one day. Actually, if apache2 is already running, the first thing is to lose it: 

sudo service apache2 stop
sudo apt remove apache2*

Schedule weekly deletion of files in temp which are more than an hour old with: 

crontab crontab

The file crontab file has to say: 

@weekly find /var/www/html/temp -type f -mmin +60 -exec rm "{}" \;

in order to delete everything in /temp that is more than 1 hour old every saturday at midnight. To check its running: crontab -l or -e.

 

To pack the files on the server:

 

tar -cvf my_safe_copy.tar folder1 folder2 ... 

 

Then gzip.