<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Control WebPanel Wiki &#187; Python</title>
	<atom:link href="https://wiki.centos-webpanel.com/category/python/feed" rel="self" type="application/rss+xml" />
	<link>https://wiki.centos-webpanel.com</link>
	<description>CentOS WebPanel Wiki</description>
	<lastBuildDate>Wed, 12 Feb 2025 20:38:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>Install Django</title>
		<link>https://wiki.centos-webpanel.com/install-django</link>
		<comments>https://wiki.centos-webpanel.com/install-django#comments</comments>
		<pubDate>Wed, 06 May 2020 11:14:34 +0000</pubDate>
		<dc:creator><![CDATA[publisher]]></dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Install Django]]></category>

		<guid isPermaLink="false">http://wiki.centos-webpanel.com/?p=1369</guid>
		<description><![CDATA[Django is a free and open-source high-level Python Web framework. Being a Python Web framework, Django requires Python What Python version should I use with Django? Python 3 is recommended. Django 1.11 is the last version to support Python 2.7. Support for Python 2.7 and Django 1.11 ends in 2020. Since newer versions of Python [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Django is a free and open-source high-level Python Web framework.<br />
Being a Python Web framework, Django requires Python</p>
<p><strong>What Python version should I use with Django?</strong></p>
<p>Python 3 is recommended. Django 1.11 is the last version to support Python 2.7. Support for Python 2.7 and Django 1.11 ends in 2020. Since newer versions of Python are often faster, have more features, and are better supported, the latest version of Python 3 is recommended.</p>
<p>You don’t lose anything in Django by using an older release, but you don’t take advantage of the improvements and optimizations in newer Python releases.</p>
<table style="width:100%">
<tr>
<th>Django versions</th>
<th>Python versions</th>
</tr>
<tr>
<td>1.11</td>
<td>2.7, 3.4, 3.5, 3.6, 3.7</td>
</tr>
<tr>
<td>2.0</td>
<td>3.4, 3.5, 3.6, 3.7</td>
<tr>
<td>2.1, 2.2</td>
<td>3.5, 3.6, 3.7</td>
</tr>
</tr>
</table>
<p>&nbsp;<br />
<strong>Installation steps</strong></p>
<p><strong>1. Install the latest version of python</strong></p>
<p><strong>a) Prepare Your System</strong></p>
<pre>
# yum -y groupinstall development
# yum -y install zlib-devel libffi-devel
</pre>
<p><strong>b) Download python Source Code</strong></p>
<pre>
# cd /usr/local/src
# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
</pre>
<p><strong>c) Build Python</strong></p>
<pre>
# cd /usr/local/src
# tar xvf Python-3.7.3.tgz
# cd Python-3.7.3
# ./configure --with-ensurepip=install --enable-shared
# make
# make install
</pre>
<p>After installation please create a symlink for the shared object library by using the following command:</p>
<pre>ln -s /usr/local/lib/libpython3.7m.so.1.0 /usr/lib64/libpython3.7m.so.1.0</pre>
<p>Upgrade pip</p>
<pre>pip3.7 install --upgrade pip</pre>
<p><strong>Note:</strong> Please note that after installation, the new python binary path will be located at /usr/local/bin/python</p>
<p><strong>2. Installing Django</strong><br />
The recommended way to install Django is to use pip<br />
&nbsp;<br />
<strong> Install django with the following command:</strong></p>
<pre># pip3.7 install django</pre>
<p>This will install the latest version of django.To install a particular version of django,for example version 2.0</p>
<pre># pip3.7 install django==2.0</pre>
<p>To tell Django is installed and which version, run the following command in a shell prompt</p>
<pre># python3.7 -m django --version</pre>
<p>You can also confirm it’s installed correctly type:</p>
<p><strong>3. Installing Django in Python Virtual Enviroment</strong><br />
&nbsp;<br />
The steps above are for installing Django globally. However, Django can be installed into a virtualenv. Virtualenv is a tool used to create an isolated Python environment. This environment has its own installation directories that don't share libraries with other virtualenv environments (and optionally doesn't access the globally installed libraries either)<br />
&nbsp;<br />
<strong>Installing Virtualenv using pip3</strong></p>
<pre>#pip3.7 install virtualenv </pre>
<p><strong>3.1. You'll need the full path to the Python 3 version of virtualenv, so run the following to view it:</strong></p>
<pre>#which virtualenv</pre>
<p><strong>3.2. Navigate to your site's directory, where you'll create the new virtual environment:</strong></p>
<pre>#cd /home/username</pre>
<p><strong>3.3. Create the virtual environment while you specify the version of Python you wish to use. </strong></p>
<p>The following command creates a virtualenv named 'project1' and uses the -p flag to specify the full path to the Python3 version you just installed</p>
<pre>#virtualenv -p /usr/local/bin/python3.7 project1</pre>
<p>This command creates a local copy of your environment specific to this website. While working on this website, you should activate the local environment in order to make sure you're working with the right versions of your tools and packages.</p>
<p><strong>3.4. To activate the new virtual environment, run the following:</strong></p>
<pre># source project1/bin/activate</pre>
<p>The name of the current virtual environment appears to the left of the prompt. For example:</p>
<pre>(project1) [root@server]#</pre>
<p>Within the environment, install the Django package using pip. Installing Django allows us to create and run Django applications</p>
<pre>(project1) [root@server]# pip3.7 install django</pre>
<p>You can also install all other packages in the virtual environment that you may need in your development.</p>
<p>For Example, you can install mysqlclient by using the following command:</p>
<pre>(project1) [root@server]# pip3.7 install mysqlclient</pre>
<p>Going forward, whenever you create new Django projects you simply need to create a new virtual environment, install a fresh version of Django and other packages that may be needed.</p>
<p><strong>3.5. Deactivating your virtualenv</strong></p>
<p>When finished working in the virtual environment, you can deactivate it by running the following:</p>
<pre>(project1) [root@server]# deactivate</pre>
<p>This puts you back into your Shell user's default settings.<br />
If you want to go back into your virtualenv, please refer to <strong>3.4</strong> above.</p>
]]></content:encoded>
			<wfw:commentRss>https://wiki.centos-webpanel.com/install-django/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Deploy Django Apps using Apache with mod-wsgi and nginx</title>
		<link>https://wiki.centos-webpanel.com/how-to-deploy-django-apps-using-apache-with-mod-wsgi-and-nginx</link>
		<comments>https://wiki.centos-webpanel.com/how-to-deploy-django-apps-using-apache-with-mod-wsgi-and-nginx#comments</comments>
		<pubDate>Wed, 06 May 2020 11:04:50 +0000</pubDate>
		<dc:creator><![CDATA[publisher]]></dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Django wsgi]]></category>

		<guid isPermaLink="false">http://wiki.centos-webpanel.com/?p=1366</guid>
		<description><![CDATA[Django is a free and open-source high-level Python Web framework. Being a Python Web framework, Django requires Python. In this guide, we will demonstrate how to install and configure Django. We will then set up Nginx and Apache to sit in front of our Django application. We will also be using the mod_wsgi Apache module [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Django is a free and open-source high-level Python Web framework. Being a Python Web framework, Django requires Python.<br />
In this guide, we will demonstrate how to install and configure Django. We will then set up Nginx and Apache to sit in front of our Django application.</p>
<p>We will also be using the mod_wsgi Apache module that can communicate with Django over the WSGI interface specification.</p>
<p>1. Install the latest version of python and Django<br />
Please refer to <a href="/install-django">/install-django</a> for a guide on how to install python and Django.</p>
<p>2. Install mod_wsgi</p>
<p>a) Download latest version of mod_wsgi. As of writing, the latest version was <strong>4.6.5</strong></p>
<pre># cd /usr/local/src
# wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.5.zip
</pre>
<p>b) Unzip and build mod-wsgi with the version of python installed in step 1 above</p>
<pre># cd /usr/local/src
# unzip 4.6.5.zip
# cd mod_wsgi-4.6.5
# ./configure ./configure --with-apxs=/usr/local/apache/bin/apxs --with-python=/usr/local/bin/python3.7
# make
# make install
</pre>
<p>c) Add mod_wsgi module to httpd.conf file</p>
<pre>echo "LoadModule wsgi_module modules/mod_wsgi.so" &gt;&gt; /usr/local/apache/conf/httpd.conf</pre>
<p>d) Restart Apache</p>
<pre>systemctl restart httpd</pre>
<p>e)Check if mod_wsgi is loaded</p>
<pre>/usr/local/apache/bin/httpd -M | grep wsgi</pre>
<p>3. Setup and Configure Django</p>
<p>a) Create account to host Django App</p>
<p>With Django installed in step 1, we will create an account on CWP to host our Django application.<br />
CWP.Admin Left Menu -&gt; User accounts -&gt; New Account -&gt; fill the details and click create<br />
Now we Create a Django project called myproj using django-admin tool in your Docroot.</p>
<pre># cd /home/username/public_html/
# django-admin startproject myproj .
</pre>
<p>The above commands will create a file - manage.py and a folder called myproj inside /home/username/public_html</p>
<p>b) Alter project settings<br />
The first thing we should do with our newly created project files is to alter the settings.</p>
<pre># cd /home/username/public_html/myproj
# nano settings.py
</pre>
<p>i) Locate <strong>ALLOWED_HOSTS</strong>.Inside the square brackets, enter your server's public IP address, domain name or both. Each value should be wrapped in quotes and separated by a comma like a normal Python list:</p>
<p><strong>ALLOWED_HOSTS = ["server_domain_or_IP"]</strong><br />
eg : <code>ALLOWED_HOSTS = ["8.8.8.8"]</code></p>
<p>ii) At the bottom of the file, we will add a line to configure this directory. Django uses the STATIC_ROOT setting to determine static files directory, where Django will place static files so that the webserver can serve these easily.Put the following under the <strong>STATIC_URL = '/static/'</strong></p>
<p><strong>STATIC_ROOT = '/home/username/public_html/static'</strong></p>
<p>Note: Please make sure the static directory is created in the location specified above.</p>
<p>iii) Setup MySQL Database.<br />
CWP.user left menu –&gt; SQL services –&gt; MySQL Manager –&gt; Create database and database user.<br />
Enter the database name and the password that will be used for the user.<br />
After creation, click on List Databases and users, you will see the database name and database user(created automatically)</p>
<p>Locate the Database section in the settings.py file and update it with the following.<br />
By default, Django used SQLite, which you can use but we are using MySQL in this guide.</p>
<p>replace sqlite database backend</p>
<pre>DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
</pre>
<p>with Mysql database engine backend :</p>
<p><code>DATABASES = {<br />
'default': {<br />
'ENGINE': 'django.db.backends.mysql',<br />
'NAME': 'mydatabase',<br />
'USER': 'mydatabaseuser',<br />
'PASSWORD': 'mypassword',<br />
'HOST': '127.0.0.1',<br />
}<br />
}</code></p>
<p>Save and close the file when you are finished.</p>
<p>ensure you've python mysqlclient installed</p>
<pre>pip3.7 install mysqlclient
</pre>
<p>c) Complete Initial Project Setup<br />
Now, we can migrate the initial database schema to our mysql database using the management script:</p>
<pre># cd /home/username/public_html
# python3.7 manage.py  makemigrations
# python3.7 manage.py  migrate
</pre>
<p>Create an administrative user for the project by typing:</p>
<pre># cd /home/username/public_html
# python3.7 manage.py createsuperuser
</pre>
<p>You will have to provide a username, an email address, and choose and confirm a password.</p>
<p>We can collect all of the static content into the directory location we configured by running the command:</p>
<pre># cd /home/username/public_html
# python3.7 manage.py collectstatic
</pre>
<p>Now, you can test your project by starting up the Django development server with the following command:</p>
<pre># cd /home/username/public_html
# python3.7 manage.py runserver 0.0.0.0:8000
</pre>
<p>In your web browser, visit <strong>http://server_domain_or_IP:8000</strong> and you should see the default Django index page</p>
<p>4. Configure Apache and Nginx<br />
CWP.Admin left Menu -&gt; Webserver settings -&gt; Webserver Domain Conf -&gt; Select Username -&gt; create configuration -&gt; select the option - nginx-&gt;apache-&gt;proxy(custom port)</p>
<p>leave custom port as 8181 or you can change it.leave IP as 127.0.0.1, save changes.</p>
<p>a) Configure apache<br />
Edit the vhost file for your domain in /usr/local/apache/conf.d/vhosts/ and add the following inside the virtual host container</p>
<p><code>WSGIScriptAlias / /home/username/public_html/myproj/wsgi.py<br />
&lt;Directory "/home/username/public_html/myproj"&gt;<br />
&lt;Files wsgi.py&gt;<br />
Require all granted<br />
&lt;/Files&gt;<br />
&lt;/Directory&gt;</code></p>
<p><strong>Note :</strong> Please replace <strong>username</strong> and and <strong>project name(myproj)</strong> as per your setup as this is a guide.</p>
<p>Any request to Apache will execute the WSGI Script<br />
To tell mod_wsgi how to handle the requests, modify the WSGI file located at /home/username/public_html/myproj/wsgi.py as per your setup:</p>
<p><code>import os<br />
<strong>import sys</strong> #Add this<br />
from django.core.wsgi import get_wsgi_application<br />
<strong>sys.path.append('/home/username/public_html/')</strong> #Add this also<br />
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproj.settings')<br />
application = get_wsgi_application()</code></p>
<p>b) Configure Nginx<br />
Edit the vhost file for your domain in /etc/nginx/conf.d/vhosts/ and add the following inside the server container</p>
<p><code>location /static/ {<br />
root /home/username/public_html/;<br />
expires 1d;<br />
}</code></p>
<p>c) Restart Apache and Nginx services</p>
<pre> systemctl restart nginx httpd</pre>
<p>d)In your web browser, visit <strong>http://yourdomain</strong> and you should see the default Django index page.<br />
Django is running successfully and you can continue with creating Django applications.</p>
]]></content:encoded>
			<wfw:commentRss>https://wiki.centos-webpanel.com/how-to-deploy-django-apps-using-apache-with-mod-wsgi-and-nginx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache run python script</title>
		<link>https://wiki.centos-webpanel.com/apache-run-python-script</link>
		<comments>https://wiki.centos-webpanel.com/apache-run-python-script#comments</comments>
		<pubDate>Thu, 14 Jan 2016 23:13:54 +0000</pubDate>
		<dc:creator><![CDATA[publisher]]></dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[apache run python script]]></category>

		<guid isPermaLink="false">http://wiki.centos-webpanel.com/?p=446</guid>
		<description><![CDATA[Install python &#038; python3 and run python scripts with apache on your CentOS WebPanel server. Install python yum install python Install python3 yum install python3 Check python version python -V Check python3 version python3 -V Create a test script cd /home/USERNAME/public_html mkdir cgi-bin nano cgi-bin/test.py Python2 test script in the file: /home/USERNAME/public_html/cgi-bin/test.py #!/usr/bin/python # enable [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Install python &#038; python3 and run python scripts with apache on your CentOS WebPanel server.</p>
<p><strong>Install python</strong></p>
<pre>yum install python</pre>
<p><strong>Install python3</strong></p>
<pre>yum install python3</pre>
<p><strong>Check python version</strong></p>
<pre>python -V</pre>
<p><strong>Check python3 version</strong></p>
<pre>python3 -V</pre>
<p><strong>Create a test script</strong></p>
<pre>
cd /home/USERNAME/public_html
mkdir cgi-bin
nano cgi-bin/test.py
</pre>
<p>Python2 test script in the file:<br />
<strong>/home/USERNAME/public_html/cgi-bin/test.py</strong></p>
<pre>
#!/usr/bin/python
# enable debugging
import cgitb
cgitb.enable()
print "Content-Type: text/plain\r\n\r\n"
print
print "Hello World!"
</pre>
<p>Python3 test script in the file:<br />
<strong>/home/USERNAME/public_html/cgi-bin/test.py</strong></p>
<pre>
#!/usr/bin/python3
# enable debugging
import cgitb
cgitb.enable()
print ("Content-Type: text/plain\r\n\r\n")
print
print ("Hello World!")
</pre>
<p><strong>Update test.py file and folder cgi-bin permissions</strong></p>
<pre>chown -R USERNAME.USERNAME /home/USERNAME/public_html/cgi-bin
chmod +x /home/USERNAME/public_html/cgi-bin/test.py
</pre>
<p>Now create .htaccess file to handle this script.<br />
Add this test code bellow in your file <strong>/home/USERNAME/public_html/cgi-bin/.htaccess</strong></p>
<pre>
Options +ExecCGI
AddHandler cgi-script .py
</pre>
<div class="st-alert st-alert- "><a href="/how-to-enable-mod_suexec-with-apache" title="How to enable mod_suexec with CentOS WebPanel" target="_blank">Please don't forget to enable mod_suexec for security reasons if you will have with multiple sites.</a>
</div>
<p><strong>Enable CGI</strong><br />
Create File: /usr/local/apache/conf.d/mod_cgid.conf</p>
<pre>LoadModule cgid_module modules/mod_cgid.so

&lt;IfModule cgid_module&gt;
&lt;Directory /home/*/public_html/cgi-bin/&gt;
Options ExecCGI SymLinksifOwnerMatch
SetHandler cgi-script
AddHandler cgi-script .cgi .pl .py
Require all granted
AllowOverride All
&lt;/Directory&gt;
&lt;/IfModule&gt;
</pre>
<p>Now restart apache and you are ready to run your cgi-scripts.</p>
<pre>service httpd restart</pre>
<p>Test if cgid module is now loaded</p>
<pre>/usr/local/apache/bin/httpd -M|grep cgid</pre>
<p><strong>Test if suexec module is now loaded</strong></p>
<pre>/usr/local/apache/bin/httpd -M|grep suexec</pre>
<p>If module is loaded, you should get output containing this: <strong>suexec_module (shared)</strong><br />
<a href="http://wiki.centos-webpanel.com/how-to-enable-mod_suexec-with-apache" title="How to enable mod_suexec with CentOS WebPanel" target="_blank">http://wiki.centos-webpanel.com/how-to-enable-mod_suexec-with-apache</a></p>
<p><em>Running cgi scripts requires some security so don't forget to check this instructions</em><br />
<a href="http://wiki.centos-webpanel.com/how-to-run-cgi-scripts-with-apache" title="How to secure cgi scripts with suexec" target="_blank">http://wiki.centos-webpanel.com/how-to-run-cgi-scripts-with-apache</a></p>
<p><strong>Now you can run your python script by using URL</strong></p>
<pre>http://domain.com/cgi-bin/test.py</pre>
]]></content:encoded>
			<wfw:commentRss>https://wiki.centos-webpanel.com/apache-run-python-script/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
