Categories
Uncategorized

Set up a Django Project on EC2 From Scratch

I am a total newbie at Python, so I am writing this blog just to document my journey/effort into setting up a Django project on an EC2 instance!

I set up an EC2 instance with the latest publicly available Ubuntu machine image. For this project, I chose a t1.micro instance.

It is highly recommended that Python projects be set up in Virtual Environments. A Python VE is noting but a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. The reason this is recommended is because you may write multiple python apps and each app may end up using different versions of python modules and packages. If one of your Python app is using version 1.0 of a particular package and another app is using version 2.0 of the same package and both these packages are installed in the same environment; one of the apps will break due to a conflict in package versions.
However, in a virtual environment , each package’s version is isolated from other versions and hence there cannot be such a clash.

Since each app will have its own virtual environment , it is a good idea to have all your virtual environments in a hidden directory. For this I followed the official Python docs’ recommendation and created my first virtual environment as under:

python3 -m venv ~/.virtualenvs/testenv

Next, the virtual environment needs to be activated by running this command:

source ~/.virtualenvs/testenv/bin/activate

The above command “activates” the virtual environment and your shell prompt should now change to ‘testenv‘ . You are now working in a python virtual environment.

The next step is to actually install Django framework within this VE by simply running:

python3 -m pip install Django

Note that Django will get installed for THIS virtual environment and not globally! This is the whole point of creating a VE…django is now available as a local copy and if you create a new project down the road (months/years later), you will install a new copy/version of Django in a new virtual environment….without running into the danger of breaking any existing Django app/code!

Once Django is installed successfully, you can start your new Django project by running:

django-admin startproject mysite

This will install all the necessary files and create the dir structure needed by the Django app. However, this article is about running the Django app on an EC2 instance. You cannot simply run

python manage.py runserver

and expect to go to http://127.0.0.1:8000/ and see the “rocket launch” of an empty Django project! In order to run your project on an EC2 instance, you will need to modify the settings.py file under the ‘mysite’ directory:

sudo vi mysite/setting.py
Add the public IPV4 DNS of the EC2 on the ALLOWED HOSTS line:
ALLOWED _HOSTS = ['ec252.XX.XX.XXX.compute1.amazonaws.com']

Your EC2 security group must allow INBOUND TCP traffic on port 8000 before you start the server. Once that is done, run the following command and visit your EC2 in a browser and if all went well, you should see the Django rocket launched!

python3 manage.py runserver ec2.xxxxx.compute-1.amazonaws.com:8000

 1,484 total views,  1 views today

Categories
Uncategorized

Allow Chrooted User Access To Outside Directories

To allow access to a directory by a chrooted linux user (which lies outside the user’s dir tree) use “mount –bind sourceDirPath targetDirPath”

Mounting a directory means creating a link to the contents of the directory anywhere on the volume or even another volume. For example, if you have a D:/ and a C:/, one can create a link to the D:/ in the C:/ and access the contents of the D:/ as if it were a folder within the C:/

Using the bind –mount however, replicates one directory structure within the other (and not just created a link). Any changes made in one directory get reflected in the other directory immediately.

One use case: You are working on a project and want to give access to two different directories to a chrooted linux user. Furthermore, neither of the two directories are within the home directory of the chrooted user. This would not possible since the home directory of the chrooted user does not contain either of the dirs (that is the whole point of chrooting a user!).

Using “mount –bind” the root user can allow the chrooted user access to any number of dirs which are outside the home dir of this user.

To allow access to the outside dir, the root user must first create an empty dir under the chrooted user’s home dir with the same name and then bind the empty dir to the outside dir.

For example, assume the chrooted user’s home dir is /var/www/chr_dir and the root user wants the chrooted user to have access to /var/www/someDir.

sudo mkdir /var/www/chr_dir/someDir
mount --bind /var/www/someDir /var/www/chr_dir/someDir

Now when the chrooted user logs into his account (via FTP or SSH or otherwise); he will see someDir as one the directories with all the same contents as they are under /var/www/someDir.

To remove the user’s access to the directory, use the ‘umount’ command:

sudo umount /var/www/chr_dir/someDir

This will “unmount” the directory from the user’s home directory and the user will not have access to the contents of the outside directory anymore. Do note that the dir ‘someDir’ will still appear in the user’s account. Only its contents will get removed. To remove the actual directory itself, remove it using the “rm” command.

Source: https://askubuntu.com/questions/780406/is-mount-bind-dir1-dir2-an-alternative-for-hard-links-for-directories

 1,755 total views,  3 views today

Categories
Uncategorized

Add Node Packages Globally

When you install a new nodejs package, it will get installed in your home directory by default. This is usally /home/$USER where $USER is ‘centos’ on a Centos machine . In order to run a module GLOBALLY on your machine, you will need to install the package globally and set the NODE_PATH environment variable after the installation.

For example, to install ‘puppeteer’ globally run this command:

sudo npm install -g --save puppeteer --unsafe-perm=true --

This will install the ‘puppeteer’ package to /usr/lib/node_modules/puppeteer/

However, if you try to run a puppeteer script from anywhere on your server, you will get an error saying:

internal/modules/cjs/loader.js:895
throw err;  ^
Error: Cannot find module 'puppeteer'

To make puppeteer available globally on your machine simply add the path to the node_modules to your server’s environment variables like so:

For linux machines, you will need to add the line given (for each distro) to your bash profile. Do ‘sudo vi ~/.bash_profile ‘ ; add the line given below; save the file; then logout and login to the SSH environment for the changes to take effect:

  • Centos: export NODE_PATH=/usr/lib/node_modules
  • Ubuntu: export NODE_PATH=/usr/local/lib/node_modules
  • Windows 7/8*: Add to path C:\Users\{USERNAME}\AppData\Roaming\npm\node_modules
  • Windows 10*: Add to path %AppData%\npm\node_modules


*On windows system, you can add to your path by following these steps:

  • Open the Start Search, type in “env”, and choose “Edit the system environment variables”
  • Click the “Environment Variables…” button.
  • Under the “System Variables” section (the lower half), find the row with “Path” in the first column, and click edit.
  • The “Edit environment variable” UI will appear. Here, you can click “New” and type in the new path you want to add. From this screen you can also edit or reorder them.
  • Dismiss all of the dialogs by choosing “OK”. Your changes are saved!
  • Restart the machine (or at least the apps that require the path) in order for the changes to take effect.

 1,452 total views,  2 views today