Como Instalar Python, Apache y Mysql en Ubuntu 14.04

Download Como Instalar Python, Apache y Mysql en Ubuntu 14.04

Post on 06-Sep-2015

15 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Como Instalar Python, Apache y Mysql en Ubuntu 14.04

TRANSCRIPT

<ul><li><p> Sign UpLog InCommunity Tutorials Questions Projects</p><p>How To Set Up an Apache, MySQL, andPython (LAMP) Server Without Frameworkson Ubuntu 14.04Tags: LAMP Stack, Apache, MySQL, Python Distribution: Ubuntu</p><p>IntroductionThis article will walk you through setting up a server with Python 3, MySQL, and Apache2, sans the help ofa framework. By the end of this tutorial, you will be fully capable of launching a barebones system intoproduction.</p><p>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 avanilla system without this powerful tool can be tricky, but earns you invaluable insight into server structurefrom the ground up.</p><p>This tutorial uses only package installers, namely apt-get and Pip. Package installers are simply smallprograms that make code installations much more convenient and manageable. Without them, maintaininglibraries, modules, and other code bits can become an extremely messy business.</p><p>PrerequisitesTo follow this tutorial, you will need:</p><p>One Ubuntu 14.04 Droplet.</p><p>A sudo non-root user, which you can set up by following this tutorial.</p><p>Step 1 Making Python 3 the DefaultIn this step, we will set Python 3 as the default for our python command.</p><p>Alvin WanApr 29, 2015 5 3 Share Contents</p><p> Menu</p><p>How To Set Up an Apache, MySQL, and Python ... https://www.digitalocean.com/community/tutorial...</p><p>1 de 9 28/06/15 18:17</p></li><li><p>First, check your current Python version.</p><p>$ python --version</p><p>On a fresh Ubuntu 14.04 server, this will output:</p><p>Python 2.7.6</p><p>We would like to have python run Python 3. So first, let's remove the old 2.7 binary.</p><p>$ sudo rm /usr/bin/python</p><p>Next, create a symbolic link to the Python 3 binary in its place.</p><p>$ sudo ln -s /usr/bin/python3 /usr/bin/python</p><p>If you run python --version again, you will now see Python 3.4.0 .</p><p>Step 2 Installing PipIn this section, we will install Pip, the recommended package installer for Python.</p><p>First, update the system's package index. This will ensure that old or outdated packages do not interferewith the installation.</p><p>$ sudo apt-get update</p><p>Pip allows us to easily manage any Python 3 package we would like to have. To install it, simply run thefollowing:</p><p>$ sudo apt-get install python3-pip</p><p>For an overview of Pip, you can read this tutorial.</p><p>Step 3 Installing MySQLIn this section, we will install and configure MySQL.</p><p>How To Set Up an Apache, MySQL, and Python ... https://www.digitalocean.com/community/tutorial...</p><p>2 de 9 28/06/15 18:17</p></li><li><p>Installing SQL is simple:</p><p>$ sudo apt-get install mysql-server</p><p>Enter a strong password for the MySQL root user when prompted, and remember it, because we will needit later.</p><p>The MySQL server will start once installation completes. After installation, run:</p><p>$ mysql_secure_installation</p><p>This setup will take you through a series of self-explanatory steps. First, you'll need to enter the rootpassword 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.</p><p>Python 3 requires a way to connect with MySQL, however. There are a number of options, likeMySQLclient, but for the module's simplicity, this tutorial will use pymysql . Install it using Pip:</p><p>$ sudo pip3 install pymysql</p><p>Step 4 Installing Apache 2In this section, we will install Apache 2, and ensure that it recognizes Python files as executables.</p><p>Install Apache using apt-get:</p><p>$ sudo apt-get install apache2</p><p>Like MySQL, the Apache server will start once the installation completes.</p><p>Note: After installation, several ports are open to the internet. Make sure to see the conclusion of thistutorial for resources on security.</p><p>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 samelocation.</p><p>$ sudo mkdir /var/www/test</p><p>How To Set Up an Apache, MySQL, and Python ... https://www.digitalocean.com/community/tutorial...</p><p>3 de 9 28/06/15 18:17</p></li><li><p>Finally, we must register Python with Apache. To start, we disable multithreading processes.</p><p>$ sudo a2dismod mpm_event</p><p>Then, we give Apache explicit permission to run scripts.</p><p>$ sudo a2enmod mpm_prefork cgi</p><p>Next, we modify the actual Apache configuration, to explicitly declare Python files as runnable file andallow such executables. Open the configuration file using nano or your favorite text editor.</p><p>$ sudo nano /etc/apache2/sites-enabled/000-default.conf</p><p>Add the following right after the first line, which reads .</p><p> Options +ExecCGI DirectoryIndex index.py</p><p>AddHandler cgi-script .py</p><p>Make sure that your block is nested inside the block, like so. Make sureto indent correctly with tabs, too.</p><p>/etc/apache2/sites-enabled/000-default.conf</p><p>Options +ExecCGIDirectoryIndex index.py</p><p>AddHandler cgi-script .py</p><p> ...</p><p>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 definesthe executables.</p><p>How To Set Up an Apache, MySQL, and Python ... https://www.digitalocean.com/community/tutorial...</p><p>4 de 9 28/06/15 18:17</p></li><li><p>We also want to allow executables in our website directory, so we need to change the path forDocumentRoot , too. Look for the line that reads DocumentRoot /var/www/html , a few lines below thelong comment at the top of the file, and modify it to read /var/www/test instead.</p><p>DocumentRoot /var/www/test</p><p>Your file should now resemble the following.</p><p>/etc/apache2/sites-enabled/000-default.conf</p><p>Options +ExecCGIDirectoryIndex index.py</p><p>AddHandler cgi-script .py</p><p> ...</p><p>DocumentRoot /var/www/test</p><p> ...</p><p>Save and exit the file. To put these changes into effect, restart Apache.</p><p>$ sudo service apache2 restart</p><p>Note: Apache 2 may throw a warning which says about the server's fully qualified domain name; this can beignored as the ServerName directive has little application as of this moment. They are ultimately used todetermine subdomain hosting, after the necessary records are created.</p><p>If the last line of the output reads [ OK ] , Apache has restarted successfully.</p><p>Step 5 Testing the Final ProductIn this section, we will confirm that individual components (Python, MySQL, and Apache) can interact withone another by creating an example webpage and database.</p><p>First, let's create a database. Log in to MySQL. You'll need to enter the MySQL root password you setearlier.</p><p>How To Set Up an Apache, MySQL, and Python ... https://www.digitalocean.com/community/tutorial...</p><p>5 de 9 28/06/15 18:17</p></li><li><p>$ mysql -u root -p</p><p>Add an example database called example.</p><p>mysql&gt; CREATE DATABASE example;</p><p>Switch to the new database.</p><p>mysql&gt; USE example;</p><p>Add a table for some example data that we'll have the Python app add.</p><p>mysql&gt; CREATE TABLE numbers (num INT, word VARCHAR(20));</p><p>Press CTRL+D to exit. For more background on SQL, you can read this MySQL tutorial.</p><p>Now, create a new file for our simple Python app.</p><p>$ sudo nano /var/www/test/index.py</p><p>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.</p><p>#!/usr/bin/python</p><p># Turn on debug mode.import cgitbcgitb.enable()</p><p># Print necessary headers.print("Content-Type: text/html")print()</p><p># Connect to the database.import pymysqlconn = pymysql.connect( db='example', user='root', passwd='your_root_mysql_password', host='localhost')</p><p>How To Set Up an Apache, MySQL, and Python ... https://www.digitalocean.com/community/tutorial...</p><p>6 de 9 28/06/15 18:17</p></li><li><p>c = conn.cursor()</p><p># 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()</p><p># Print the contents of the database.c.execute("SELECT * FROM numbers")print([(r[0], r[1]) for r in c.fetchall()])</p><p>Save and exit.</p><p>Next, fix permissions on the newly-created file. For more information on the three-digit permissions code,see the tutorial on Linux permissions.</p><p>$ sudo chmod 755 /var/www/test/index.py</p><p>Now, access your server's by going to http://your_server_ip using your favorite browser. You shouldsee the following:</p><p>http://your_server_ip</p><p>[(1, 'One!'), (2, 'Two!'), (3, 'Three!')]</p><p>Congratulations! Your server is now online.</p><p>ConclusionYou now have a working server that can run Python 3 with a robust, SQL database. The server is now alsoconfigured for easy maintenance, via well-documented and established package installers.</p><p>However, in its current state, the server is vulnerable to outsiders. Whereas elements like SSL encryptionare not essential to your server's function, they are indispensable resources for a reliable, safe server. Learnmore by reading about how to configure Apache, how to create an Apache SSL certificate and how tosecure your Linux server.</p><p> ShareHeart 5 Subscribe</p><p>How To Set Up an Apache, MySQL, and Python ... https://www.digitalocean.com/community/tutorial...</p><p>7 de 9 28/06/15 18:17</p></li><li><p>Author:Alvin Wan</p><p>Editor:Hazel Vird</p><p>Spin up an SSD cloud server in under a minute.Simple setup. Full root access. Straightforward pricing.</p><p>Related TutorialsHow To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Debian 8</p><p>How to Back Up a LAMP Server with Bacula on Ubuntu 14.04Building for Production: Web Applications DeployingBuilding for Production: Web Applications Overview</p><p>How to use the PHPMyAdmin One-Click Application Image</p><p>3 Comments</p><p>Log In to Comment</p><p>How To Set Up an Apache, MySQL, and Python ... https://www.digitalocean.com/community/tutorial...</p><p>8 de 9 28/06/15 18:17</p></li><li><p>Copyright 2015 DigitalOcean Inc.</p><p>Community Tutorials Questions Projects Tags</p><p>Terms, Privacy, &amp; Copyright Security Get Paid to Write</p><p>This work is licensed under a CreativeCommons Attribution-NonCommercial-ShareAlike 4.0 International License.</p><p>katherine May 3, 2015excellent, thank you!</p><p>Reply 1</p><p>pi June 4, 2015Wait, there is some acronym abuse going on here. The P in LAMP is PHP. People trying to manuallyinstall a mediawiki from scratch will get very confused. LAMPy would make sense...</p><p>Reply</p><p>jonppenny June 21, 2015While the "P" in lamp is largely recognised as standing for PHP, it can also stand forPython or Perl. I do agree it can get confusing, especially for people new to LAMP.</p><p>How To Set Up an Apache, MySQL, and Python ... https://www.digitalocean.com/community/tutorial...</p><p>9 de 9 28/06/15 18:17</p></li></ul>