Skip to content

Instantly share code, notes, and snippets.

@JuanitoFatas
Created June 18, 2014 02:27
Show Gist options
  • Save JuanitoFatas/77656cd6e48778039699 to your computer and use it in GitHub Desktop.
Save JuanitoFatas/77656cd6e48778039699 to your computer and use it in GitHub Desktop.
-### Setting Up the WEBrick SSL Server on LocalHost
+Setting Up the WEBrick SSL Server on LocalHost
+==============================================
-With WEBrick as the default development server, one cannot just configure Rails to enable SSL, because WEBrick must be invoked with SSL support turned on. This means that modifying `config.ru` is necessary.
+With WEBrick as the default development server, one cannot just configure Rails
+to enable SSL, because WEBrick must be invoked with SSL support turned on. This
+means that modifying `config.ru` is necessary.
-WEBrick is a Rack-based server. The `config.ru` file is used by Rack-based servers to start the application.
+WEBrick is a Rack-based server. The `config.ru` file is used by Rack-based
+servers to start the application.
-Out of the box, Rails with WEBrick server does not provide an easy solution to build an SSL server using HTTPS for development purposes on localhost.
+Out of the box, Rails with WEBrick server does not provide an easy solution to
+build an SSL server using HTTPS for development purposes on localhost.
-Running an HTTPS WEBrick server on `https://localhost:8080` is not difficult and can be accomplished as described below.
+Running an HTTPS WEBrick server on `https://localhost:8080` is not difficult and
+can be accomplished as described below.
-Open the `config.ru` file which is located in the root directory of the Rails application.
+Open the `config.ru` file which is located in the root directory of the Rails
+application.
-Insert the following code at the top of the config.ru file above all default `require` code.
+Insert the following code at the top of the config.ru file above all default
+`require` code.
```ruby
require 'openssl' # Implements the Secure Sockets Layer protocols.
@@ -20,9 +28,11 @@ require 'webrick/https' # Configures WEBrick as an HTTPS server.
The application is now ready to create the HTTPS server.
-Delete `run Rails.application`. Insert the code below in place of `run Rails.application`.
+Delete `run Rails.application`. Insert the code below in place of
+`run Rails.application`.
-The additional options specifically tell WEBrick to boot up with SSL support and it also provides the SSL private key and SSL certificate name.
+The additional options specifically tell WEBrick to boot up with SSL support and
+it also provides the SSL private key and SSL certificate name.
```ruby
Rack::Handler::WEBrick.run Rails.application, {
@@ -35,18 +45,27 @@ Rack::Handler::WEBrick.run Rails.application, {
)
}
```
-WEBrick needs to access the private key and the certificate. So, the `certs/server.key` and `certs/server.crt` files must be created. Note that both files refer to keys and certificates the developer creates on their local environments and are self-signed certificates.
-Create the certs directory inside the root directory of the application using the `mkdir certs` command.
+WEBrick needs to access the private key and the certificate. So, the
+`certs/server.key` and `certs/server.crt` files must be created. Note that both
+files refer to keys and certificates the developer creates on their local
+environments and are self-signed certificates.
+
+Create the certs directory inside the root directory of the application using
+the `mkdir certs` command.
Once a certs directory is created then run the following commands.
+
```ruby
cd certs
ssh-keygen -f server.key
openssl req -new -key server.key -out request.csr
openssl x509 -req -days 365 -in request.csr -signkey server.key -out server.crt
```
-Note that the `ssh-keygen` command generates a "self-signed" certificate. Also note that when running both `openssl` commands, other security related questions will be asked. Follow the prompts accordingly.
+
+Note that the `ssh-keygen` command generates a "self-signed" certificate. Also
+note that when running both `openssl` commands, other security related questions
+will be asked. Follow the prompts accordingly.
It is time to start the HTTPS server.
Type `rails s`.
@@ -72,11 +91,16 @@ Certificate:
...
[2014-05-31 16:42:53] INFO WEBrick::HTTPServer#start: pid=2443 port=8080
```
-If the output above shows up, a successful HTTPS server has been created on port 8080 on localhost.
-###Stopping the Rails WEBrick SSL Server
+If the output above shows up, a successful HTTPS server has been created on port
+8080 on localhost.
-Be aware that `ctrl-c` might not be enough to kill the process, so it may be necessary to stop the process manually through the `ps` or `kill -9 [PID]` commands.
+Stopping the Rails WEBrick SSL Server
+-------------------------------------
+
+Be aware that `ctrl-c` might not be enough to kill the process, so it may be
+necessary to stop the process manually through the `ps` or `kill -9 [PID]`
+commands.
For example, one can run the following commands to kill the process.
@@ -84,6 +108,9 @@ For example, one can run the following commands to kill the process.
This chained command will provide an output similar to the following.
-`$ 2443 0.0 1.5 2578296 123204 s000 T 4:42PM 0:02.98 $/.rbenv/versions/2.1.0/bin/ruby bin/rails s`
+```bash
+$ 2443 0.0 1.5 2578296 123204 s000 T 4:42PM 0:02.98 $/.rbenv/versions/2.1.0/bin/ruby bin/rails s`
+```
-The first number "2443" is the process id number. In the example, `kill -9 2443` will shutdown the server.
\ No newline at end of file
+The first number "2443" is the process id number. In the example, `kill -9 2443`
+will shutdown the server.
\ No newline at end of file
@JuanitoFatas
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment