Skip to content

Instantly share code, notes, and snippets.

@jgram925
Last active June 24, 2019 23:34
Show Gist options
  • Save jgram925/06350b219839a6bc5c38ca16ecefd202 to your computer and use it in GitHub Desktop.
Save jgram925/06350b219839a6bc5c38ca16ecefd202 to your computer and use it in GitHub Desktop.
Ubuntu Web Server Setup.md

Update & Upgrade Ubuntu

LINUX COMMAND: sudo apt-getupdate && sudo apt-get upgrade-y

Set a static IP address using netplan

  1. Navigate to /etc/netplan/

  2. Create a file name “01-netcfg.yaml”

     LINUX COMMAND: sudo nano 01-netcfg.yaml
    

    NOTE: nano is a text editor and can be swapped with other text editors such as vi or vim4.

  3. Depending on how the static network needs to be setup, there is a variety of variables that can be configured. A good resource for learning what needs to be set can be found on the official website at netplan.io.

  4. Save the file once completed and apply the settings.

     LINUX COMMAND: sudo netplan apply
    

Install all the packages needed for the environment

NOTE: Ubuntu Server 18.04.1 removed most of the repositories from the “sources.list” file. You will need to modify the file to include a fuller list of repositories and save the file.

LINUX COMMAND: sudo apt-get install -y nginx supervisor postgresql postgresql-contrib postgresql-server-dev-10 libpq-dev python3-dev libxml2-dev libxslt1-dev zlib1g-dev python3-pip python-pip

Setup virtualenvwrapper and virtualenv

  1. Install virtualenvwrapper using the command below.

     LINUX COMMAND: sudo pip install virtualenvwrapper
    

    NOTE: This should install it in the “/usr/local/bin/” directory. If you don’t use sudo it will install elsewhere.

  2. Create a directory for the virtual environments under your home directory.

     LINUX COMMAND: mkdir ~/envs
    
  3. Create a document name “.bash_aliases” under your home directory .

     LINUX COMMAND: nano ~/.bash_aliases
    
  4. Enter the below info into the text editor and then save.

     export WORKON_HOME=~/envs
    
     source /usr/local/bin/virtualenvwrapper.sh
    
  5. Run the bash script using the command below.

     LINUX COMMAND: source ~/.bash_aliases
    
  6. Install virtualenv using the command below.

     LINUX COMMAND: pip install vitrualenv
    
  7. Create a virtualenv in your “envs” folder under you home directory.

     LINUX COMMAND: mkvirtualenv ~/envs/<virtualenv_name>
    
  8. Test to make sure that it was successful by using the command below.

     LINUX COMMAND: workon <virtualenv_name>
    
  9. Exit the virtualenv using the command below.

     LINUX COMMAND: deactivate
    

Git a clone of the project from Gitlab

  1. Log into Gitlab, go to profile settings, click the key that says “SSH Keys”, and click “generate one” link.

  2. Copy the code at the top and paste it into the terminal. Follow the prompts to leave the directory the same and do not set a passphrase.

  3. Enter the command below and copy the whole key.

     LINUX COMMAND: cat ~/.ssh/id_rsa.pub
    
  4. Navigate back one page on Gitlab and paste the code into the “Key” section, change the “Title” to something that will make identifying the key in the future easy ,and click “Add Key”

  5. Navigate to the project you want to add to the server and copy the “SSH” code.

  6. In the terminal create a directory under your home directory.

     LINUX COMMAND: mkdir ~/<project folder name>
    
  7. In the terminal navigate to the new directory you just created then paste the “SSH” code from Gitlab after the command below.

     LINUX COMMAND: git clone <ssh link to project>
    
  8. Navigate to the “requirements.txt” in the project folder and then run the code below.

     LINUX COMMAND: pip install -r requirements.txt
    
  9. Create a copy of the settings file and modify the allowed hosts, the database section to match the database settings, and the static root location.

  10. Navigate to “~/envs/<env_name/bin/” and edit “postactivate.py”, then save.

    LINUX COMMAND: export DJANGO_SETTINGS_MODULE=’<project_name>.settings.<settings_name>
    
  11. Navigate to “~/envs/<env_name/bin/” and edit “postdeactivate.py”, then save.

    LINUX COMMAND: unset DJANGO_SETTINGS_MODULE
    

Setup Postgresql

  1. Login to Postgres using the command below.

     LINUX COMMAND: sudo -u postgres psql
    
  2. Create an empty database using the code below.

     POSTGRES COMMAND: CREATE DATABASE projectname;
    
  3. Create a database user using the code below.

     POSTGRES COMMAND: CREATE USER username WITH PASSWORD ‘password’;
    
  4. Configure the database using the code below.

     POSTGRES COMMAND: ALTER ROLE username SET client_encoding TO 'utf8';
    
     POSTGRES COMMAND: ALTER ROLE username SET default_transaction_isolation TO 'read committed';
    
     POSTGRES COMMAND: ALTER ROLE username SET timezone TO 'UTC';
    
  5. Grant all privileges to the database for the user.

     POSTGRES COMMAND: GRANT ALL PRIVILEGES ON DATABASE projectname TO username;
    
  6. Login to the project virtualenv and migrate to Postgres.

    NOTE: check your project root directory to see if dblite3 file was created, if so the migration to Postgres didn’t work and you’ll need to delete that file and start over.

  7. While logged into the project virtualenv create a super user, and test database.

     PYTHON COMMAND: python manage.py createsuperuser
    
  8. Check to see if your super user was created and the database is working.

     PYTHON COMMAND: python manage.py runserver 0.0.0.0:8000
    

Install & Setup Gunicorn

  1. Switch to your virtualenv using the command below.

     LINUX COMMAND: workon <virtualenv name>
    
  2. Enter the command below.

     LINUX COMMAND: pip install gunicorn
    
  3. Test that the Gunicorn server is running with the command below.

     LINUX COMMAND: gunicorn --bind 127.0.0.1:8000 calib_site.wsgi:application
    

    NOTE: Bind to the address in the gunicorn config.

Setup Supervisor

  1. Create a folder in the home directory using the command below.

    LINUX COMMAND: mkdir ~/supervisor_confs
    
  2. Navigate to “/etc/supervisor/supervisord.conf” and add the config details below next to the other include location and then save the file.

     CONFIG: /home/<user name>/supervisor_confs/*.conf
    

    NOTE: Make sure to add a space between the included directories.

  3. Navigate to the project’s “conf” folder, make a copy of one of the conf files for the supervisor, move the file to the newly created “supervisor_confs” directory, and modify as needed.

Setup Nginx

  1. Create a folder in the home directory using the command below.

     LINUX COMMAND: mkdir ~/nginx_confs
    
  2. Navigate to “/etc/nginx/nginx.conf” and add the config details below next to the other include location and then save the file.

     CONFIG: /home/<user name>/nginx_confs/*.conf
    

    NOTE: Make sure to add a space between the included directories.

  3. Navigate to the project’s “conf” folder, make a copy of one of the conf files for nginx, move the file to the newly created “nginx_confs” directory, and modify as needed.

  4. Navigate to “~/projects/<project_name>/bin/<install_file>”, then copy and modify the install file to mock the project directory. Then run the file using the command below.

     LINUX COMMAND: source <install_file>
    
  5. From the same directory as above, copy and modify the “runserver” to mock the project directory. Then run the file using the command below.

     LINUX COMMAND: source <runserver>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment