Installing python windows server 2012

How to install Django onto Windows Server 2012

Nov 28, 2016 · 9 min read

For a school project in April 2016, I had to install a Django project onto a Windows server (using Apache and PostgreSQL). Here’s how I did it — hopefully this will be useful to somebody out there trying to do the same.

Note: I’m using a customized version of Django called Django-mako-plus , but this tutorial should mostly work for both.

Things to be aware of:

  • Make sure that you use a consistent bitness (32-bit, 64-bit). Later on in this tutorial are included some links to 64-bit files, so I’d recommend using that bitness.
  • When you install Python, don’t install Python 3.5. Install 3.4 or lower instead. You’ll run into issues later on if you install 3.5, because of how new it is.
  • When you go to put your project on your server, I’d recommend you first install Git on your server and then git clone your project into the folder. Not a requirement, but it will come in handy when you want to pull changes later on.
  • Google is your friend. Use it.

1. Install a new C++ compiler

While going through this process, I ran into a bunch of .dll issues. Rather than trying to deal with those as they come up, I’d recommend saving yourself the headache and installing a new C++ compiler. Go to Microsoft.com and download a new C++ redistributable.

The .dll errors that this has solved for me are:

Thanks to this StackOverflow article that helped me find this solution.

2. Install Python-related things

First download & install Python, and then get Django, Mako, Django-mako-plus with pip:

3. Installing psycopg2

Here’s the first pain point — installing psycopg2. Running

gave me a C++ compiler error similar to the person in this thread. The solution that worked for me was to download a pre-compiled package from StickPeople.com. Be sure to get the right download according to your Windows and Python versions.

If that doesn’t work for you, try running one of these.

4. Install Apache

Installing Apache on Windows could be complicated, because the Apache Software Foundation doesn’t provide compiled versions of their server, but thankfully previous developers have already done the hard work for you.

I chose to use ApacheLounge, but ApacheHaus and other options I found seem to be as equally viable. So just grab the most recent version for your operating system, extract the Apache24 folder, and stick it in your system root (most likely C:/).

When you open it up, you’ll be confronted by a bunch of different folders. The most important of these for our purposes will be /bin, /conf, and /htdocs.

4.1. Add apache to your system variables

This part isn’t technically necessary, but it’s really handy to do. You’re going to add the main Apache service httpd.exe to your system variables so you can use httpd in your command line (like you use pip or python).

  • Open up your system variables by searching for “System Variables” in your start menu
  • Click on “Environment Variables”
  • In the “System variables” pane, scroll down to “Path” and add C:\Apache24\bin to the end of that path. Make sure there is a semicolon before you add that, or else it will mess up your path

Pro tip: You can get the path to any folder and file in Windows by holding shift, right clicking on it, and selecting “copy as path” — super handy!

You can then install Apache as a service for Windows by running

4.2 Troubleshooting Apache Installation

If you get some error like

Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

it’s because the ServerName value in your httpd.conf file isn’t set. So navigate to C:\Apache\conf\httpd.conf and add the line

Читайте также:  Windows update setup files

below the ServerName documentation block, which looks like this:

If you get a different error like:

(OS 10048)Only one usage of each socket address (protocol/network address/port) is normally permitted. :
AH00072: make_sock: could not bind to address [::]:80 (OS 10048) Only one usage of each socket address (protocol/network address/port) is normally permitted.
AH00072: make_sock: could not bind to address 0.0.0.0:80
AH00451: no listening sockets available, shutting down
AH00015: Unable to open logs

It’s because you have another process running on port 80. The troubleshooting for removing this is a bit complicated because several different kinds of processes could be running on port 80.

Here are a reference links for reference:

Here’s how I solved this issue:

  1. You have to figure out what is running on port 80, and then close that. Windows has a program called Netstat that lets us find out what programs are running on what ports. Run netstat.exe –a –n –o
  2. Pretty quickly after you run it, you’ll probably have to manually stop it with crtl+c since it goes through all your ports.
  3. Go to the top of the printout and look for port 80. It’ll most likely look like TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 1540
  4. The last number is the PID of the program that is listening on the port (and blocking Apache from listening). If you open Task Manager and go into the detail tab, you can sort by PID. Look for the task that matches the PID we found earlier.
  5. You’ll have to use your own discretion to determine if it’s a service that you can just straight up close or not. When I first had this error, it was the “System” service. My solution was to restart the computer, and then when I did this a second time, it was a different program. Once you find the service and stop it, try installing Apache again. If it installs without complaining, you know you’ve done it right.

If you run into an error where you can’t use Apache because it just stops (and shows a Windows dialog), click on details. If you’re missing a vctruntime140.dll file, you need to install the latest version of the C++ compiler. Refer to step 1 for that.

4.3. Finalize Apache Installation

Okay, once apache is installed, we can make it starts on boot by typing in services.msc in our command line, which opens the Windows Services window. Find the apache service, open its properties, and make sure that “Startup type” is set to “Automatic”.

Then try starting your server by using the command

(If that isn’t working, try httpd –k stop just to make sure it’s actually off.)

When you go to localhost or 127.0.0.1 you should see an “It works!” page.

5. Connect Django and Apache with mod_wsgi

Okay, now that we have a server, let’s tie Django and Apache together with an external module called wsgi. Unfortunately, the wsgi file that comes with the default Django installation isn’t enough, which is where mod_wsgi comes in.

Here are some initial reference docs:

5.1. Get a mod_wsgi.so file

Getting the mod_wsgi.so file (the Apache module) is a little tough- if you’re able to find a pre-compiled binary, that’s great! I would recommend searching around to see if you can find a compatible file.

If you have to compile it yourself, first download the source code for the latest mod_wsgi release from Github and then follow the instructions from Graham Dumpleton.

The only issues that I encountered when compiling the binaries were fixed by modifying the .mk files in the win32 directory to reflect the actually directory paths for your python and apache installations and adding the line

to the wsgi_memory.h file in the src/server/ directory.

5.2. Install the mod_wsgi.so file

So, however you go about it, you should now have some mod_wsgi.so file. Rename it to mod_wsgi (leave the “.so” file type off of the file name) and deposit it in the C:\Apache24\modules directory.

Register it by adding the following line to your httpd.conf file:

Your best bet is to stick it at the end of the “LoadModule” statements, underneath

You can test your installation of mod_wsgi by running the command

and wsgi_module should appear at the end of the list. If it doesn’t, go back and troubleshoot because you’re not going to have success later on if this isn’t working.

6. Configure your project properly

Now, assuming everything before this has worked, you should now have a server with a working installation of Django, Mako, Django-Mako-Plus, Psycopg2, PostgreSQL, and Apache with the mod_wsgi module installed. Now we can start with the process of actually setting up your site.

Читайте также:  Вся для pda windows

6.1. Stick your source code in the htdocs folder

First, grab your site installation and stick it in the C:/Apache24/htdocs directory. This is your “document root,” unless you specify otherwise; it’s the directory where your Apache server will look by default when searching for files to serve. I’d recommend git-cloning your project into this folder.

6.2. Adjust your Django-Mako-Plus Project settings

Second, let’s do some adjusting in your settings.py file to make sure everything runs properly.

Reconfigure your database settings if needed to make sure they’re pointing to the new database that your created when you installed PostgreSQL. If it has the same name and password as your original, you’re golden. Make sure a database actually exist with the give name. You can do this in pgAdmin.

Then, we need to do some privacy things that Conan Albrecht talks about in the django-mako-plus tutorial. Go to the portion of your settings.py file that talks about static files. Comment out the line that says BASE_DIR so that your file looks like this:

6.3 Get all your static things together

Then, go to your project root (where your manage.py file is) and run

This will gather up all of your errant media, script, and style files into a convenient /static/ directory. If you’ve been using the $ < STATIC_URL >tag properly, this will also go off without a hitch. If not, you might need to prepend the uri of any static resource with /static/.

But you should be using the $ < STATIC_URL >tag. So mend your ways if you aren’t.

6.4 Update your host files to serve your site correctly

Go to your httpd.conf and uncomment the line:

This will allow you to specify a few different things about how your files are served.

Jump over to your httpd-vhosts.conf file (found in your C:/Apache24/conf/extra/ directory) and add the following (replace the uppercased stuff with the appropriate text):

These settings specify where to find all of the different resources that are needed to make your site, including where to find your static files (which you’ve conveniently organized with dmp_collectstatic) and your site’s wsgi driver (that came with your Django project in the first place).

This is essentially everything that I can remember needing to set up my site. If you go to localhost at this point, you should see your site in all of its fully-rendered glory.

Host multiple Django sites on the same server

One thing that I struggled with was properly configuring my virtualhosts to allow multiple django sites at the same time. The solution that I found involved removing the WSGIPythonPath variable and the WSGIApplicationGroup variables. Then, in the wsgi.py file of your project, add these lines:

You need to put the whole path to your project. For example, my wsgi.py file looks like this:

and change the line where the environment is set to

All of this is intended to load the correct Python path to your project each time it’s requested, instead of having them overlap and serve the wrong files.

You can read more about it here:

Conclusion

