Which folder on AWS EC2 for my private Python scripts? - python

I have an AWS EC2 (Amazon Linux) instance configured with a LAMP web server on it as well. On top of having a small website hosted there I'd like it to run in the background a few Python scripts with cron.
Those scripts are private and separate from the website, so I don't want them in the /var/www/html folder.
On my personal computer those would stay in my User's Documents folder, but being new at having a server (and linux) I don't know where to put them.
By common practice, is there a specific folder I should use ?

Related

How can I work on a python project hosted on a remote server using Pycharm?

I have a server I connect to using SFTP, and I've followed these steps to edit individual files hosted on the server.
But I have an entire python project structure on the server, and I'd like PyCharm's suggestions/auto-complete to be aware of the other files/directories in said project.
Is there anyway to achieve this using PyCharm? I feel like file mappings are the answer, but I don't see any way to download a remote directory onto a local drive and then setup a synchronization.
Also there are some resources I don't want to download onto my local environment, so if there was a way to do this remote only, so much the better.

How to write AWS Deployment script to launch AWS instance

I am looking to launch an AWS instance by deploying a script. However, I do not fully understand what this means. What should be in the script in order to launch it and how do I approach this in order to meet the following requirements?
User specifies AWS credentials in a separate key file;
User invokes termination script and pass the instance ID from
command line;
Termination script shuts down AWS instance.
Upon completion, the termination script returns message indicating
whether the termination process has been completed successfully
I would appreciate some help in understanding what exactly a deployment script it and what language I should write it in. I have been coding thus far in Python and have created a script that creates an instance. But I am not sure how this is different from deploying an instance.
The usage of the expressions "create an instance" and "deploy an instance" can mean the same thing or different things. Depends on the engineer's viewpoint.
Basically creating an EC2 instance means the AWS definition of launching an EC2 instance. Deploying an EC2 instance may include additional configuration details such as patching the OS, installing software and applications, etc. It is up to you to decide which is which and how each should be done.
When deploying an EC2 instance, I prefer to configure a machine exactly the way that I want with OS patches, software and my applications. Then I create an AMI. When I then launch a new EC2 instance, I use my hand created AMI. Then the new EC2 instance is exactly what I want. No long deployment phase.
Best practices when writing scripts. Do not store your Amazon credentials in your scripts, source code, random files, etc. Install the Amazon CLI (Command Line Internface) tool and then configure the CLI with your credentials. Now your credentials are stored in a well defined location with the added benefit that Amazon SDKs, scripts, etc. will know how to find the credentials and will automatically load and use them.
The easiest way of writing scripts to manage AWS services is to use the AWS CLI. Just about anything that you can do in the Amazon Management Console, you can do with the CLI. The CLI works on Windows, Linux and Mac OS.
AWS Command Line Interface
Here is a CLI example that will terminate an EC2 instance. Replace with your instance ID:
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
Writing your scripts in Python is another good idea. Managing AWS services with Python is very easy; there are lots of examples available on the Internet; and Python is just so easy and quick to develop Amazon apps. Use the Boto3 library and not the older Boto library. I use Python 3.x for all new development, but be aware that there is a lot of already created work on the Internet for AWS that runs under Python 2.x.
CLI EC2 Commands

Deploy Python webApp on AWS (without using Elastic Beanstalk)

I'm hosting a website on AWS. Its a web interface with a SQL database. The website will be used to:
1. View results of query from Database
2. Insert data into database
3. View the data and update it where needed.
The codes and connections works file when I run the application on localhost (Apache on my C drive). But we want to host it on AWS so that people around me can use it.
So, In AWS I uploaded the code on EC2 and installed apache on it, all the html links are working but the python file is simply displaying the code.
I'm guessing it has something to do with the shebang. Currently my code has the following shebang:
#!C:\Python27\python.exe
Can someone guide me if its the shebang or if there is something else i need to do.
I have installed boto, but not sure what to do next. The AWS website and most of the forums talk about using Elastic Beanstalk. I want to host a fully functioning Python webApp on AWS without using Elastic Beanstalk.
When apache displays code, that is a clear sign that Apache is not configured properly to execute python. You should look to see if mod_python is installed and configured correctly.
Also, #! is generally used with Linux not windows. If apache/mod_python is installed and configured correctly I can't imagine what code you'd have that would need #! since the .py extension would be enough.
IF your EC2 instance is indeed running Linux, and your code does indeed need #! try:
#!/bin/python
OR
#!/usr/local/bin/python
(Depends on where the python binary is, and those are the most common locations.)
If your EC2 instance is running Windows then "Unless you are using cygwin, windows has no shebang support"
Hi have you logged into your EC2 instance through the endpoint and then run your script, from the command line. I have some experience with EC2 running apache2 only my application was written in Java, having previously used python scripts I was able to run them by logging into my EC2 instance, you can do this from AWS management console. hope this helps you somewhat.

Create local files from a Django web

I have written a Django web run under Linux server, the web is used to read a
file path and generate some folders and files on the local machine which browse
the web, not sure how to do that, currently it can only generate the files on
the server runs the web locally.
Does it need to use the python package paramiko?
Thanks,
Le
Django runs on server and every web server has no access to local (user) computer - it can't create folder on local computer (and it can't steal your files). It can only generate file and user can download it.

Apache: How to set default document to a python script?

I have apache running on a Raspberry Pi. My website is using running Python scripts. How do I make, say, start.py, as the default document for the site? In other words, if the host name for the Raspberry Pi is pi, I want my users to just use http://pi and start.py will execute.
I would use apache's mod_fastcgi module, mapping a virtual host in apache to a script folder containing python scripts. Note however, this is far from production quality, or secure! However, if your Pi is on a trusted intranet network such as a home, it should be fine.
Here's one blog which spells it out, step by step: http://raspberryserver.blogspot.com/. The final step in the tutorial is to install apc, which you can skip. It's for PHP.
Essentially, you'll be able to drop python files into the /var/www/fastcgi folder, and profit.

Categories