Configuring WSGI on Ubuntu/Centos

Reading Time: 3 minutes

This article outlines the process of configuring a server for Python 3 web applications with Apache 2.4 using mod_wsgi. 

What is mod_wsgi?

Mod_wsgi is an Apache module that allows Python web applications to function on a server. This module provides a web framework for Flask, Django, and other Python based frameworks to operate within a production environment on a server.

Prerequisites

  • A CentOS or Ubuntu server
  • Apache 2.4, apache-dev/httpd-devel and python3-dev installed
  • Python 3, pip3, and virtualenv installed
  • Access to a terminal

If you do not have the above prerequisites installed, this build will not work as expected. The following commands can be used to install the prerequisites

apt install apache2 apache2-dev python3 python3-dev python3-pip

pip3 install virtualenv

Step 1

Begin by downloading the source files for mod_wsgi.i

root@host [~]# wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.5.tar.gz

Next, unpack the archive

root@host [~]# tar -xzvf 4.6.5.tar.gz

Enter the source files directory

root@host [~]# cd mod_wsgi-4.6.5

Next, determine the path to the Python interpreter.

root@host [~]# which python3
/usr/bin/python3
root@host [~]# 

Configure the installation and define the Python interpreter for mod_wsgi.

root@host [~]# ./configure --with-python=/usr/bin/python3
checking for apxs2... no
checking for apxs... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/src/mod_wsgi-4.6.5':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

Now, run the following commands to compile the package.

root@host [~]# make && make install

The path to where the module was installed will be at the end of the output from the last command.

root@host [~]# /usr/lib/apache2/modules/mod_wsgi.so

Use the path noted above to load the module into Apache by adding the following snippet to the bottom of the apache.conf or httpd.conf

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

Now test the syntax and restart Apache.

Debian/Ubuntu based systems:

root@host [~]# apache2 -t
root@host [~]# service apache2 restart

RHEL/CentOS based systems:

root@host [~]# httpd -t
root@host [~]# service httpd restart

Step 2

Now that mod_wsgi has been installed, let’s take a look at the configuration. We will begin by creating a basic Python web application to detail the steps to configure mod_wsgi.

First, let’s create a virtual environment using the Python version that mod_wsgi was configured for. For this article, the example application will be placed in /var/www/. Next, we will run this command to create the directory wsgi_test, which will contain the virtual environment.

root@host [~]# virtualenv --python=python3 wsgi_test

Next, enter this directory and activate the virtualenv.

root@host [~]# cd wsgi_test
root@host [~]# source bin/activate
root@host [~]# pip install flask
root@host [~]# touch wsgi_test.py
root@host [~]# vim wsgi_test.py

Place the following configuration inside of the wsgi_test.py file

from flask import Flask
 app = Flask(__name__)
 @app.route("/")
 def index():
     return "Hello, from WSGI"

Now, let’s create the wsgi file to load the application.

root@host [~]# touch wsgi.py
root@host [~]# vim wsgi.py

Next, add the following information to the file. This data will activate the virtual environment when a request is received and execute the application.

activate_this = '/var/www/wsgi_test/bin/activate_this.py'
 with open(activate_this) as file_:
         exec(file_.read(), dict(__file__=activate_this))
 import sys
 sys.path.insert(0, '/var/www/wsgi_test/')
 from wsgi_test import app as application

Now that the example application has been created, let’s configure Apache.

We will begin by creating a virtual host for this application. This file will be placed inside of either
/etc/httpd/conf.d/ for RHEL based systems, or
/etc/apache2/sites-enabled/ for Debian based.

Redhat

For RHEL based systems, write the file to the following path replacing domain.com with your domain.

/etc/httpd/conf.d/domain.com.conf

Debian

For Debian based systems, write the file to the following path, replacing domain.com with your domain.

/etc/apache2/sites-enabled/domain.com.conf

The contents of the file will be as follows, replacing domain.com with your domain.

