Tutorial

How To Set Up an Apache, MySQL, and Python (LAMP) Server Without Frameworks on Ubuntu 14.04

How To Set Up an Apache, MySQL, and Python (LAMP) Server Without Frameworks on Ubuntu 14.04

Introduction

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.

Prerequisites

To follow this tutorial, you will need:

  • One Ubuntu 14.04 Droplet.
  • A sudo non-root user, which you can set up by following this tutorial.

Step 1 — Making Python 3 the Default

In this step, we will set Python 3 as the default for our python command.

First, check your current Python version.

  1. 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.

  1. sudo rm /usr/bin/python

Next, create a symbolic link to the Python 3 binary in its place.

  1. sudo ln -s /usr/bin/python3 /usr/bin/python

If you run python --version again, you will now see Python 3.4.0.

Step 2 — Installing Pip

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.

  1. sudo apt-get update

Pip allows us to easily manage any Python 3 package we would like to have. To install it, simply run the following:

  1. sudo apt-get install python3-pip

For an overview of Pip, you can read this tutorial.

Step 3 — Installing MySQL

In this section, we will install and configure MySQL.

Installing SQL is simple:

  1. sudo apt-get install mysql-server

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:

  1. mysql_secure_installation

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:

  1. sudo pip3 install pymysql

Step 4 — Installing Apache 2

In this section, we will install Apache 2, and ensure that it recognizes Python files as executables.

Install Apache using apt-get:

  1. sudo apt-get install apache2

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.

  1. sudo mkdir /var/www/test

Finally, we must register Python with Apache. To start, we disable multithreading processes.

  1. sudo a2dismod mpm_event

Then, we give Apache explicit permission to run scripts.

  1. sudo a2enmod mpm_prefork cgi

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.

  1. sudo nano /etc/apache2/sites-enabled/000-default.conf

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.

/etc/apache2/sites-enabled/000-default.conf

<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.

/etc/apache2/sites-enabled/000-default.conf

<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.

  1. sudo service apache2 restart

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.

Step 5 — Testing the Final Product

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.

  1. mysql -u root -p

Add an example database called example.

  1. CREATE DATABASE example;

Switch to the new database.

  1. USE example;

Add a table for some example data that we’ll have the Python app add.

  1. CREATE TABLE numbers (num INT, word VARCHAR(20));

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.

  1. sudo nano /var/www/test/index.py

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.

#!/usr/bin/python

# Turn on debug mode.
import cgitb
cgitb.enable()

# Print necessary headers.
print("Content-Type: text/html")
print()

# Connect to the database.
import pymysql
conn = pymysql.connect(
    db='example',
    user='root',
    passwd='your_root_mysql_password',
    host='localhost')
c = conn.cursor()

# Insert some example data.
c.execute("INSERT INTO numbers VALUES (1, 'One!')")
c.execute("INSERT INTO numbers VALUES (2, 'Two!')")
c.execute("INSERT INTO numbers VALUES (3, 'Three!')")
conn.commit()

# Print the contents of the database.
c.execute("SELECT * FROM numbers")
print([(r[0], r[1]) for r in c.fetchall()])

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.

  1. sudo chmod 755 /var/www/test/index.py

Now, access your server’s by going to http://your_server_ip using your favorite browser. You should see the following:

http://<^>your_server_ip<^>
[(1, 'One!'), (2, 'Two!'), (3, 'Three!')]

Congratulations! Your server is now online.

Conclusion

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.

Learn more about our products

About the authors
Default avatar
Alvin Wan

author

AI PhD Student @ UC Berkeley

I’m a diglot by definition, lactose intolerant by birth but an ice-cream lover at heart. Call me wabbly, witling, whatever you will, but I go by Alvin


Default avatar

staff technical writer

hi! i write do.co/docs now, but i used to be the senior tech editor publishing tutorials here in the community.


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
24 Comments
Leave a comment...

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 askubuntu

Thank 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

sudo rm /usr/bin/python

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.

    I get a generic 403 error, any pointers?

    Check your permissions. The chmod 755 step should handle it for you, but that won’t be any good if the parent folder doesn’t at also have 755 - or is at least owned by your apache.

    I have a Laravel App running on a DigitalOcean droplet. Seeing that this tutorial is also using virtual host, how will this affect my PHP setup?

    Are you guys kidding? How was this article approved? It is asking people to remove /usr/bin/python. It causes so many issues for non-expert users. This is absolutely not recommended. Please, remove of fix this part. This is evil and irresponsible from your end.

    thanks for saving my time and business.

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.