This article will walk you through setting up a server with Python 3, MySQL, and Apache2, sans the help of a framework. By the end of this tutorial, you will be fully capable of launching a barebones system into production.
Django is often the one-shop-stop for all things Python; it’s compatible with nearly all versions of Python, comes prepackaged with a custom server, and even features a one-click-install database. Setting up a vanilla system without this powerful tool can be tricky, but earns you invaluable insight into server structure from the ground up.
This tutorial uses only package installers, namely apt-get and Pip. Package installers are simply small programs that make code installations much more convenient and manageable. Without them, maintaining libraries, modules, and other code bits can become an extremely messy business.
To follow this tutorial, you will need:
In this step, we will set Python 3 as the default for our python
command.
First, check your current Python version.
On a fresh Ubuntu 14.04 server, this will output:
Python 2.7.6
We would like to have python
run Python 3. So first, let’s remove the old 2.7 binary.
Next, create a symbolic link to the Python 3 binary in its place.
If you run python --version
again, you will now see Python 3.4.0
.
In this section, we will install Pip, the recommended package installer for Python.
First, update the system’s package index. This will ensure that old or outdated packages do not interfere with the installation.
Pip allows us to easily manage any Python 3 package we would like to have. To install it, simply run the following:
For an overview of Pip, you can read this tutorial.
In this section, we will install and configure MySQL.
Installing SQL is simple:
Enter a strong password for the MySQL root user when prompted, and remember it, because we will need it later.
The MySQL server will start once installation completes. After installation, run:
This setup will take you through a series of self-explanatory steps. First, you’ll need to enter the root password you picked a moment ago. The first question will ask if you want to change the root password, but because you just set it, enter n. For all other questions, press ENTER to accept the default response.
Python 3 requires a way to connect with MySQL, however. There are a number of options, like MySQLclient, but for the module’s simplicity, this tutorial will use pymysql
. Install it using Pip:
In this section, we will install Apache 2, and ensure that it recognizes Python files as executables.
Install Apache using apt-get:
Like MySQL, the Apache server will start once the installation completes.
Note: After installation, several ports are open to the internet. Make sure to see the conclusion of this tutorial for resources on security.
We want to place our website’s root directory in a safe location. The server is by default at /var/www/html
. To keep convention, we will create a new directory for testing purposes, called test
, in the same location.
Finally, we must register Python with Apache. To start, we disable multithreading processes.
Then, we give Apache explicit permission to run scripts.
Next, we modify the actual Apache configuration, to explicitly declare Python files as runnable file and allow such executables. Open the configuration file using nano or your favorite text editor.
Add the following right after the first line, which reads <VirtualHost *:80\>
.
<Directory /var/www/test>
Options +ExecCGI
DirectoryIndex index.py
</Directory>
AddHandler cgi-script .py
Make sure that your <Directory>
block is nested inside the <VirtualHost>
block, like so. Make sure to indent correctly with tabs, too.
<VirtualHost *:80>
<Directory /var/www/test>
Options +ExecCGI
DirectoryIndex index.py
</Directory>
AddHandler cgi-script .py
...
This Directory block allows us to specify how Apache treats that directory. It tells Apache that the /var/www/test
directory contains executables, considers index.py
to be the default file, then defines the executables.
We also want to allow executables in our website directory, so we need to change the path for DocumentRoot
, too. Look for the line that reads DocumentRoot /var/www/html
, a few lines below the long comment at the top of the file, and modify it to read /var/www/test
instead.
DocumentRoot /var/www/test
Your file should now resemble the following.
<VirtualHost *:80>
<Directory /var/www/test>
Options +ExecCGI
DirectoryIndex index.py
</Directory>
AddHandler cgi-script .py
...
DocumentRoot /var/www/test
...
Save and exit the file. To put these changes into effect, restart Apache.
Note: Apache 2 may throw a warning which says about the server’s fully qualified domain name; this can be ignored as the ServerName directive has little application as of this moment. They are ultimately used to determine subdomain hosting, after the necessary records are created.
If the last line of the output reads [ OK ]
, Apache has restarted successfully.
In this section, we will confirm that individual components (Python, MySQL, and Apache) can interact with one another by creating an example webpage and database.
First, let’s create a database. Log in to MySQL. You’ll need to enter the MySQL root password you set earlier.
Add an example database called example.
Switch to the new database.
Add a table for some example data that we’ll have the Python app add.
Press CTRL+D to exit. For more background on SQL, you can read this MySQL tutorial.
Now, create a new file for our simple Python app.
Copy and paste the following code in. The in-line comments describe what each piece of the code does. Make sure to replace the passwd
value with the root MySQL password you chose earlier.
Save and exit.
Next, fix permissions on the newly-created file. For more information on the three-digit permissions code, see the tutorial on Linux permissions.
Now, access your server’s by going to http://your_server_ip
using your favorite browser. You should see the following:
[(1, 'One!'), (2, 'Two!'), (3, 'Three!')]
Congratulations! Your server is now online.
You now have a working server that can run Python 3 with a robust, SQL database. The server is now also configured for easy maintenance, via well-documented and established package installers.
However, in its current state, the server is vulnerable to outsiders. Whereas elements like SSL encryption are not essential to your server’s function, they are indispensable resources for a reliable, safe server. Learn more by reading about how to configure Apache, how to create an Apache SSL certificate and how to secure your Linux server.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
excellent, thank you!
Wait, there is some acronym abuse going on here. The P in LAMP is PHP. People trying to manually install a mediawiki from scratch will get very confused. LAMPy would make sense…
While the “P” in lamp is largely recognised as standing for PHP, it can also stand for Python or Perl. I do agree it can get confusing, especially for people new to LAMP.
Very helpful. Though I got some errors changing the default python version with symlink. Maybe it’s better to just refer to the right python version in the .py script files like so
#!/usr/bin/python3
They also talk about this on askubuntuThank you, Alvin! It was very helpful.
ugh, i’m pretty sure this hosed my python installation. now every single thing that needs python is just blowing up. reimage time i guess. glad i only spent a couple of hours on it.
Same here. Changing the python symlink broke my Software Center installation. Took me half an hour just to figure that out.
Hey Alvin,
Thank you for posting. I found your article very readable and helpful. I have a question though, when I got to the end I didn’t see the sql output. Instead I got “It worked! Congratulations on your first Django-powered page. Next start your first app by running python manage.py startapp [appname]”
Any ideas? I double checked your instructions.
Edit: I did check the numbers table to see if index.py executed and the values were all there.
Edit 2: More tests. I can only get the current django version by using python2.7, it isn’t found with the 3.0 python command. This is probably the issue. Will try upgrading django to python 3.0
Cheers David
I run server so have error: [Sat Nov 07 11:43:55.367547 2015] [cgi:error] [pid 406] [client 108.162.222.174:12539] AH01215: (8)Exec format error: exec of ‘/var/www/python/test/index.py’ failed [Sat Nov 07 11:43:55.368001 2015] [cgi:error] [pid 406] [client 108.162.222.174:12539] End of script output before headers: index.py
You shouldn´t change this by hand
This is a bad practice on any linux distro, messing with cause system errors since the operating system needs the python version that is by default, if you need a different version of python for your script, consider using
#!/usr/bin/env python3
in the first line of your script ( since we are using python3 this case),#!/usr/bin/env
is used instead of#!/usr/bin/python
because it will use the environment path for python, in case it is not in/usr/bin
.I already done this, and someone mentioned that I shouldn’t have done it. At first I was getting some errors but now it seems to be ok. Do you think I should use a live-cd to turn it back?
Amazing! thanks!
Hi, how do I make Apache run PHP and Python together? I managed to run PHP, but couldn’t find a way to run it together with Python. Appreciate your help.
did you find a solution for this? I have the same problem now!
Thanks for the excellent tutorial. I think this is just what I needed. One question though, can you either explain how to set up the website now through python or write another tutorial? I normally use flask to serve my pages and am just wondering how to serve pages through python now using your tutorial.
Am also needing to integrate Flask into this setup. Have you found anything that would help? Thanks.
This worked for me. I made my Flask app and ran it through a CherryPy server http://flask.pocoo.org/snippets/24/
It is as simple as Flask just with a full fledged server
I see you say that the server is vulnerable to outsiders. Is that even if I am just using it locally on my computer? Or should I be using virtualenv?
If you never connect the box to the outside, you should be fine. However, if there is an outside connection, I would take steps. There is a basic firewall setup given in this tutorial: https://classroom.udacity.com/courses/ud299/lessons/4331066009/concepts/48010894470923#
An excellent tutorial, to be sure. Was able to get right to it without trouble. I did make the following modifications:
Instead of exiting MySql using ^D, I used the “exit” command. Would rather exit normally than use keyboard controls.
Instead of deleting executables from /usr/bin, I copied files so there there is now python, python2, and python3. The “python” executable is actually python3. Saved python2 in case I need to run python2 programs.
I did not disable multi-threading since that is important to me. The rest of the steps automatically compensated for that since they detect multi-threading being set and adapt accordingly.
It was necessary to use http://your_server_ip/index.py. Referencing the server_ip by itself simply brought up the sample html page, which is fine if you want to also employ static html.
At issue now is integrating Flask into this setup. It would be great to have some pointers on that, without setting up a virtual environment.
If anybody else has run into the issue “no module named pymysql”, how did you fix it?
Best configuration document I’ve read in 2017 so far. Thank you very much for sharing.
i am getting this error on using the command :sudo a2enmod mpm_prefork cgi Error:Considering conflict mpm_event for mpm_prefork:Considering conflict mpm_worker for mpm_prefork:Module mpm_prefork already enabledapache2: Syntax error on line 219 of /etc/apache2/apache2.conf: Syntax error on line 1 of /etc/apache2/sites-enabled/000-default.conf: /etc/apache2/sites-enabled/000-default.conf:1: <VirtualHost> was not closed.Module cgi already enabled
Hey All,
I am getting a 500 internal server error and i find this in the error logs when I access localhost after these steps I see a that it sayd index.py has a malformed header, any idea whats causing this ?
Sat Apr 22 22:03:05.398787 2017] [mpm_prefork:notice] [pid 23873] AH00171: Graceful restart requested, doing restart AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message [Sat Apr 22 22:03:05.412296 2017] [mpm_prefork:notice] [pid 23873] AH00163: Apache/2.4.18 (Ubuntu) configured – resuming normal operations [Sat Apr 22 22:03:05.412315 2017] [core:notice] [pid 23873] AH00094: Command line: ‘/usr/sbin/apache2’ [Sat Apr 22 22:03:36.684778 2017] [cgi:error] [pid 25146] [client 127.0.0.1:38712] malformed header from script ‘index.py’: Bad header: () [Sat Apr 22 22:06:47.707436 2017] [cgi:error] [pid 25150] [client 127.0.0.1:38780] malformed header from script ‘index.py’: Bad header: ()
This comment has been deleted
My server just spits out the code. Something I did wrong? I am using Ubuntu 16.04 and I didn’t change the default python (instead I specify which python version to use).
The shebang #!/usr/bin/python is key.