<VirtualHost *:80>
     ServerName domain.com
     WSGIDaemonProcess wsgi_test user=www-data group=www-data threads=2
     WSGIScriptAlias / /var/www/wsgi_test/wsgi.py
     <Directory /var/www/wsgi_test>
         Require all granted
     </Directory
 </VirtualHost>

For RHEL based systems, replace the user and group with apache as follows:

WSGIDaemonProcess wsgi_test user=apache group=apache threads=2

Now test and restart Apache.

Debian based systems:

root@host [~]# apache2 -t
root@host [~]# service apache2 restart

RHEL based systems:

root@host [~]# httpd -t
root@host [~]#  Service httpd restart

You can also use the curl command via the CLI

root@host [~]# curl http://domain.com

or navigate directly to the domain in a browser to see your application. Any errors will be logged to Apache’s default error log file usually found in /var/log/apache2/error_log

Ready to get started?

Liquidweb offers multiple server options for setting up and configuring this type of environment to run your apps. Reach out to one of our Solutions Team members today and they can put together a hosting package that will definitely meet your specific needs.

You can also give us a call 24 hours a day 1.800.580.4985 or open a chat or ticket with us.

Installing Linux Software Via The Commandline

Reading Time: 7 minutes

Installing Linux software from the commandline can save you time, money, and make life easy – if you know what you’re doing! This article is a brief overview of how the commandline operates on Linux distributions.

Once we have learned how the commandline works, we will move on to installing, removing, and updating our software packages VIA the commandline with yum. To get started, let’s begin with making sure we understand what the commandline does.

What Is The Commandline?

The commandline is a text-based interface which can be used to input instructions on a computer. A program called the bash shell usually provides the Linux commandline. There have been multiple shells developed over time as Linux has grown. Red Hat Linux utilizes the GNU Bourne-Again Shell. This shell is also known as “bash.” Bash is an improved version of one of the most well-known shells used on UNIX like systems, the Bourne Shell (sh). When the bash shell is used to pass info between the user and the computer, it displays a symbol when it is awaiting a command from the user. This interface is called the shell prompt. When an average user starts a bash shell, the default prompt usually ends with a dollar sign $ character, but this can be modified to suit the user.

CLI1

The dollar sign $ is replaced by the pound sign # if the shell is running as the root user. This symbol makes it apparent that it is a superuser shell, which can help to avoid accidents and mistakes on the accounts with elevated privileges.

CLI2

Using bash to execute commands can be very robust. The bash shell also provides a useful scripting language that can support automation of tasks. The shell even has additional capabilities that can help simplify or make operations that seem difficult in a graphical user interface (GUI) as simple as a single command.

Users access the bash shell through a terminal. A terminal provides a keyboard for user input and a display for output. On text-based installations, this can be the Linux machines physical console, the hardware keyboard, and screen. Terminal access can also be configured through serial ports. Another way to access a shell is from a virtual console. A Linux machines physical console supports multiple virtual consoles which act as separate terminals. Each virtual console supports an independent login session. If a graphical environment is available, it will run on the first virtual console. With a GUI running, you may access a virtual console by pressing Ctrl+Alt+F1 to go to the first console. You may use this combination, changing the F1 key out for F2 through F6, each representing their console.

How To Use The Shell?

Commands entered at the shell prompt must have three basic parts:

  1. A command to run
  2. Options to adjust the behavior of the command
  3. Arguments, which are targets of the commands

The command is the name of the program to run. It is usually followed by one or more options, which adjust the behavior of the command. The command options will start with either one or two dashes (ex: -a or –all) to distinguish them from other arguments. Commands may also be followed by one or more arguments or flags, that often indicates a target that the command should affect. For example, the command

usermod -L ge 

has a command (usermod), an option (L), and argument flags (ge). The effect of this command is to lock the password on user Ge’s account.

To effectively use a command, you need to know what arguments, options, and flags it uses and in what order it expects them (the syntax of the command). Most commands have a –help option. This option causes the command to print a description of what it does, describes the command’s syntax, a list of the options it accepts, along with what they do. If you are unsure how to operate a program and –help option is not available, you may try the “man” page. A man page is also known as a manual page. This option provides an overview of the program, and it’s syntax. If we wanted to look at how the sudo command is used, we would type “man sudo” to get a manual page for the command sudo.

