<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jason Stathopulos</title>
    <description>The latest articles on DEV Community by Jason Stathopulos (@jstats).</description>
    <link>https://dev.to/jstats</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F942211%2Fd472763a-88c5-44b0-9580-ba3709fc9030.jpg</url>
      <title>DEV Community: Jason Stathopulos</title>
      <link>https://dev.to/jstats</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jstats"/>
    <language>en</language>
    <item>
      <title>Part 3 — Adding Git, Passenger and Nginx</title>
      <dc:creator>Jason Stathopulos</dc:creator>
      <pubDate>Thu, 13 Oct 2022 21:53:09 +0000</pubDate>
      <link>https://dev.to/jstats/part-3-adding-git-passenger-and-nginx-29ge</link>
      <guid>https://dev.to/jstats/part-3-adding-git-passenger-and-nginx-29ge</guid>
      <description>&lt;p&gt;Previously, we added in the SSH Keys to secure a connection between your computer and the Droplet, installed rbenv, Ruby and Rails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing PostgreSQL
&lt;/h2&gt;

&lt;p&gt;Rails, by default, uses a sqlite3 which is a single file in your application that is being read and written and which stores your tables, indexes, triggers, and views. This works great for a small number of users accessing your site. However, sqlite3 uses the same disk as your application, reducing the performance of your application. &lt;a href="http://serverfault.com/questions/114524/sqlite-on-a-production-server"&gt;This article&lt;/a&gt; should help to answer why sqlite3 will not work on a production server.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.postgresql.org/"&gt;PostgreSQL&lt;/a&gt;, on the other hand, is a database server that securely stores data. It can also handle a variety of workloads from small applications to large web applications with many concurrent users. This makes it ideal as a production database.&lt;/p&gt;

&lt;p&gt;When you are going to use &lt;em&gt;apt-get,&lt;/em&gt; it is important to look for the latest versions of any packages since they are being updated a daily basis&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once apt-get is updated. Install PostgreSQL and its development libraries:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get install postgresql postgresql-contrib libpq-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Create Database User
&lt;/h2&gt;

&lt;p&gt;You will need to create a user which will be able to access the database for your application. I will be using pguser as the user name. You should change the name to whichever you like&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo -u postgres createuser -s pguser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It is always good to have a password for any user you create and a database user is no different.&lt;/p&gt;

&lt;p&gt;Go into the PostgreSQL console by typing in this command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo -u postgres psql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When you see &lt;em&gt;postgres=#&lt;/em&gt;, it means you are now in the PostgreSQL console. Next enter the command, which will change the password for pguser&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=# \password pguser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You will be prompted to enter in a password, do so and press enter. The pguser now has a password. To quit PostgreSQL console, type this command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=# \q
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Setting Up Deployment with Git
&lt;/h2&gt;

&lt;p&gt;According to &lt;a href="http://git-scm.com/"&gt;git-scm.com&lt;/a&gt;, which manages Git, it is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.&lt;/p&gt;

&lt;p&gt;In our case we will be using it to get files from your development repo into the production server. The main result is you do not want to copy files from development environment to your production since you may not know which files are ready for production and which ones are still being developed. By using Git to push to production a snapshot of the current environment, you will know if the same files can be used on production and thus know if it will work.&lt;/p&gt;

&lt;p&gt;If you are planning on having an application with no development, then you can skip this part and go to Create New Rails Application section.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step One — Creating a Bare Git
&lt;/h3&gt;

&lt;p&gt;First you will need to be in your home directory and create two directories for the Git repo and another where the actual files of the application will go. I will be using appname, as the name of the application. Please change the name of the application to the name you would like to use.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd ~

$ mkdir repo/

$ mkdir appname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next we will go into the repo directory and create a bare initial version of Git which will have only the folders and files needed to run Git but no other configuration since we will be creating them ourselves.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd repo

$ mkdir appname.git

$ cd appname.git

$ git init — bare
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step Two — Setting Up Git Hooks
&lt;/h3&gt;

&lt;p&gt;Git hooks are actions that Git can take when Git does something. According to the &lt;a href="http://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks"&gt;Git documentation&lt;/a&gt; there are three possible server hooks: ‘pre-receive’, ‘post-receive’ and ‘update’.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;pre-receive: Executes as soon as the server receives a ‘push’&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;post-receive: Executes when a ‘push’ is completely finished&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;update: Similar to pre-receive, however, executes once for each branch&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will be using post-receive while in appname.git to change directories to hook. Using your favourite editor, create a file called post-receive&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd hooks

$ nano post-receive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then add in this line so that when you push your code from your local machine onto this server it will automatically take the files from the repo and add them to the application folder&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git — work-tree=/home/[your user name]/appname — git-dir=/home/[your user name]/appname.git checkout -f
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Save and exit the file&lt;/p&gt;

&lt;p&gt;Finally, change the permission of the post-receive file to allow it to be executed&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ chmod +x post-receive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step Three — Setting Up Local Git
&lt;/h3&gt;

&lt;p&gt;Exit out of your ssh by using&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ exit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When you are on your local computer, go to your application and, if you have not done so, enter this command to set up your Git&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now add in a Git remote to your Droplet which will create a remote URL called live by typing in this command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git remote add live ssh://[your Droplet ip address]/home/[your user name]/appname.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you do not have any commits on this Git, type in this command in order to test that the connection to your Droplet works&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git add .

$ git commit -m ‘Init commit’
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This command will add everything that has been changed into Git staging and then commit it to the branch, in this case, master branch. To find out more about Git, click on &lt;a href="https://try.github.io/levels/1/challenges/1"&gt;this link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now any time you would like to update the files on your Droplet, enter in this Git command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git push live master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will send your commits to the Droplet from your master branch and load the files into your application folder. Go to your Droplet and see if the files have been uploaded.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ssh [name of user name]@[your Droplet ip address]

$ cd ~/[your user name]/appname/

$ ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you see the files, you are ready to go to the next part. Go to the &lt;em&gt;Installing figaro Gem&lt;/em&gt; section of Create New Rails Application which will show how to store both your database password and secret keys.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create New Rails Application
&lt;/h2&gt;

&lt;p&gt;I will be creating a Rails application to show how to configure it from start to finish. I will be using appname as the name of the application. Change it to the application you would like to use.&lt;/p&gt;

&lt;p&gt;Go to the folder where you would like the application to be located. I will be using my current user's home directory since it is easy to access and this user should be the only one to access the web site using ssh.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step One — Create Application Folder &amp;amp; Files
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd ~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We will also be using &lt;em&gt;-d postgresql&lt;/em&gt; which will automatically use PostgreSQL instead of Rails’ default database SQLite3.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails new appname -d postgresql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Go into your new application directory&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd appname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step Two — Configuration Database
&lt;/h3&gt;

&lt;p&gt;Using your favourite editor go into the configuration for the database&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nano config/database.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Find ‘production:’ which will be near the bottom of database.yml and change the name of the username to the name of the database user.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;production:

    &amp;lt;&amp;lt;: *default

    database: Appname_production

    username: pguser

    password: &amp;lt;%= ENV[‘APPNAME_DATABASE_PASSWORD’] %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Exit and save config/database.yml&lt;/p&gt;

&lt;h3&gt;
  
  
  Step Three — Generated Secret Keys
&lt;/h3&gt;

&lt;p&gt;Secret keys are used by your application to verify that user sessions and cookies are unique from any other application, so that one application does not log into a user from another application. If anyone finds out your Secret Keys they could login as a user and break your application.&lt;/p&gt;

&lt;p&gt;We will need to run rake secret to generate a random key that will be unique for this application. Also, you will need to run rake in production mode. To do so, add in this command &lt;em&gt;RAILS_ENV=production&lt;/em&gt; in front of any other Rails command. By adding it to any command, you switch from development mode into production mode.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ RAILS_ENV=production rake secret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Copy the key from the console and add it to a notepad in order to retrieve later when we add it to figaro gem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step Four — Install figaro gem
&lt;/h3&gt;

&lt;p&gt;Figaro gem hides sensitive information from being accidentally committed to your version control where bad hackers could use that information to access your application. By doing this, you are protecting the data from being used outside the server.&lt;/p&gt;

&lt;p&gt;Open Gemfile using your favourite editor&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nano Gemfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then add in the gem figaro&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ gem ‘figaro’
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Exit and save Gemfile then run bundle to install figaro&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ bundle install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Open config/application.yml using your favourite editor, here we will be adding our database password and secret key which will be in the environment variables on the server.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;production:

    SECRET_KEY_BASE: [Enter Secret Key]

    APPNAME_DATABASE_PASSWORD: [Enter Database Password]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Exit and save config/application.yml&lt;/p&gt;

&lt;h3&gt;
  
  
  Step Five — Create Production Database
&lt;/h3&gt;

&lt;p&gt;We can now create our database for use in production.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ RAILS_ENV=production rake db:create
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you see any errors which say you cannot connect to the database, double check either config/application.yml or config/database.yml to ensure there are no spelling mistakes for the environment variables. If you are migrating from your development environment you should run those commands&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ RAILS_ENV=production rake db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Also if you have database seeds you could run them as well.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ RAILS_ENV=production rake db:seed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step Six — Testing Our Application
&lt;/h3&gt;

&lt;p&gt;It is now time to test that the application is running correctly. However, first go to your &lt;a href="https://cloud.digitalocean.com/droplets"&gt;Digital Ocean console&lt;/a&gt; and find out the IP address for your server, then type this command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails server — binding=[your Droplet ip address]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will run the default application server for Rails, WEBrick, an application server to run Rails and since we bind it to an IP Address, you can go to your server public id using port 3000 to see your site online. Enter this address in your browser&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://[your Droplet ip address]:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you see the Rails Welcome page or the homepage of your site then we are good to go. Go back to your console and press &lt;em&gt;CRTL+C&lt;/em&gt; to stop WEBrick since now we are going to use Passenger instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Passenger &amp;amp; Nginx
&lt;/h2&gt;

&lt;p&gt;Before we start installing both Passenger and Nginx, a little background on what both of them do. When you are interacting with Web Applications such as Rails or Meteor, there are actually two servers that are being used to run your application, a web server and an application server. A web server gets the request from your user’s computer and sends it to the application server. Then sends back a response to your user’s computer. Application server actually runs your application and returns the response back to the user in the form of a view, JSON or any other data type the user has requested.&lt;/p&gt;

&lt;p&gt;While there is a debate on which ones are the best, we will be using &lt;a href="https://www.phusionpassenger.com/"&gt;Passenger&lt;/a&gt; and &lt;a href="http://nginx.org/en/"&gt;Nginx&lt;/a&gt;, since they are both fast and reliable. You may wish to do your own research and see which one works for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step One — Adding APT Source File
&lt;/h3&gt;

