How to setup Linux Apache Mysql Python server

Apache, MySQL, and Python can be used in tandem to host and serve a website from a Linux system. This assortment of software is known as a LAMP stack (Linux, Apache, MySQL, Python) although sometimes the ‘P’ means PHP, which can either be used in addition to or as a replacement of Python. Apache serves the website, MySQL stores and retrieves information from a database, and Python can generate HTML and related code for the site’s visitors to view.

In this tutorial, we will go over the step by step instructions to install Apache, MySQL, and Python on Linux. The installation process is straightforward, but Apache will require some configuration in order to read Python scripts, and MySQL must run through initial setup before it can be used. We will make a simple ‘Hello World’ Python script and then access it from our web browser to show that Apache is reading the code as intended.

DID YOU KNOW?
Python can also be used to host a website by itself. This is not suitable for a production website, but can be useful to generate a very quick website that is intended only for personal use. See our tutorial on Configuring a simple web server in Linux to see how to host a website with Python.

In this tutorial you will learn:

  • How to install Apache, MySQL, and Python on all major Linux distros
  • How to run through first time setup of MySQL
  • How to configure Apache to read Python files
  • How to create a test Python script and host it with Apache
How to setup Linux Apache Mysql Python server
How to setup Linux Apache Mysql Python server
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Apache web server, MySQL or MariaDB, Python
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install Apache, MySQL, and Python




The first thing we’ll need to do is prep our Linux system with the proper software packages. The installation commands will differ depending on which Linux distro you are running.

You can use the appropriate command below to install Apache, MySQL, and Python with your system’s package manager.

To install Apache, MySQL, and Python on Ubuntu, Debian, and Linux Mint:

$ sudo apt update
$ sudo apt install apache2 mariadb-server mariadb-client python3

To install Apache, MySQL, and Python on Fedora, CentOS, AlmaLinux, Red Hat, and Rocky Linux:

$ sudo dnf install httpd mariadb-server python3

Configure MySQL

One of the first things we should do is get our WordPress database ready. In order to do that, we first need to do some initial configuration of MySQL. To get started, execute the following command in terminal:

$ sudo mysql_secure_installation

Leave the first response blank and press enter. You should reply with y (yes) to the rest of the prompts, and configure a root password when prompted to do so. This setup only takes a moment to complete.

The initial setup of MySQL with mysql_secure_installation
The initial setup of MySQL with mysql_secure_installation

Configure Apache

Apache should already be installed and running at this point, and that can be verified by opening a browser and navigating to loopback address 127.0.0.1 or just localhost on your system.

Default Apache page, indicating that our website is accessible
Default Apache page, indicating that our website is accessible

Although Apache is hosting our site (or lack of one) already, it’s best practice to configure a new Apache virtual host file for our website. This will allow you more flexibility in the future if you want to host multiple websites or make changes to where your website directory is installed, etc.

  1. Copy the default Apache configuration into a new file with the following command. We are calling our new website file mysite.conf:
    $ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysite.conf
    

    Note: The /etc/apache2 directory is /etc/httpd on RHEL-based systems. Adjust the command if necessary.

  2. Then, use nano or your favorite text editor you prefer to open this file:
    $ sudo nano /etc/apache2/sites-available/mysite.conf
    
  3. Change the DocumentRoot setting to where we plan to install your Python website. We are just using mysite as the directory name.
    DocumentRoot /var/www/mysite
    



  4. Create a setting for ServerName and enter your website’s fully qualified domain name. If you don’t have one, leave it as localhost.
    ServerName your-site.com
    
  5. Below these settings, we need to add the following lines. This tells Apache to look for an index.py (Python) file, and the ExecCGI lines are necessary so Apache knows to execute the files.
    <Directory /var/www/mysite>
        Options +ExecCGI
        DirectoryIndex index.py
    </Directory>
    AddHandler cgi-script .py
    
  6. Create an alias for the www prefix as well. This isn’t necessary if you are just using localhost.
    ServerAlias www.your-site.com
    

    This is how your config file should look when you’re done. Note that we commented out the alias line in our config since we are only hosting locally.

    Filling out the DocumentRoot and ServerName values in Apache virtual host file
    Filling out the DocumentRoot and ServerName values in Apache virtual host file
  7. Save your changes and exit the file. Check your Apache config for any errors. Then, enable the site in Apache and disable the default site.
    $ apachectl configtest
    $ sudo a2ensite mysite.conf
    $ sudo a2dissite 000-default.conf
    
  8. Then, disable the mpm_event module and enable mpm_prefork and cgi so that Apache can execute our Python file(s).
    $ sudo a2dismod mpm_event
    $ sudo a2enmod mpm_prefork cgi
    
  9. Finally, restart Apache for the new changes to take effect.
    $ sudo systemctl restart apache2
    OR:
    $ sudo systemctl restart httpd
    

Make a Python web page

Now we will make a simple Python script, place it in the website’s directory, and then navigate to our website to see if the page we made in Python is displayed as intended.

  1. Make the necessary site directory in case it does not exist yet:


    $ sudo mkdir /var/www/mysite
    
  2. Create the following file in your preferred text editor:
    $ sudo nano /var/www/mysite/index.py
    
  3. Paste the following code and then save and exit the file:
    #!/usr/bin/python3
    print ("Content-Type: text/html")
    print()
    print ("<html><head>")
    print ("</head><body>")
    print ("Hello World!")
    print ("</body></html>")
    
  4. Configure the correct permissions:
    $ sudo chown -R www-data:www-data /var/www/mysite
    $ sudo chmod 755 /var/www/mysite/index.py
    

You can now navigate to your website and see that Apache displays the contents of our Python file:

Our website displaying the Hello World text from the Python script
Our website displaying the Hello World text from the Python script

Closing Thoughts

In this tutorial, we saw how to setup a web server on a Linux system using Apache, MySQL, and Python. This allows us to serve content that has been written with Python scripts, while retrieving and storing information in a MySQL database. Python can interact with MySQL and display information from databases, or be used to generate HTML and other related web code.



Comments and Discussions
Linux Forum