How To Access The Commandline In A Desktop Environment?

The desktop environment is the graphical user interface on a Linux system. The default desktop environment in RHEL is GNOME. It provides an integrated desktop for users and a unified development platform on top of a graphical framework provided by the X Window System. The GNOME Shell provides the core user interface functions for the GNOME desktop environment.

To get a shell prompt in GNOME, start a graphical terminal application.
We can select Applications → Utilities → Terminal
On the desktop, right-click or press the Menu key, and select “Open in Terminal” from the menu.
From the Activities Overview, select Terminal from the dash.

When a terminal window opens, a shell prompt displays for the user that started the graphical terminal program. The shell prompt and the terminal window’s title bar will indicate the current user name, hostname, and working directory.

RPM And Software Packages

Red Hat developed the RPM Package Manager software, which provides a standard way to package software for distribution. Managing software in the form of RPM packages is much simpler than working with software that is extracted into a file system from an archive. The RPM software allows administrators to track which files are, or have been installed. It also checks to ensure supporting packages are present when the software is installed. Lastly, it identifies which dependencies need to be removed if a software package is uninstalled.

Information about installed packages is stored in a local RPM database on each system. All of the software provided by Red Hat is implemented as an RPM package. RPM packages are usually named using a combination of the following info using this format: 

name-version-release.architecture

When installing software from repositories, only the software name is required. Additionally, the software with the highest version will be installed. If there are several files within the same version, the software with the highest release number will be installed.

Each RPM software package is an archive made up of three components:

  1. The files installed by the packages
  2. Information (metadata) about the package – name, version, release, architecture
  3. A script which usually runs when the package is installed, updated, or removed

RPM packages are generally signed digitally by the group that packaged them. All software packages that come from a specific source are all signed with the same private GPG key. This way, if the software package is altered or corrupted in some way, the signature will no longer be shown as valid. This key signing allows the system to verify the integrity of the package before the install begins.

The YUM Package Manager

The YUM command searches multiple repositories for software packages and any associated dependencies, so they can be installed together to reduce or eliminate and dependency issues.

The main configuration file for yum is located at /etc/yum.conf with additional repository configuration files located in the etc/yum.repos.d directory. The yum command is used to list repositories, packages, and package groups:

Once a software package is installed, updates are checked against a repository during the daily update check. This allows for the RPM command to be used to query, update, install, and remove any outdated RPM packages.

Unfortunately, this check does not resolve dependencies automatically, and all software packages must be listed. Tools like YUM are essentially front-end applications for the RPM software and can be used to install an individual package or a collection of packages.

Managing Software Updates With YUM

Yum is a powerful commandline tool that can be used to flexibly manage software packages.

Finding Software with YUM

yum help – display usage information
yum list – display installed and available packages.

cli5

Yum search KEYWORD – list packages by keywords found in the name and summary fields. To search for packages that have “web server” in their name, we can use search all:

yum info PACKAGENAME – gives detailed information about a package.

Yum provides PATHNAME – displays packages that match the path name specified. To find packages that provide /var/www/html directory use:

Before we begin to install a package, let’s first make sure our system is up-to-date by issuing the command “yum update”.

Now that we know our system is updated, let’s install our package.

yum install PACKAGENAME

obtains and installs a software package, including all dependencies.

Next, we are asked if we want to install the package and dependencies. Type in ‘Y’ for yes, ‘N’ for NO

Yum now installs our package, fail2ban.

yum update PACKAGENAME 

obtains and installs the newest version of the software package, including all dependencies. With no PACKAGENAME specified, it will install all relevant updates for the system.

Yum remove PACKAGENAM

removes an installed software package, including any supported packages:

Enabling YUM Software Repositories

Registering a system to the subscription management service automatically configures access to the software repositories based on the attached subscriptions. To view all of the available repositories, we can use