&lt;p&gt;To start up we will be using Advanced Packaging Tool (APT) but first we will need to add a new source where the Passenger package is located on the internet, to &lt;em&gt;apt-get.&lt;/em&gt; To install Passenger run this command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-key adv — keyserver keyserver.ubuntu.com — recv-keys 561F9B9CAC40B2F7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create a new APT source file using your favourite editor&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo nano /etc/apt/sources.list.d/passenger.list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add in this line and save and exit the file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;deb [https://oss-binaries.phusionpassenger.com/apt/passenger](https://oss-binaries.phusionpassenger.com/apt/passenger) trusty main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Finally we will be updating the owner and permission of the new APT source file to be the root user and have that user able to read and write to the file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo chown root: /etc/apt/sources.list.d/passenger.list

$ sudo chmod 600 /etc/apt/sources.list.d/passenger.list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step Two — Install Passenger Using APT
&lt;/h3&gt;

&lt;p&gt;First as always update apt-get&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Install both Nginx and Passenger from &lt;em&gt;apt-get&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get install nginx-extras passenger
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Setting Up Nginx the web server
&lt;/h2&gt;

&lt;p&gt;Open up the configuration for Nginx, using your favourite editor&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo nano /etc/nginx/nginx.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Find the following lines located at the bottom of the file. This is how Nginx knows to use Passenger as the application server and run Ruby from it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;

# passenger_ruby /usr/bin/ruby;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Remove the # in order to uncomment those lines and allow Passenger to use them&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;

passenger_ruby /usr/bin/ruby;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Save the file and exit&lt;/p&gt;

&lt;h2&gt;
  
  
  Finally, Deployment
&lt;/h2&gt;

&lt;p&gt;First, we need to disable the default Nginx configuration in your favourite editor by using this command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/default
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Find these lines they are located at the top of the file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;listen 80 default_server;

listen [::]:80 default_server ipv6only=on;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add # in front of them to comment them out&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# listen 80 default_server;

# listen [::]:80 default_server ipv6only=on;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Save and exit the file&lt;/p&gt;

&lt;p&gt;Next, we will be creating a configuration for your application. Replace appname with the name of your application, again you will need to use your favourite editor&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo nano /etc/nginx/sites-available/appname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add the file server block which will tell Nginx how to run the web server for this application&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {

    listen 80 default_server;

    server_name [www.mydomain.com;](http://www.mydomain.com;)

    passenger_enabled on;

    passenger_app_env production;

    root /home/[your user name]/appname/public;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The server block has different parts as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;listen 80: tells Nginx to listen on port 80, the port where the Internet traffic comes from&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;server_name: the name of the domain the application is on. This is optional and you can use your Droplet IP address to access this application from the browser&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;passenger_enabled on: enables Nginx to use Passenger application server for this application&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;passenger_app_env: Set the type of application to use development or production, which is default. If the application is not running correctly you can switch this to development mode and see a more detailed error message. Please note that for security reasons you should never have this set to development and forget about it, since bad hackers could use the detailed error messages as a way to access your application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;root: Location of where to load the files, this points to the public folder of an application to only load the HTML files since HTML files can access CSS and JavaScript outside of the public folder. Thus a user would not be able to access either the CSS or JavaScript and see how they work. This is a secure way of providing your application from being hacked.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Save and exit the file&lt;/p&gt;

&lt;p&gt;Finally, we will be copying your application Nginx configuration from the available folder to the enabled folder so that Nginx can run it when the user types in the domain name or IP address.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo ln -s /etc/nginx/sites-available/appname /etc/nginx/sites-enabled/appname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Restart Nginx, since Nginx only enables sites when it first loads up&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo nginx -s reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To test your application in your browser, enter in the IP address for this Droplet, or if you have a domain name, use that instead.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://[your Droplet ip address]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you see the Rails Welcome page or the homepage of your site all is good and your application is officially deployed.&lt;/p&gt;

&lt;h2&gt;
  
  
  General Maintenance
&lt;/h2&gt;

&lt;p&gt;Just because you deploy your application does not mean you no longer have to worry about your server. You will need to do regular updates to all of the packages you have installed, as well as any gems that you have installed.&lt;/p&gt;

&lt;p&gt;For the packages, run this command on a regular basis&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get update &amp;amp;&amp;amp; sudo apt-get upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Which will update your APT and upgrade any packages that need to be updated&lt;/p&gt;

&lt;p&gt;Then run this command which is the same as &lt;em&gt;sudo nginx -s reload&lt;/em&gt; to reset your server&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo service nginx restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For any gems in your Rails application you should run bundle install which will go to &lt;a href="http://rubygems.org"&gt;rubygems.org&lt;/a&gt; and find the latest version of the gems, if the Gemfile allows it to update.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Your application should now be live. Take a moment to realize how far you have come. Of course this is only setting up a basic server, there is always room to grow. I hope your installation went smoothly over the course of this tutorial and that you have found it to be helpful. Please leave comments below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;The articles that were used in the creation of this post:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.sqlite.org/about.html"&gt;SQLite&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/PostgreSQL"&gt;PostgreSQL — Wikipedia&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://git-scm.com/"&gt;git-scm.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-ruby-on-rails-application-on-ubuntu-14-04"&gt;How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps"&gt;How To Set Up Automatic Deployment with Git with a VPS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-passenger-and-nginx-on-ubuntu-14-04"&gt;How To Deploy a Rails App with Passenger and Nginx on Ubuntu 14.04&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://askubuntu.com/questions/187071/how-do-i-restart-shutdown-from-a-terminal"&gt;How do I restart /shutdown from a terminal?&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>devops</category>
    </item>
    <item>
      <title>Part 2 — Adding Users, Ruby and Rails</title>
      <dc:creator>Jason Stathopulos</dc:creator>
      <pubDate>Thu, 13 Oct 2022 21:45:18 +0000</pubDate>
      <link>https://dev.to/jstats/how-to-deploy-a-rails-app-with-passenger-and-nginx-on-digital-ocean-part-2-adding-users-ruby-and-rails-15lc</link>
      <guid>https://dev.to/jstats/how-to-deploy-a-rails-app-with-passenger-and-nginx-on-digital-ocean-part-2-adding-users-ruby-and-rails-15lc</guid>
      <description>&lt;p&gt;Previously, we created a Digital Ocean account and an SSH key to have a secure connection.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create Droplet
&lt;/h2&gt;

&lt;p&gt;Next we are going to create a droplet. To do so, click on Create Droplet, which will create a virtual machine on Digital Ocean for you.&lt;/p&gt;

&lt;p&gt;Once you click on create Droplet you will find a page to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Enter the name of the Droplet&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose the size of the Droplet&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select the Region where the actual server will be. The closer to you the faster your updates will apply because the distance is shorter between you and the server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Any available settings for that server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You should choose at the very least, IPv6 to future proof your site and enable backups to provide loss of any data in an emergency&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select the type of image for your server which is the operating system and if you want to use a application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose Ubuntu 14.10 x64, since Postgres 9.4 can only run on this operating system&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, select the SSH key we created in the Initial Setup&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Click on Create Droplet. This will now take a minute or two to create the Droplet for you. Once done, you will be taken to the control panel for that Droplet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Users
&lt;/h2&gt;

&lt;p&gt;Before we can begin to install Ruby, we need to take care of some administration duties. Mainly creating a user who is not the root user because a root user has too much access to more sensitive parts of the operating system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step One — Create a User
&lt;/h3&gt;

&lt;p&gt;Log into your Droplet that you created from the last part and enter this command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ adduser **newuser**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;adduser&lt;/code&gt; command will ask you to enter in a password and confirmation password. It will also ask for additional information. This is optional and you can just skip over it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;adduser&lt;/code&gt; command will now enter in the user to the operating system and create all of the folders for the new user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step Two — Add Grant New User Sudo Privileges
&lt;/h2&gt;

&lt;p&gt;As the root user, enter this command to access the sudoers file which keeps track of which users of the Droplet are using the &lt;code&gt;sudo&lt;/code&gt; command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ visudo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Search for root ALL=(ALL:ALL) ALL in visudo&lt;/p&gt;

&lt;p&gt;Creating a new line under root ALL=(ALL:ALL) ALL and add in this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**newuser** ALL=(ALL:ALL) ALL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will give newuser administrative access for commands when you use sudo in front of them. Use with caution though as this command is very powerful and could make your Droplet unstable if used incorrectly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step Three — Add SSH Keys To New User
&lt;/h2&gt;

&lt;p&gt;Change user to the &lt;strong&gt;newuser&lt;/strong&gt; since we need to access their home directory&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ su newuser

$ password: **[Enter in newuser password]**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then create a hidden folder called .ssh&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir .ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Exit out of ssh by typing exit twice. Once for &lt;strong&gt;newuser&lt;/strong&gt; and another one for the root user.&lt;/p&gt;

&lt;p&gt;Type in this command to copy your SSH public key to the newuser&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat ~/.ssh/id_rsa.pub | ssh **[name of new user name]**@**[your Droplet ip address]** “cat &amp;gt;&amp;gt; ~/.ssh/authorized_keys”
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You will need to enter in the password of the &lt;strong&gt;newuser&lt;/strong&gt; in order to add in the SSH public key into that account. Once done, use the command ssh to go into the Droplet as the new user. You should not have to enter in a password.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing rbenv
&lt;/h2&gt;

&lt;p&gt;Finally, after we have configured the access rights to the Droplet, it is time to install rbnenv which will be used to control the different version of Ruby on your Droplet. This can help if you are going to run multiple sites on this Droplet. While I will be installing rbenv you can just install Ruby directly. If the Droplet is going to only be used for one website, take a look at this &lt;a href="https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-passenger-and-nginx-on-ubuntu-14-04"&gt;article’s step 4 for the instructions&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step One — Update apt-get
&lt;/h3&gt;

&lt;p&gt;Before we can begin, we should explain what is an apt-get. apt is the package manager of Linux which Ubuntu is based on. A package is a list of files that are archived and thus make it easier to transmit over the internet. Keeping that in mind, a Package Manager is able to download those packages and install them as applications onto your system. It also can keep track of them which means they can be updated, uninstalled or you can search other packages you may want to install later.&lt;/p&gt;

&lt;p&gt;Because apt package list is always being updated, it is best to use the command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To update the list and versions of the different packages on your system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step Two — Install the rbenv and Ruby dependencies
&lt;/h2&gt;

&lt;p&gt;This will install any Ruby dependencies that are needed, as well as Git, which is a version control for your code which we will talk about later. Git will allow you to get &lt;code&gt;rbenv repo&lt;/code&gt; from Github, a poplar repository of open source projects using Git.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now that the Ruby dependencies have been installed, enter in these commands to install rbenv and have it run when you first log in.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd

$ git clone git://github.com/sstephenson/rbenv.git .rbenv

$ echo ‘export PATH=”$HOME/.rbenv/bin:$PATH”’ &amp;gt;&amp;gt; ~/.bash_profile

$ echo ‘eval “$(rbenv init -)”’ &amp;gt;&amp;gt; ~/.bash_profile

$ exec $SHELL

$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

$ echo ‘export PATH=”$HOME/.rbenv/plugins/ruby-build/bin:$PATH”’ &amp;gt;&amp;gt; ~/.bash_profile

$ exec $SHELL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will install rbenv to your home directory and set the appropriate environment variables that will be used, allowing rbenv to use the active version of Ruby.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Ruby
&lt;/h2&gt;

&lt;p&gt;Ruby is an open source programming language that is dynamic and relies heavily on Object-Oriented design pattern. Everything in Ruby is an object. Allowing anything to have a basic function that it could use at any time or even override those functions and adding your own custom ones.&lt;/p&gt;

&lt;p&gt;To install Ruby, you will need to use rbenv. As of this writing, the current version of Ruby is 2.2.2. Go to the &lt;a href="https://www.ruby-lang.org/en/downloads/"&gt;Ruby Official site&lt;/a&gt; to know what is the latest version or if you want to use another version of Ruby, then you may replace my version with your own.&lt;/p&gt;

&lt;p&gt;Type in those commands which will install Ruby and make that version the default globally. This may take a while depending on your connection.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rbenv install 2.2.2

$ rbenv global 2.2.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We should verify that the version of Ruby. Type in this command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ruby -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If the version is 2.2.2, then we are good to go. Next, we should stop gems from installing documentation on our Droplet, since this takes up space and I am hoping that you are not developing on the Droplet but using it as your production server.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ echo “gem: — no-document” &amp;gt; ~/.gemrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Gems are similar to packages as mentioned before however, they are meant for to be use in Ruby to add functionality and allow development of Ruby application easier.&lt;/p&gt;

&lt;p&gt;We should also install bundler gem. This is similar to apt which keeps track of gems, in Rails.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ gem install bundler
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Installing Rails
&lt;/h2&gt;

&lt;p&gt;Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pages. It encourages and facilitates the use of web standards such as XML or JSON for data transfer, and HTML, CSS and JavaScript for display and user interfacing, according to &lt;a href="http://en.wikipedia.org/wiki/Ruby_on_Rails"&gt;Wikipedia&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To install Rails, use this command&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ gem install rails
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To verify that the correct versions of Rails has been installed, type in this command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You should see the latest version of Rails&lt;/p&gt;

&lt;h2&gt;
  
  
  Install Javascript Runtime
&lt;/h2&gt;

&lt;p&gt;Because a few Rails features, such as the Asset Pipeline, depend on a Javascript runtime, we will need to install Node.js.&lt;/p&gt;

&lt;p&gt;Add the Node.js PPA to apt-get:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo add-apt-repository ppa:chris-lea/node.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Update apt-get and install the Node.js package:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get update

&lt;p&gt;$ sudo apt-get install nodejs&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion for the Second Part&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Our Droplet is taking shape. We created a Droplet, added in the SSH Keys to secure a connection between your computer and the Droplet, installed rbenv, Ruby and Rails.&lt;/p&gt;

&lt;p&gt;Next time we will be explaining Git and how to bring over your code from Development into the Droplet, installing Postgres, Passenger and Nginx. So click on this &lt;a href="https://dev.to/jstats/part-3-adding-git-passenger-and-nginx-29ge"&gt;link&lt;/a&gt; to go to the next part.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;The articles that were used in the creation of this post:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Ruby_%28programming_language%29"&gt;Ruby (Programming Language) — Wikipedia&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Ruby_on_Rails"&gt;Ruby on Rails — Wikipedia&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-14-04"&gt;Initial Server Setup with Ubuntu 14.04&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-add-and-delete-users-on-an-ubuntu-14-04-vps"&gt;How To Add and Delete Users on an Ubuntu 14.04 VPS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-14-04"&gt;How To Install Ruby on Rails with rbenv on Ubuntu 14.04&lt;/a&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Part 1 — Creating SSH</title>
      <dc:creator>Jason Stathopulos</dc:creator>
      <pubDate>Thu, 13 Oct 2022 21:36:52 +0000</pubDate>
      <link>https://dev.to/jstats/how-to-deploy-a-rails-app-with-passenger-and-nginx-on-digital-ocean-part-1-creating-ssh-3ja7</link>
      <guid>https://dev.to/jstats/how-to-deploy-a-rails-app-with-passenger-and-nginx-on-digital-ocean-part-1-creating-ssh-3ja7</guid>
      <description>&lt;p&gt;Deploying, in general, is an art form since it requires patience and know-how. For beginners this can seem impossible to do but don’t fret, after a few tutorials and some willingness to learn, anyone can deploy a website or application.&lt;/p&gt;

&lt;p&gt;In this tutorial we will be deploying a Rails app with &lt;a href="https://www.phusionpassenger.com/"&gt;Passenger&lt;/a&gt;, as the application server, and &lt;a href="http://nginx.org/en/"&gt;Nginx&lt;/a&gt;, as the web server. Also, the Rails app will use &lt;a href="http://www.postgresql.org/"&gt;Postgres&lt;/a&gt;, so we will be installing that as well.&lt;/p&gt;

&lt;p&gt;We will start from the very beginning. The first step is to sign up to &lt;a href="https://www.digitalocean.com/"&gt;Digital Ocean&lt;/a&gt;, if you have not already.&lt;/p&gt;

&lt;h2&gt;
  
  
  How SSH Works
&lt;/h2&gt;

&lt;p&gt;Before we can begin, we will need to set up an SSH key, since this will allow us to have a secure connection to a Droplet without having to remember or write down a password furthermore, passwords are more prone to brute forcing attacks.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An SSH key pairs are two cryptographically secure keys that can be used to authenticate a client to a server. Each key pair consists of a public key and a private key.&lt;/p&gt;

&lt;p&gt;The private key is retained by the client and should be kept absolutely secret. Any compromise of the private key will allow attackers to log into servers that are configured with the associated public key without additional authentication. As an additional precaution, the key can be encrypted on disk with a passphrase.&lt;/p&gt;

&lt;p&gt;The associated public key can be shared freely. The public key can be used to encrypt messages that only the private key can decrypt. This property is employed as a way of authenticating using the key pair.&lt;/p&gt;

&lt;p&gt;The public key is uploaded to a remote server that you want to be able to log into with SSH. The key is added to a special file within the user account you will be logging into called ~/.ssh/authorized_keys.&lt;/p&gt;

&lt;p&gt;When a client attempts to authenticate using SSH keys, the server can test the client on whether they are in possession of the private key. If the client can prove that it owns the private key, a shell session is spawned or the requested command is executed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Ellingwood, Justin. “&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server"&gt;How To Configure SSH Key-Based Authentication on a Linux Server&lt;/a&gt;” Digital Ocean. DigitalOcean Inc, 20 Oct. 2014. Web. 26 Apr. 2015.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pSHbSmGk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AAOMfqMymZR16N7zOiId5FQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pSHbSmGk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AAOMfqMymZR16N7zOiId5FQ.png" alt="An overview of the flow to Authentication of SSH key" width="745" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating SSH
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step One — Creating the RSA Pair
&lt;/h3&gt;

&lt;p&gt;First we will need to create a RSA Pair on your computer by going into the command line and typing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;  $ ssh-keygen -t rsa -C “**your_email@example.com**”
&amp;gt;  # This will create a new SSH key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step Two — Generate a new SSH key
&lt;/h3&gt;

&lt;p&gt;ssh-keygen will ask you where the RSA Pair should go. It is recommended to keep the default settings as they are, so press enter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;  $ Enter file in which to save the key (/Users/**you**/.ssh/id_rsa): **[Press enter]**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ssh-keygen will then ask you for a passphrase. It is recommended to create a strong passphrase in order to prevent your SSH key being hacked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;  $ Enter passphrase (empty for no passphrase): **[Type a passphrase]**
&amp;gt;  $ Enter same passphrase again: **[Type passphrase again]**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you enter in your passphrase ssh-keygen will generate the key and display the results on the screen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;  Your identification has been saved in /Users/**you**/.ssh/id_rsa.
&amp;gt;  Your public key has been saved in /Users/**you**/.ssh/id_rsa.pub.
&amp;gt;  The key fingerprint is: 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db 
**your_email@example.com**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step Three — Add your SSH key to the ssh-agent
&lt;/h3&gt;

&lt;p&gt;In order to store your private SSH key to verify that you are the owner of the public key, you will need to add it to the ssh-agent.&lt;/p&gt;

&lt;p&gt;First ensure ssh-agent is enabled.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ eval “$(ssh-agent -s)”
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add your SSH key to the ssh-agent&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ssh-add ~/.ssh/id_rss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step Four — Add your SSH key to your Digital Ocean account
&lt;/h3&gt;

&lt;p&gt;First you will need to copy the SSH key to your clipboard. You should never copy a SSH key by highlighting it, since any whitespaces or newlines will throw off the matching between your SSH and the SSH on Digital Ocean.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pbcopy &amp;lt; ~/.ssh/id_rsa.pub

# Copies the contents of the id_rsa.pub file to your clipboard
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you have not already logged into Digital Ocean, log into it. Click on the icon of the person at the top right of the page. This will give you options, choose Your Settings then Security tab.&lt;/p&gt;

&lt;p&gt;If you scroll down a bit you will see a form to enter an SSH key.&lt;/p&gt;

&lt;p&gt;Enter in the name of your SSH key. It should be the name of the computer your SSH is coming from&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NzjO-xh4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AEr41uJsxjbKZjER-jEMqlQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NzjO-xh4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AEr41uJsxjbKZjER-jEMqlQ.png" alt="Enter in your SSH key" width="822" height="463"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Paste in the SSH key&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1_glkv_G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ACfPf2Fd2S8ZAMybGAe_3Qw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1_glkv_G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ACfPf2Fd2S8ZAMybGAe_3Qw.png" alt="Pasted SSH key" width="836" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on Create SSH Key to save the SSH key to your account.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Applying SSH Keys To Existing Droplets&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you have already created some Droplets that do not have SSH keys you can still apply the ones you created. If you have no existing Droplets, then skip this section.&lt;/p&gt;

&lt;p&gt;Enter this command for each of the existing Droplets you wish to use an SSH key on&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat ~/.ssh/id_rsa.pub | ssh root@**[your Droplet ip address]** “cat &amp;gt;&amp;gt; ~/.ssh/authorized_keys”

# This will connect to your Droplet and create a file called authorized_keys with your SSH key, in the Droplet.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Troubleshooting&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If you must enter in a password then there is something wrong with the public key, either on your computer or on Digital Ocean.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you destroy a Droplet and then create another Droplet on the same IP address,you will get a warning message. Use this command to remove it from your known host.&lt;/p&gt;

&lt;p&gt;$ ssh-keygen -R &lt;strong&gt;[your Droplet ip address]&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;then try connecting to your server again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion for the First Part
&lt;/h2&gt;

&lt;p&gt;This is just the start. We have the basic setup in order for you to create many different Droplets without having to write down or memorize different passwords for each of your Droplets allowing you to focus on maintaining your Droplets.&lt;/p&gt;

&lt;p&gt;Once you finish creating the SSH key and updating any existing Droplets, it is time to create new Droplets. So click on this &lt;a href="https://dev.to/jstats/how-to-deploy-a-rails-app-with-passenger-and-nginx-on-digital-ocean-part-2-adding-users-ruby-and-rails-15lc"&gt;link&lt;/a&gt; to go to the next part&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;The articles that were used in the creation of this post:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://help.github.com/articles/generating-ssh-keys/"&gt;GitHub Help: Generating SSH Keys&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets"&gt;How To Use SSH Keys with DigitalOcean Droplets&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server"&gt;How To Configure SSH Key-Based Authentication on a Linux Server&lt;/a&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Basic Networking: HTTP Request &amp; Response</title>
      <dc:creator>Jason Stathopulos</dc:creator>
      <pubDate>Thu, 13 Oct 2022 21:19:10 +0000</pubDate>
      <link>https://dev.to/jstats/basic-networking-http-request-response-16mj</link>
      <guid>https://dev.to/jstats/basic-networking-http-request-response-16mj</guid>
      <description>&lt;p&gt;The way HTTP which stands for HyperText Transfer Protocol is the basis for data communication on the internet, and the way it works is that a request from the client or browser asks the server for a file. The server gets the file if it is on the server. Then sends it back, to the client as a response, to render. If any files need to provide, then the browser will need to make an additional request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Request
&lt;/h2&gt;

&lt;p&gt;HTTP Request Uses methods sometimes called the verb such as GET and POST as well as the path/route the request is going to on the server, which is sometimes that is called the noun. The request uses headers for additional information such as content type.&lt;/p&gt;

&lt;p&gt;The following is a sample HTTP request&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET(verb) /index.html(noun) HTTP/1.1
Host: localhost:1234
Headers: (Content-Type, etc.)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One more thing, the web is inherently stateless meaning that the request is always new, when sending, to the server, and there is no connection between two or more requests which is the IP Address and Port to access the server. However, you could use cookies to keep track of the state for the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Response
&lt;/h2&gt;

&lt;p&gt;After processing the request the server comes back with a response, status codes are sent to tell the client the results of what happened to the HTTP request on the server.&lt;/p&gt;

&lt;p&gt;HTTP/version usually is 1.1 with a status number (200, 404) If it is okay or not found.&lt;br&gt;
Content-type: type of content for HTML it is text/html&lt;/p&gt;

&lt;p&gt;The content for example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP/1.1 200 OK or 404 NOT FOUND
Content-type: text/html
&amp;lt;!DOCTYPE html&amp;gt;
…
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Request Methods
&lt;/h2&gt;

&lt;p&gt;A request method is important because it tells us what should we do with the request’s data. A server is not only able to retrieve data but also in charge of managing any changes to it as well. A request method combines with a URL.&lt;/p&gt;

&lt;p&gt;Here are commonly used request methods, which are known as the ReSTful(Representational State Transfer) methods.&lt;/p&gt;

&lt;p&gt;GET: fetch or read a resource. The URL contains all the necessary information the server needs to locate and return the resource without changing the server content. The GET request is simple. Every time you enter a URL in your browser or click on a link a GET request is made.&lt;/p&gt;

&lt;p&gt;POST: create a new resource. POST requests usually carry a request body that specifies the data for the new resource. For the most part, when submitting forums after the user fills it in and hits submit, the browser will make a POST request to the server that includes the corresponding data in the request body.&lt;/p&gt;

&lt;p&gt;PUT: replaces a resource on the server.&lt;br&gt;
PATCH: is another type of update which updates an existing resource on the server two kinds of updates carry a request body with the updated data.&lt;/p&gt;

&lt;p&gt;DELETE: delete a resource.&lt;/p&gt;

&lt;p&gt;The other request types can also use with forums to send their request types to the server as well.&lt;/p&gt;

&lt;p&gt;These also correspond to what we refer to as the CRUD actions. Create (POST), Read (GET), Update (PUT), and Delete (DELETE).&lt;/p&gt;

&lt;h2&gt;
  
  
  Response Codes
&lt;/h2&gt;

&lt;p&gt;Response codes are the codes that come from the server to inform the client or browser if it was able to complete the request or not by using codes to describe what has happened on the server.&lt;/p&gt;

&lt;p&gt;Here are the response codes which are Response codes are that are group by numbers, some of them you may have heard of before, like “200 OK” and “404 Not Found”.&lt;/p&gt;

&lt;p&gt;1xx: Informational: These types of response codes not use. They were add in HTTP 1.1, rarely used.&lt;/p&gt;

&lt;p&gt;2xx: Success: This tells the client that the request successfully process. The most common code is 200 OK. For a GET request, the server sends the resource in the message body.&lt;/p&gt;

&lt;p&gt;3xx: Redirection: This requires the client to take additional action. The most common use-case is to jump to a different URL to fetch the resource.&lt;/p&gt;

&lt;p&gt;4xx: Client Error: These codes use when the server thinks that the client is at fault, either by requesting an invalid resource or making a bad request. The most popular coded, in this class, is 404 Not Found, which everyone will identify with, 404 indicates that the resource is invalid and does not exist on the server.&lt;/p&gt;

&lt;p&gt;5xx: Server Error: This class of codes are used to indicate a server failure while processing the request. The most commonly used error code is 500 Internal Server Error.&lt;/p&gt;

&lt;p&gt;A full list of all response codes is available inside the &lt;a href="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes"&gt;HTTP official spec&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Advanced Git</title>
      <dc:creator>Jason Stathopulos</dc:creator>
      <pubDate>Thu, 13 Oct 2022 16:34:04 +0000</pubDate>
      <link>https://dev.to/jstats/advanced-git-i3k</link>
      <guid>https://dev.to/jstats/advanced-git-i3k</guid>
      <description>&lt;p&gt;Besides some of the basic commands in git, there is a whole host of other commands. And strategies to master to get the most out of your project and reduce the amount of time maintaining codebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Proper Commit Messages
&lt;/h2&gt;

&lt;p&gt;The summary or first line of the commit can be up to 50 characters&lt;/p&gt;

&lt;p&gt;which forces developers to think about what will be in this commit and for quick reading when looking at the summaries (such as when you use &lt;code&gt;git shortlog&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The details or third line going forward, of the commit, can be 72 characters per line.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Because git adds two blank spaces on either side of the details, centring it in the terminal when you use &lt;code&gt;git log&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The commit message should explain what is going into this commit and why you are making the change. If you cannot explain it, then maybe it is time to pause and reflect on this commit.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;There are exceptions to the rules, though. If the change is noticeable, then, of course, you can do it on one line, as long it is less than 50 characters. If you cannot do so, then you will need to add in more detail in the body.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here is your chance to also explain if this commit will have any side effects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How it will fix the issues&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Try adding links to commits if you found the answer on stack overflow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This help will help developers, and reviews of your pull request in knowing what you have done and what reason for this commit without having to look into the code and only to guess the reason.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DO NOT bunch changes into one commit if you need to use “and” then that should be an induction you should split the commit up and put them into their commits.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use present tense person will make it easier to read the commit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some Git Commands I Use
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Add — patch
&lt;/h3&gt;

&lt;p&gt;Before we begin with rebasing, let’s talk a little about &lt;code&gt;add — patch&lt;/code&gt;, which allows you to add part of the code into staging while leaving the rest to be commit later.&lt;/p&gt;

&lt;p&gt;Suppose you make lots of changes to one file but forgot to commit instead of committing the whole thing and explain what is going on.&lt;/p&gt;

&lt;p&gt;To start by typing, &lt;code&gt;git add — patch LocationOfFile&lt;/code&gt; this will start up the patch screen and give you options to split add some of the changes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Stage this hunk [y,n,q,a,d,/,e,?]?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By typing in &lt;code&gt;?&lt;/code&gt; for help, the following will display&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    y — stage this hunk

    n — do not stage this hunk

    q — quit; do not stage this hunk or any of the remaining ones

    a — stage this hunk and all later hunks in the file

    d — do not stage this hunk or any of the later hunks in the file

    g — select a hunk to go to

    / — search for a hunk matching the given regex

    j — leave this hunk undecided, see next undecided hunk

    J — leave this hunk undecided, see next hunk

    k — leave this hunk undecided, see previous undecided hunk

    K — leave this hunk undecided, see previous hunk

    s — split the current hunk into smaller hunks

    e — manually edit the current hunk

    ? — print help
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some of those will only appear at specific types of hunks. Here are some of the most used sections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;y / Yes will stage that hunk&lt;/li&gt;
&lt;li&gt;n / No will not stage that hunk&lt;/li&gt;
&lt;li&gt;q / quit stops &lt;code&gt;add — patch&lt;/code&gt; any remaining hunks will not be added&lt;/li&gt;
&lt;li&gt;s / smaller will shrink the hunk into smaller pieces. However, this will only use in big hunks&lt;/li&gt;
&lt;li&gt;e / edit remove the code that will not be stage from your text editor for that hunk&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Rebase
&lt;/h3&gt;

&lt;p&gt;Reapplies commits on top of the base. For example, when you are on a branch and type in &lt;code&gt;git rebase develop&lt;/code&gt;, it will take the first commit, on this branch, and change the SHA to the SHA of the last commit, HEAD, on the develop.&lt;/p&gt;

&lt;p&gt;Which updates the SHA for each of the commits, in that branch, if you previously push your changes up to the remote repo, then you will have to to &lt;code&gt;git push -f origin branchName&lt;/code&gt;. A word of warning, if the branch is shared, you will need to tell everyone that is using it to pull down the branch to avoid duplicating code or better DO NOT rebase on a shared. Furthermore, because the SHA changes, you must NEVER rebase on develop or master branch as everyone on the team will have a separate version of the project and cause unforeseen issues with commits later on.&lt;/p&gt;

&lt;p&gt;Any example of rebasing is this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Before Rebase&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    A — -B — -C topic

    /

    D — -E — -F — -G develop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;After &lt;code&gt;git rebase develop&lt;/code&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    A’ — B’ — C’ topic

    /

    D — -E — -F — -G develop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now why you want to do this, well, because this will keep your git history clean without having multiple merges commits. Those merge branches which serve no purpose other than showing when the developer merges their branch with develop or master branch by having a clean log you to view all of the commits a glance.&lt;/p&gt;

&lt;p&gt;So if you look at this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ec1c2a9 Topic #6

    033c6bd Merge branch ‘develop’ into topic

    82a5ad2 Topic #5

    82a5ad2 Topic #4

    9953bb1 Topic #3

    a12s7zk Merge branch ‘develop’ into topic

    161e1aa Topic #2

    9sdb12r Merge branch ‘develop’ into topic

    a397dbe Topic #1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ec1c2a9 Topic #6

    82a5ad2 Topic #5

    82a5ad2 Topic #4

    9953bb1 Topic #3

    161e1aa Topic #2

    a397dbe Topic #1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pull — rebase — preserve-merges
&lt;/h3&gt;

&lt;p&gt;Now, if we look at git pull, by default it does a merge instead of a rebase, this could result in the git history containing, merge commits, which undermine the result we want with rebase. By changing the default to ‘pull rebase’, you will not need to remember to do this when you do a pull.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    git config — global pull.rebase preserve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, remember when I said never rebase develop or master well. There is an exception to the rule if you add in — preserve-merges this or rebase = preserve in the Git config than pull — rebase will not try to remove the merge commits that might be on the develop or master branch. Thus leaving existing commits alone and you will not have to force push to the remote repo since all it will do is pull down the commits and move the commits in front of whichever branch you are pulling down too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rebase (-i) — interactive
&lt;/h3&gt;

&lt;p&gt;Rebase interactive or Rebase -i, for now on, is the most potent way of multiple git history, why would you want to do such a thing well. Let us say you have a working branch that you may make commits that are not in order or do a bunch of commits that are all part of the same items. Or you decided to hack away at a problem until you found a solution leaving behind a bunch of commits that no longer apply. Suppose you submit a pull request like this. It will take a well to figure out what the actual changes. Also, you end up with a measly history where it becomes impossible to find what difference and a reason behind it.&lt;/p&gt;

&lt;p&gt;So along comes &lt;code&gt;rebase -i&lt;/code&gt; where you can squash, fixup, reword, drop, editing and reorder commits — turning the commits into groups of logical progression. Before I start with the main sections, there is one other thing you can do in &lt;code&gt;rebase -i&lt;/code&gt; called exec, where you can execute SHELL commands during your rebasing. Most of the time, you will not need to use it, but it is there if you need it.&lt;/p&gt;

&lt;p&gt;To start you will need to type &lt;code&gt;git rebase -i&lt;/code&gt; then choose either name of the branch to base off of usually develop or master, but it can be any local branch, or you can use a commit’s SHA which will be after the commit you want to use edit, for example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    git rebase -i a397dbe

    82a5ad2 Topic #4

    9953bb1 Topic #3

    161e1aa Topic #2

    a397dbe Topic #1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It goes from the latest, the top, to oldest, the bottom, so If you want Topic #2 to start your rebase, then you will get the SHA of Topic #1 as that is base commit to starting.&lt;/p&gt;

&lt;p&gt;Once you do this, an editor that you configure or vi by default will open up, and give you the options, by default pick is select meaning that you have chosen that commit to stay.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    pick 835f50c Topic #2

    pick 0d5bf2f Topic #3

    pick 90e56ea Topic #4

    pick 25e1b8c Topic #5

    pick cec1509 Topic #6

    pick f7e230e Topic #7

    pick 2de656d Topic #8

    pick 31bc4a1 Topic #9

    pick 7565ef6 Topic #10

    pick c6d5e23 Topic #11

    # Rebase 713274d..c6d5e23 onto 713274d (10 command(s))

    #

    # Commands:

    # p, pick = use commit

    # r, reword = use commit, but edit the commit message

    # e, edit = use commit, but stop for amending

    # s, squash = use commit, but meld into previous commit

    # f, fixup = like “squash”, but discard this commit’s log message

    # x, exec = run command (the rest of the line) using shell

    # d, drop = remove commit

    #

    # These lines can be re-ordered; they are executed from top to bottom.

    #

    # If you remove a line here THAT COMMIT WILL BE LOST.

    #

    # However, if you remove everything, the rebase will be aborted.

    #

    # Note that empty commits are commented out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Squash&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Squash is the ability to merge two commits while preserving the commit messages, which allows you to view which commit messages and reword them into a new combine commit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    pick 90e56ea Topic #4

    squash 25e1b8c Topic #5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, Topic #5 will be merged into Topic #4 then open up your text editor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    # This is a combination of 2 commits.

    # The first commit’s message is:

    Topic #4

    # This is the 2nd commit message:

    Topic #5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you can decide what the new commit message will be or leave as is, and the combined messages of Topic #4 and 5 will use.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fixup&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Similar to squash but instead of the entering in a commit message. The message is from the commit being merge into, will be used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    pick 25e1b8c Topic #5

    fixup cec1509 Topic #6

    fixup f7e230e Topic #7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, Topic #6 and 7 will merge into Topic #5 and use that message.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Reword&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Allows you to fix misworded or misspelled commits, this gives you a second chance to set a commit wording to make it more clear.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drop&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Will remove commits from git history by either stating it on the line or by deleting the line. A huge word of warning if you remove a commit, any subsequential changes. To the same line of code. That was made by later commits will also be affected. Because it has never happened, in the git history, thus any change that invades that will also change and possibly be remove the code from history going forward.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Editing&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It takes &lt;code&gt;commit — amend&lt;/code&gt; to the extreme where you can amend commits in the middle of two commits instead of at the end. It allows you to split commits up or remove a piece of code that was mistakenly left behind.&lt;/p&gt;

&lt;p&gt;To start editing, choose a line in the &lt;code&gt;rebase -i&lt;/code&gt; option, save and exit the options. Git will start to rebase until it reaches that line where it will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Stopped at 0d5bf2f05e90219eb11e2e5e91d8f6ad0c4518af… Topic #3

    You can amend the commit now, with

    git commit — amend

    Once you are satisfied with your changes, run

    git rebase — continue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you can amend the commit or make new commits then continue with the rebase.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Reordering&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It allows you to group commits to have a logical grouping. So suppose you create a page and commit it. Then you make some other commits, then, later on, you added more to that page and commit them. However, those changes you want to keep then separate, form the create a commit. So in &lt;code&gt;rebase -i&lt;/code&gt; you can cut the line with the changes and paste it under the create commit. That allows anyone looking at pull requests to see the logical progression of the page. But be warned, misusing this could result in the illogical placement of code, such as a function called, while the function itself, not created yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reflog
&lt;/h3&gt;

&lt;p&gt;Now you must be wondering what if you screw up and now your branch is unusable, or your code is lost. No worries there is Reflog, which will list most of the git commands you have done for a given time, default 90 days. It makes it possible to use git rebase without worry.&lt;/p&gt;

&lt;p&gt;It gives you a chance to reverse any git commands that have gone wrong, for instance, if a rebase has broken your application.&lt;/p&gt;

&lt;p&gt;You can do this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;git reflog&lt;/code&gt; to list all of the git commands for the rebase
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    38b323f HEAD@{0}: rebase -i (finish): returning to refs/heads/feature/add_git_reflog

    38b323f HEAD@{1}: rebase -i (pick): Clarify inc/dec operators

    4fff859 HEAD@{2}: rebase -i (pick): Update java.html.markdown

    34ed963 HEAD@{3}: rebase -i (pick): [yaml/en] Add more resources (#1666)

    ed8ddf2 HEAD@{4}: rebase -i (pick): start spanish translation (#1748)

    2e6c386 HEAD@{5}: rebase -i (start): checkout 02fb96d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Select where to reset to, in our case its &lt;code&gt;2e6c386&lt;/code&gt;, or &lt;code&gt;HEAD@{5}&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git reset — hard HEAD@{5}&lt;/code&gt; this will reset your repo to that head&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can start the rebase again or leave it alone.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Merge Conflicts
&lt;/h2&gt;

&lt;p&gt;Of course, there will be times where merge conflicts will happen. First, any tools you use for merge conflicts will still work with rebase. Keep in mind, of how time works. If you make changes to a commit, that will also have changes later on. You should not make those changes unless they commit states otherwise. Or you will need to apply those changes every time the file has changed in a commit.&lt;/p&gt;

&lt;p&gt;There are also three options you can do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rebase — continue&lt;/code&gt;, which will continue on the rebase. However, if there is a merge conflict, and it’s not resolved, you will not be able to continue until the conflict is gone.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rebase — skip&lt;/code&gt;, which will skip the merge conflict; Besides, doing so may cause further issues with other commits. It is best to fix those conflicts now.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rebase — abort&lt;/code&gt; which will stop the rebase and undo any changes done by this rebase. That allows you to make smaller rebase changes or rethink what to change.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Refer:
&lt;/h2&gt;

&lt;p&gt;From &lt;a href="https://git-scm.com/book/ch5-2.html"&gt;https://git-scm.com/book/ch5-2.html&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Short (50 chars or less) summary of changes&lt;/li&gt;
&lt;li&gt;More detailed explanatory text, if necessary. Wrap it to&lt;/li&gt;
&lt;li&gt;about 72 characters or so. In some contexts, the first&lt;/li&gt;
&lt;li&gt;line is treated as the subject of an email and the rest of&lt;/li&gt;
&lt;li&gt;the text as the body. The blank line separating the&lt;/li&gt;
&lt;li&gt;summary from the body is critical (unless you omit the body&lt;/li&gt;
&lt;li&gt;entirely); tools like rebase can get confused if you run&lt;/li&gt;
&lt;li&gt;the two together.&lt;/li&gt;
&lt;li&gt;Further paragraphs come after blank lines.

&lt;ul&gt;
&lt;li&gt;Bullet points are okay, too&lt;/li&gt;
&lt;li&gt;Typically a hyphen or asterisk is used for the bullet,&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;preceded by a single space, with blank lines in&lt;/li&gt;
&lt;li&gt;between, but conventions vary here&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://medium.com/@preslavrachev/what-s-with-the-50-72-rule-8a906f61f09c#.d3huuguud"&gt;https://medium.com/@preslavrachev/what-s-with-the-50-72-rule-8a906f61f09c#.d3huuguud&lt;/a&gt;&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
    </item>
    <item>
      <title>A Complete Beginner’s Guide to Big O Notation</title>
      <dc:creator>Jason Stathopulos</dc:creator>
      <pubDate>Thu, 13 Oct 2022 01:48:31 +0000</pubDate>
      <link>https://dev.to/jstats/a-complete-beginners-guide-to-big-o-notation-56lh</link>
      <guid>https://dev.to/jstats/a-complete-beginners-guide-to-big-o-notation-56lh</guid>
      <description>&lt;p&gt;Since in programing there can be thousands of ways to solve a problem, how do you tell which one is the 'best' or let say that there is a function that is implanted in two different ways one with loops the other way by recursion which one of those ways should be the use in the function? That is where Big O comes in it a system of find out which algorithm would take longer to run and understand. The algorithm that takes less amount of time and less complex to understand is going to win.&lt;/p&gt;

&lt;p&gt;Big O uses numbers to tell which algorithm is excellent and which ones need to be though again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Big O?
&lt;/h2&gt;

&lt;p&gt;The reason for even using Big O is not only for job interviews but also when there are bigger datasets as performance matters at that point. Since there are a lot of data, the algorithms that take an hour to sort or search a large dataset is not as productive as the algorithm that takes seconds to do the same task.&lt;/p&gt;

&lt;p&gt;Big O gives a precise vocabulary to talk about how our algorithm performs; furthermore. It could help in discussing trade-offs between different approaches and have a fruitful discussion among developers and engineers on which algorithm is going to perform better.&lt;/p&gt;

&lt;p&gt;Big O can also help identify which parts of the algorithm are inefficient and can help us find pain points in our applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does 'better' or 'best' mean?
&lt;/h2&gt;

&lt;p&gt;Say we have a function that sums up 'n':&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addUpTo(n) {
  let total = 0
  for (let index = 1; index &amp;lt;= n; index++) {
    total += index
  }
  return total
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addUpTo(n) {
  return (n * (n + 1)) / 2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which of those would consider better is it because one is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster?&lt;/li&gt;
&lt;li&gt;Less memory-intensive?&lt;/li&gt;
&lt;li&gt;More readable?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of those are valid concerns, how would we know which those are better in general?&lt;/p&gt;

&lt;p&gt;The ideal algorithm would have all three of those concerns; however, in most cases, speed is considered better than a less memory-intensive algorithm. Sadly, readable is usually the one that gets overlooked the most.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time Complexity
&lt;/h2&gt;

&lt;p&gt;So let's examine the most considerable concession speed or commonly refer to as Time Complexity is how we analyze the runtime of an algorithm as the size of the inputs increases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Timing Operation
&lt;/h2&gt;

&lt;p&gt;How does one examine speed one way is to use timer functions, which measured how long the service has executed. By measuring the performance before the function runs, then measures the returns after, then subtract the difference to show how fast the function has completed.&lt;/p&gt;

&lt;p&gt;As shown below for our &lt;code&gt;addUpTo&lt;/code&gt; example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addUpTo(n) {
  let total = 0
  for (let index = 1; index &amp;lt;= n; index++) {
    total += index
  }
  return total
}

let t1 = performance.now()

console.log(addUpTo(1000000))

let t2 = performance.now()

// Will be slower then the single line version of `addUpTo`
console.log(`Time Elapsed: ${(t2 - t1) / 1000} seconds.`)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addUpTo(n) {
  return (n * (n + 1)) / 2
}

let t1 = performance.now()

console.log(addUpTo(1000000))

let t2 = performance.now()

// Will be faster then the for loop version of `addUpTo`

console.log(`Time Elapsed: ${(t2 - t1) / 1000} seconds.`)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Manually doing the timing is not as practical. In the long term, as there's an overhead of setting it up, then is how would you communicated the results in percentages or seconds. &lt;/p&gt;

&lt;p&gt;Furthermore, there is an issue with time, in general. As different machines can record different times since each machine can set up differently so which results can be completely different. Even the same device can have different results as well. For fast algorithms, speed measurements may not be precise enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple Operations
&lt;/h2&gt;

&lt;p&gt;Another way besides counting time is to count the number of simple operations the computer has to perform to remove the issue of different computer setups since simple operations are coincident in a function. So what counts as simple operations. Those can be math operations such as the plus (+), minus (-), multiplication (*), or division (/) signs. Here is a link to all of the simple operations. However, it would be best if you did not count every operation should think where the most impact on memory is happening in the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addUpTo(n) {
  // Here there is only 3 operations (*, + and /)
  return (n * (n + 1)) / 2
}

function addUpTo(n) {
  let total = 0
  for (let index = 1; index &amp;lt;= n; index++) {
    // Here + is n additions since n is controlling the number of times the loop is going around i.e. n = 20 then it is 20 
operations Furthermore, = is a assignment operation so it can be count as well so with the example above the number of operations is 40
    total += index
  }
  return total
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Counting simple operations can be hard, so it looks like the number of simple operations is going to be more in functional one version vs another functional version. It can be safe to say that the one with the least amount of operations could be faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Proper Intro to Big O
&lt;/h2&gt;

&lt;p&gt;Here is the overview again, Big O Notation is a way to formalize fuzzy counting, it allows us to talk formally about how the runtime of an algorithm grows as the inputs grow. Also, we do not care about the details only what is the trends as the inputs grow. Big O is always the most a function can handle before it starts to break down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;f(n) = meanings a function with an input of n&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We say that an algorithm is O(f(n)) if the number of simple operations the computer has to do is eventually less than a constant time f(n), as n increases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;f(n) could be linear (f(n) = n) Meaning that the runtime scales as well with so represents with an n&lt;/li&gt;
&lt;li&gt;f(n) could be quadratic (f(n) = n²) Meaning that the runtime could be squared so represents with an n²&lt;/li&gt;
&lt;li&gt;f(n) could be constant (f(n) = 1) Meaning there is no impact to the runtime, so it is in coincident time so represents with a 1&lt;/li&gt;
&lt;li&gt;f(n) could be something entirely different!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a chart showing the growth of operations and their impact on the algorithm:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fey9n6sunln64ytfb4koi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fey9n6sunln64ytfb4koi.gif" alt="Big O Notation Chart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An example of what the definition is trying to say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Always 3 operations or O(1) since the number of operations does not grow
function addUpTo(n) {
  return (n * (n + 1)) / 2
}
// Number of operations is (eventually) bounded by a multple of n (say, 10n) or O(n) since the operations is bound by n
function addUpTo(n) {
  let total = 0
  for (let index = 1; index &amp;lt;= n; index++) {
    total += index
  }
  return total
}
function printAllPairs(n) {
  // The Big O For this function is O(n²) since the operations is bound by n twice
  for (let index = 1; index &amp;lt;= n; index++) {
    for (let innerIndex = 1; innerIndex &amp;lt;= n; innerIndex++) {
      console.log(index, innerIndex)
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Simplifying Big O Expressions
&lt;/h2&gt;

&lt;p&gt;When determining the time complexity of an algorithm, there is some critical rule of thumbs for significant O expressions. Those rules are consequences of the definition of Big O notation. So constants do not matter, so O(2n), i.e. 2 For loops, have two O(n), so the function is O(n) since the function be slow no matter what. The same goes for O(500) since 500 means the number of operations in the function since the number of operations does not matter if the operations are not n so we use O(1) to show that the runtime is coincident.&lt;/p&gt;

&lt;p&gt;Another example is O(13n²) can be O(n²) since 13 operations do not matter all. O(n²) is the weakest part of the function.&lt;/p&gt;

&lt;p&gt;To further simplify the point, you should look at the trends in and see which expression is the one that is the weakest of them all. You can also use math when comparing expressions. So another example is O(n² + 5n + 8) would be O(n²) since if n is 10, then n² would be 100, 5*n would be 50 and 8 would be 8. So since n² has more operations than the other two, it is the weakest part of the function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules of Thumb
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Analyzing complexity with Big O can get complicated, but there are rules of thumb that could help; however, those rules won't ALWAYS work. However, they are an essential starting point.&lt;/li&gt;
&lt;li&gt;Arithmetic operations are constant as most computers can do those operations roughly at the time for 2 + 2 operations would be approximately the same time on most computers.&lt;/li&gt;
&lt;li&gt;Variable assignment is also constant, so attaching a value to a variable in-memory should be roughly the same time for all machines.&lt;/li&gt;
&lt;li&gt;Accessing elements in an array (by index) or object (by key) is constant.&lt;/li&gt;
&lt;li&gt;In a loop, the complexity is the length of the loop times the complexity of whatever happens inside of the loop; it depends on what is the loop. If more loops or other operations are happening, that could lengthen the time when the loop completes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Auxiliary Space Complexity (Less memory-intensive)
&lt;/h2&gt;

&lt;p&gt;How much additional memory do we need to allocate, to run the code in our algorithm? That is the question space complexity tries to answer. The less memory the algorithm takes up, the smaller your program is used to run it on the user computer. You are thus saving your customers money with not having to upgrade their computer to run your application.&lt;/p&gt;

&lt;p&gt;However, first Space Complexity is both the space the inputs take up and the variables inside the algorithm. Nevertheless, we are going to look at the algorithms that do not include the space taken up by the inputs called Auxiliary Space Complexity. We are going to focus on what happens inside the algorithm, not the space of the input takes up. That said, from here on, I'm going to be referred to it as Space Complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules of Thumb
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Those rules are for using Space Complexity.&lt;/li&gt;
&lt;li&gt;Most primitives (booleans, numbers, null) are constant space as they take to update the same amount of space.&lt;/li&gt;
&lt;li&gt;Strings require O(n) space (where n is the string length) since strings make up is an array of characters, so the number of characters in a string is n&lt;/li&gt;
&lt;li&gt;Reference types are generally O(n), where n is the length (for arrays) or the number of keys (for objects) since those made up with primitives, and the range is n.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum(array) {
  let total = 0
  // For Space Complexity the Big O is O(1) since there is only two places where memory is being alloweded and is not in a loop so no n
  for (let index = 0; index &amp;lt; array.length; index++) {
    total = array[index]
  }
  return total
}

function double(array) {
  let newArr = []
  // For Space Complexity the Big O is O(n) since there is newArr is adding n elments in memoary so the amount of memoary can run out at Infintiy
  for (let index = 0; index &amp;lt; array.length; index++) {
    newArr.push(2 * array[index])
  }
  return newArr
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Logarithmic
&lt;/h2&gt;

&lt;p&gt;Well they are not as common as the other Big O Notations, O(1), O(n), O(n²), they are sometimes Big O expressions involve more complex mathematical expressions, which is the expression of O(log n)&lt;/p&gt;

&lt;p&gt;Logs are the inverse of expressions, for example, log²(8) = 3 → 2³ = 8 or log²(value) = exponent → 2^exponent = value so you can move the exponent around and get the same results similar to multiple and division. Logs require a base to use them for more information on the Logarithmic look at this link.&lt;/p&gt;

&lt;p&gt;So when you see the log in Big O notation, it is the same as log².&lt;/p&gt;

&lt;p&gt;To simplify the log², think of it using a consistent value of 2. If n is always two, then it is an excellent candidate for O(log n).&lt;/p&gt;

&lt;h3&gt;
  
  
  Cliff Notes
&lt;/h3&gt;

&lt;p&gt;The logarithm of a number roughly measures the number of times you can divide that number by two before you get a value that's less than or equal to one.&lt;/p&gt;

&lt;p&gt;For example, 8 / 2 = 4 / 2 = 2 / 2 = 1 or log(8) = 3 since 8 is the base and 3 is the number of times log divide before reaching 0.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Does Logarithmic Complexity Matters
&lt;/h3&gt;

&lt;p&gt;O(log n) is the second-best expression to get next to O(1) since it does not go as much as O(1). In the same vane, there is O(n log n), which is the second-worst expression to get. Specific searching algorithms have logarithmic time complexity, efficient sorting algorithms involve logarithms, and recursion sometimes involves logarithmic space complex since they can take up n space.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analyzing Performance of Array &amp;amp; Objects
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Object
&lt;/h3&gt;

&lt;p&gt;Since objects are a type of data structure as they are key / values, unordered list. We can use Big O Notation on objects to do when you use them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Big O of Objects
&lt;/h4&gt;

&lt;p&gt;Here are the basics of how an object works with what you can do and how fast it takes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Insertion: O(1)&lt;/li&gt;
&lt;li&gt;Removal: O(1)&lt;/li&gt;
&lt;li&gt;Searching: O(n)&lt;/li&gt;
&lt;li&gt;Access: O(1)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since objects are an unordered list searching throw, it means that JavaScript would have to throw each value until the key is the same amount found. For Objects, in general, we would go in deeper depth when we get to the Hash Table.&lt;/p&gt;

&lt;h4&gt;
  
  
  Big O of Object Methods
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Object.keys: O(n) - Needs to throw each key to get the key&lt;/li&gt;
&lt;li&gt;Object.values: O(n) - Needs to throw each key to get its value&lt;/li&gt;
&lt;li&gt;Object.entries: O(n) - Needs to throw each key to get both the key and value&lt;/li&gt;
&lt;li&gt;hasOwnProperty: O(1) - Since hasOwnProperty has an argument that tells which key to search and return the value of that key&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Array
&lt;/h3&gt;

&lt;p&gt;Since arrays are a type of data structure as they are key / values, ordered list. We can use Big O Notation on arrays to do when you use them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Big O of Arrays
&lt;/h4&gt;

&lt;p&gt;Here are the basics of how an array works with what you can do and how fast it takes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Insertion: It depends&lt;/li&gt;
&lt;li&gt;Removal: It depends&lt;/li&gt;
&lt;li&gt;Searching: O(n)&lt;/li&gt;
&lt;li&gt;Access: O(1) - Since an array is linked to each element in memory, JavaScript knows which part of memory that element is located and gets the data from that part of memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When inserting or removing elements for an array, if you add or remove elements at the end of the array, then it is O(1) anywhere else it is O(n) since the array has to reindex each element after the new element.&lt;/p&gt;

&lt;h4&gt;
  
  
  Big O of Array Operations
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;push: O(1) - Adds to the end of the array&lt;/li&gt;
&lt;li&gt;pop: O(1) - Removes the end of the array&lt;/li&gt;
&lt;li&gt;shift: O(n) - Removes to the start of the array&lt;/li&gt;
&lt;li&gt;unshift: O(n) - Adds the start of the array&lt;/li&gt;
&lt;li&gt;concat: O(n) - Since this method merges tow or more arrays the size of those arrays are unknown n&lt;/li&gt;
&lt;li&gt;slice: O(n) - Since slice returns a copy part of an array since it is unknown how big that part is.&lt;/li&gt;
&lt;li&gt;splice: O(n) - Since splice adds or removes elements from the middle of an array so it is unknown how many elements would be affected by this function.&lt;/li&gt;
&lt;li&gt;sort: O(n * log n) - this one is too complex at the moment, have a look at sorting section&lt;/li&gt;
&lt;li&gt;forEach/map/filter/reduce/etc.: O(n): Needs to go over each of the element in an array&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.udemy.com/course/js-algorithms-and-data-structures-masterclass" rel="noopener noreferrer"&gt;JavaScript Algorithms and Data Structures Masterclass - Udemy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/big-o-notation-explained-with-examples" rel="noopener noreferrer"&gt;Big O Notation Explained with Examples - freecodecamp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/analysis-algorithms-big-o-analysis" rel="noopener noreferrer"&gt;Analysis of Algorithms | Big-O analysis - GeeksforGeeks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>computerscience</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