Hopefully you’ve made it this far and this tutorial went with off without a hitch. If these instructions aren’t correct or you ran into other issues, pay it forward by leaving a comment for the next person who comes along and has that same issue.

How to install Python client libraries for remote access to a Machine Learning Server

Machine Learning Server includes open-source and Microsoft-specific Python packages for modeling, training, and scoring data for statistical and predictive analytics. For classic client-server configurations, where multiple clients connect to and use a remote Machine Learning Server, installing the same Python client libraries on a local workstation enables you to write and run script locally and then push execution to the remote server where data resides. This is referred to as a remote compute context, operant when you call Python functions from libraries that exist on both client and server environments.

A remote server can be either of the following server products:

Client workstations can be Windows or Linux.

Microsoft Python packages common to both client and server systems include the following:

This article describes how to install a Python interpreter (Anaconda) and Microsoft’s Python packages locally on a client machine. Once installed, you can use all of the Python modules in Anaconda, Microsoft’s packages, and any third-party packages that are Python 3.5 compliant. For remote compute context, you can only call the Python functions from packages in the above list.

Читайте также:  Linux if path exists

Check package versions

While not required, it’s a good idea to cross-check package versions so that you can match versions on the server with those on the client. On a server with restricted access, you might need an administrator to get this information for you.

Install Python libraries on Windows

Download the installation shell script Install-PyForMLS.ps1 (or use https://aka.ms/mls93-py for the 9.3 release or https://aka.ms/mls-py for the 9.2. release). The script installs Miniconda 4.5.12, which includes Python 3.7.2, along with all packages listed previously.

Open PowerShell window with elevated administrator permissions (right-click Run as administrator).

Go to the folder in which you downloaded the installer and run the script. Add the -InstallFolder command-line argument to specify a folder location for the libraries. For example:

Installation takes some time to complete. You can monitor progress in the PowerShell window. When setup is finished, you have a complete set of packages. For example, if you specified C:\mspythonlibs as the folder name, you would find the packages at C:\mspythonlibs\Lib\site-packages .

The installation script does not modify the PATH environment variable on your computer so the new python interpreter and modules you just installed are not automatically available to your tools. For help on linking the Python interpreter and libraries to tools, see Link Python tools and IDEs, replacing the MLS server paths with the path you defined on your workstation For example, for a Python project in Visual Studio, your custom environment would specify C:\mypythonlibs , C:\mypythonlibs\python.exe and C:\mypythonlibs\pythonw.exe for Prefix path, Interpreter path, and Windowed interpreter, respectively.

Offline install

Download .cab files used for offline installation and place them in your %TEMP% directory. You can type %TEMP% in a Run command to get the exact location, but it is usually a user directory such as C:\Users\ \AppData\Local\Temp .

After copying the files, run the PowerShell script using the same syntax as an online install. The script knows to look in the temp directory for the files it needs.

Install Python libraries on Linux

On each supported OS, the package manager downloads packages from the repository, determines dependencies, retrieves additional packages, and installs the software. After installation completes, mlserver-python executable is at ‘/usr/bin’.

On Ubuntu 14.04 — 16.04

With root or sudo permissions, run the following commands:

Red Hat and CentOS 6/7

With root or sudo permissions, run the following commands:

SUSE Linux Enterprise Server 11

With root or sudo permissions, run the following commands:

Test local package installation

As a verification step, call functions from the revoscalepy package and from scikit, included in Anaconda.

If you get a «module not found» error for any of the instructions below, verify you are loading the python interpreter from the right location. If using Visual Studio, confirm that you selected the custom environment pointing the prefix and interpreter paths to the correct location.

On Windows, depending on how you run the script, you might see this message: «Express Edition will continue to be enforced». Express edition is one of the free SQL Server editions. This message is telling you that client libraries are licensed under the Express edition. Limits on this edition are the same as Standard: in-memory data sets and 2-core processing. Remote servers typically run higher editions not subjected to the same memory and processing limits. When you push the compute context to a remote server, you work under the full capabilities of that system.

Create some data to work with. This example loads the iris data set using scikit.

Print out the dataset. You should see a 4-column table with measurements for sepal length, sepal width, petal length, and petal width.

Load revoscalepy and calculate a statistical summary for data in one of the columns. Print the output to view mean, standard deviation, and other measures.

Results

Next steps

Now that you have installed local client libraries and verified function calls, try the following walkthroughs to learn how to use the libraries locally and remotely when connected to resident data stores.

Remote access to a SQL Server is enabled by an administrator who has configured ports and protocols, enabled remote connections, and assigned user logins. Check with your administrator to get a valid connection string when using a remote compute context to SQL Server.

Оцените статью