yum repolist all

To enable a repository use yum-config-manager. This will enable or disable the parameters located in the /etc/yum.repos.d/redhat.repo file.

Enabling Third Party Software Repositories

Third-party repositories are directories of software package files provided by non-Red Hat source, which can be accessed by yum from a website, FTP server, or locally. Yum repositories are used by non-Red Hat distributors of software, or for small collections of local packages. To enable third-party support, you must put the file in the /etc/yum.repos.d/ directory. If the URL for a yum repository is known, a configuration file can be created via

yum-config-manager

This will create a file in the /etc/yum.repos.d directory with the output shown above. This file can now be modified to provide a customized name and the location of the GPG key. Administrators should download the key to a local file rather than allowing yum to retrieve the key from an external source.

RPM Configuration For The Repository:

Some repositories provide a configuration file and GPG public key as part of an RPM package that can be downloaded and installed using yum localinstall.

Using YUM To Install Local Package Files

The command

yum install PACKAGEFILE.rpm 

can be used to install package files directly. It automatically downloads any dependencies the package has from any configured yum repositories.

In this example, we downloaded an rpm file and then used

yum localinstall 

to begin installing the file from our machine.

Get Started Today!

Contact us today at 1.800.580.4985 to speak to a knowledgeable Solutions Provider who can get you the info you need, to make an informed decision right away.

Too busy to talk? Click HERE to open a quick chat with us to find out more.

Want more info in an email you can review at your leisure? Email us today to get handy advice on which product would best suit your needs.

We look forward to hearing from you!

Installing Jenkins on Ubuntu 16.04

Reading Time: 3 minutes

What Is Jenkins?

Jenkins is an open source automation server software developed in Java. It allows developers to integrate CI/CD (Continuous Integration/Continuous Delivery) pipelines within their organization that ease and automate workflows. It has an extensive help community, supports over 1000 plugins, allows users the ability to automate almost any task and, it saves significant time that can be better utilized addressing other issues.

When automating tasks with Jenkins, users can optimize their workflow by quickly automating the jobs that servers are not able to do themselves. Jenkins has a wide array of features including building projects, executing unit tests for bug detection, analyzing static code, and deploying applications. For this article, we will learn how to install Jenkins on a Ubuntu 16.04 server using APT (Advanced Package Tool). When using APT, we can retrieve and install all of the needed dependencies as well.

Continue reading “Installing Jenkins on Ubuntu 16.04”

How to Check for Installed Packages on CentOS

Reading Time: 3 minutesWhile managing your server, you’ll sometimes need to check on which software (or packages) you have installed on your system. You’ll need to know package names, version numbers, dates of installation, etc. In this Liquid Web tutorial, we’re going to be discussing how to inspect packages installed on your CentOS system. There are several ways to accomplish this, and we’ll discuss a few of them. Let’s dig in! To use these commands, you’ll need to log in to your server via SSH. For more information, see Logging into Your Server via Secure Shell (SSH).

Continue reading “How to Check for Installed Packages on CentOS”

How Do I Connect My Mac to Windows?

Reading Time: 2 minutesMac users work in their native Unix environment are familiar with using the terminal to SSH into their Linux based servers. When using a Mac to log into a Windows environment, or vice versa,  the task is performed differently. Window machines use a different protocol, one aptly named RDP (Remote Desktop Protocol). For our tutorial, we’ll explore how to use your Mac to connect to a Windows server.  Let’s get started!

Continue reading “How Do I Connect My Mac to Windows?”

How to Install MySQL on Windows

Reading Time: 2 minutesIf you’re using a Windows-based server to host your content, you may using Microsoft’s database server product, MSSQL. However, licensing restrictions can make using MSSQL difficult, especially for small businesses. Microsoft offers a free version of MSSQL called MSSQL Express that will be suitable for many users, but this version does have limitations on database size and memory usage. If you need a more robust database solution but want to try something with a lower cost (like a free, open-source database server), you could try MySQL database server.

