Hostwinds Tutorials
Search results for:
Table of Contents
Tags: Ubuntu
Mezzanine is a Content Management system written in Python. It's a great alternative to the popular WordPress and offers a similar interface.
To get all of this running, use the command:
sudo apt install python3 python3-pip postgresql nginx libpq-dev
Then, use pip in install virtualenvwrapper*:
sudo pip3 install virtualenvwrapper
Normally, it's a bad idea to use pip3 in install software, but virtualenvwrapper is one of the few packages that it makes sense to install system-wide.
Now, set up an environment for the mezzanine and install it:
mkvirtualenv my_site
work on my_site
pip3 install mezzanine uwsgi pillow
With all the dependencies installed, we need to set up the database.
Configuring a user for PostgreSQL requires onlt a few SQL statements you8 can run from the SQL command line. to start the SQL shell:
sudo su postgres -c psql
In the psql shell, run these 3 lines:
CREATE USER your_site_name WITH ENCRPYPTED PASSWORD 'secure_pass';
CREATE DATABASE your_site_name;
GRANT ALL PRIVILEGES ON DATABASE your_site_name TO your_site_name;
These commands will create a database and user interact with it.
Now, we may need to grant access to that user. Use this command to edit the pg_hba.conf, that's what PostgreSQL uses to verify access:
sudo nano /etc/PostgreSQL/11/main/pg_hba.conf
Make sure this line is present in the file:
host all 127.0.0.1/32 md5
Once you've edited the file, restart the database to apply the changes:
sudo systemctl restart postgresql
Now that the database is set up, we can move on to configuring the Mezzanine. To start a Mezzanine project, use the command:
(my_site) mezzanine-project my_site
cd my site
In the my_site directory, you'll find another directory called "my_site." You'll need to edit a file called settings.py:
nano mysite/settings.py
There are two things to set up in that file.
Database configuration, use the same user and passwords from the SQL user we created earlier:
DATABASES = {
"default": {
# Add "postgresql", "mysql", "sqlite3" or "oracle".
"ENGINE": "django.db.backends.postgresql",
# DB name or path to database file if using sqlite3.
"NAME": "my_site",
# Not used with sqlite3.
"USER": "my_site",
# Not used with sqlite3.
"PASSWORD": "secure_pass",
# Set to empty string for localhost. Not used with sqlite3.
"HOST": "127.0.0.1",
# Set to empty string for default. Not used with sqlite3.
"PORT": "",
}
}
Allowed hosts:
ALLOWED_HOSTS = ['my_site.com', 'www.my_site.com']
Once those are ready, save the settings.py file.
The aptly named manage.py script is used to set up and manage your server. Now that Mezzanine has the database user properly configured, we can add the tables using the createdb command like so:
python manage.py createdb
Then, add an admin user to help manage your site:
python manage.py createsuperuser
Finally, build the assets, like javascript and CSS, using:
python manage.py collectstatic
For our website to come up, you'll need to configure a Python loader to run the code and a webserver to communicate with the outside world. Let's start with the Python loader. For this setup, we'll use the uWSGI runner.
In your my_site/my_site directory, create a file called my_site.ini, using nano:
[uwsgi]
socket = 127.0.0.1:8080
chdir = /home/$USER/my_site
wsgi-file = my_site/wsgi.py
processes = 2
threads = 1
You can set up uWSGI to run at boot by creating a service file in /usr/lib/systmd/system/my_site.service:
[Unit]
Description=My Site
After=Network.target
[Service]
User=$USER
Group=$USER
WorkingDirectory=/home/$USER/my_site
Environment="PATH=/home/$USER/.virtualenvs/mezzanine/bin"
ExecStart=/home/$USER/.virtualenvs/mezzanine/bin/uwsgi my_site.ini
Finally, run the command:
sudo systemctl enable my_site
And:
sudo systemctl start my_site
That will configure your python runner to start on boot and start it right now.
One last task to round all of this out: set up the webserver. NGINX is a popular choice for python projects because of its reverse proxy capabilities.
To configure it, add this file into /etc/Nginx/sites-enabled:
upstream mezzanine {
server 127.0.0.1:9081;
}
server {
listen 80 default_server;
server_name www.my_site.com my_site.com;
charset utf-8;
client_max_body_size 50M;
location /media {
alias /home/$USER/my_site/media;
}
location /static {
alias /home/$USER/my_site/static;
}
location / {
uwsgi_pass mezzanine;
include uwsgi_params;
}
}
Then to apply this configuration, run the command:
sudo systemctl restart nginx
Written by Hostwinds Team / December 2, 2019