MySQL is a standard part of the typical Linux server build (or LAMP stack) but is also available for use on Windows operating systems. Depending on your needs, you could fully develop your database in MySQL. Many popular Content Management Systems (CMS) also use MySQL by default, so using MySQL to manage those applications may be beneficial. MySQL and MSSQL can be run on the same server at the same time, so you’re free to use both or to experiment as needed.

Installing MySQL on your Windows server is as simple as downloading an MSI Installer package and clicking through a few options.

  1. Download the MySQL Installer from dev.mysql.com. The two download options are a web-community version and a full version. The web-community version will only download the server, by default, but you can select other applications (like Workbench) as desired. The full installer will download the server and all the recommended additional applications. (You’ll also be asked to create a user account, but you skip this part by scrolling down to the bottom and clicking “No thanks, just start my download”.)

    mysql_installer_download

  2. Run the installer that you downloaded from its location on your server, generally by double-clicking.
     
    Note
    You can use this same MSI Installer to upgrade currently installed versions of MySQL as well! As is typical, the first step is accepting the license agreement, then click Next.

    license agreement

  3. Determine which setup type you would like to use for the installation:
    1. Developer Default: this is the full installation of MySQL Server and the other tools needed for development. If you are building your database from the ground up or will be managing the data directly in the database, you’ll want to use this setup type.
    2. Server Only: if you only need MySQL Server installed for use with a CMS or other application and will not be managing the database directly, you can install just the server (you can always install additional tools later).
    3. Custom: this setup type will allow you to customize every part of the installation from the server version to whichever additional tools you select.

      setup type

  4. Install the server instance and whichever additional products you selected. Then begin the configuration process by selecting the availability level (most users will use the default, standalone version).
  5. Complete the configuration process by following the on-screen instructions. You’ll want to make sure to install MySQL as a Service so that Windows can automatically start the service after a reboot or can restart the service if it fails. For additional, step-by-step instructions, see MySQL Server Configuration with MySQL Installer.

Have other thoughts or questions? Our Sales and Support teams are available 24 hours by phone or e-mail to assist. Reach out to us by opening a support ticket at support@liquidweb.com, giving us a call at 1-800-580-4985 or, open a chat with us and we’ll be happy to answer any questions you may have!

How to Setup and Use Microsoft SQL Server Management Studio

Reading Time: 4 minutes

What is SSMS?

SQL Server Management Studio (SSMS) is a free Windows application to configure, manage, and administer Microsoft SQL Server (MSSQL). SSMS includes an Object Explorer to view and interact with databases and other elements, a Query window to write and execute Transact-SQL queries, and script editors for developers and administrators. Continue reading “How to Setup and Use Microsoft SQL Server Management Studio”

Installing Redis on Ubuntu 16.04/18.04

Reading Time: 4 minutes

What is Redis? 

Redis or “REmote DIctionary Server” is defined as an open source, “key-value” database storage medium, which is additionally known as a data structure server. At its heart, Redis works with key-value pairs and stores data in a location that’s easily referenceable by two specific values. These key-value associations are usually a set of two linked data entries which are made up by a key, which is a unique identifier for a type of data and, the value, which can be either the particular data that is identified or, an indicator to the location of that data.

Continue reading “Installing Redis on Ubuntu 16.04/18.04”

Install and Configure Git on Ubuntu 18.04

Reading Time: 1 minute

What is the purpose of Git?

Git gives you a way to not only track changes in source code, but it can also be used to track changes in files.  It then stores the data in what is called a repository, also known as a repo. Continue reading “Install and Configure Git on Ubuntu 18.04”

What are Common Commands to Update WordPress Using WP-CLI?

Reading Time: 2 minutesWP-CLI is a very handy set of commands. You can run anything that you would run in wp-admin on a WordPress site but from the command line. Useful commands which WP-CLI employs to keep WordPress core updated plugins including the default themes which come with WordPress.

Continue reading “What are Common Commands to Update WordPress Using WP-CLI?”