{"id":67,"date":"2013-02-24T22:43:24","date_gmt":"2013-02-25T04:43:24","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=67"},"modified":"2020-07-02T09:08:37","modified_gmt":"2020-07-02T14:08:37","slug":"creating-a-linux-server-for-web-services","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/creating-a-linux-server-for-web-services\/","title":{"rendered":"Creating a Linux Server for Web Services"},"content":{"rendered":"<p>Howdy!<\/p>\n<p>In this post you&#8217;re going to learn how to download and install VirtualBox on your Mac to aid the creation of Linux servers to do web service development.  Often iOS applications require the need to know how to develop web services, as much of the functionality of a given app is actually &#8220;in the cloud&#8221;.  If you&#8217;ve developed any Facebook or Twitter applications, you&#8217;re already familiar with the concept of using a &#8220;service in the cloud&#8221;.  This tutorial is part of a series designed to bootstrap you into developing your own webservices.<\/p>\n<p>First, go grab VirtualBox!  You can find a link to download a Mac DMG <a href=\"https:\/\/www.virtualbox.org\/wiki\/Downloads\">here<\/a>.  Double-click the DMG and you&#8217;ll be presented with a window prompting you to double-click on the VirtualBox.pkg installer.<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/virtualbox_installer.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/virtualbox_installer.png\" alt=\"virtualbox_installer\" width=\"673\" height=\"386\" class=\"alignnone size-full wp-image-78\" \/><\/a><\/p>\n<p>Follow each of the prompts presented to you on the screen.  Once the installation is declared successful, go to your applications folder and launch VirtualBox.<\/p>\n<p>Now that we have VirtualBox running, we need an OS to install!  Our examples will use Ubuntu 12.04 LTS, which you will need to download from <a href=\"http:\/\/releases.ubuntu.com\/precise\/\">here<\/a>.  We&#8217;re going to use the <strong>server<\/strong> installer, as we don&#8217;t need a GUI.  To be precise, we&#8217;ll use the <a href=\"http:\/\/releases.ubuntu.com\/precise\/ubuntu-12.04.2-server-amd64.iso\">64-bit Ubuntu 12.04 Server<\/a>.  Clicking on this link will download an ISO image which we will give to VirtualBox to create us a Linux server on which we&#8217;ll host web services development!<\/p>\n<p>In the Oracle VM VirtualBox Manager screen, click New.  You&#8217;ll be prompted to identify the OS you are going to install and give it a name.  We&#8217;ll use webservicedev.  For Type, choose <strong>Linux<\/strong>, and for Version choose <strong>Ubuntu (64-bit)<\/strong>.<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/create_a_vm.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/create_a_vm.png\" alt=\"create_a_vm\" width=\"636\" height=\"407\" class=\"alignnone size-full wp-image-73\" \/><\/a><\/p>\n<p>You&#8217;ll then be prompted to select a memory size, for the VM, the default of 512M is sufficient for our purposes.  When prompted for the hard drive settings, choose <strong>Create a virtual hard drive now<\/strong>.  When prompted for the hard drive file type, choose <strong>VDI (Virtual Disk Image)<\/strong>.  Choose <strong>Dynamically allocated<\/strong> when prompted, and accept the defaults for the File location and size (8GB).<\/p>\n<p>You will now see your system configured and ready to go.  You need to make one adjustment here, and that is setting the initial boot device to the Ubuntu ISO image you downloaded.  Select the webservicedev machine in the left-hand menu of the Oracle VM VirtualBox Manager and right click and choose Settings.  You&#8217;ll be presented with a menu of settings like this:<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/settings_window.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/settings_window.png\" alt=\"settings_window\" width=\"568\" height=\"400\" class=\"alignnone size-full wp-image-77\" \/><\/a><\/p>\n<p>Click the Storage icon to see:<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/default_storage_menu.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/default_storage_menu.png\" alt=\"default_storage_menu\" width=\"568\" height=\"465\" class=\"alignnone size-full wp-image-74\" \/><\/a><\/p>\n<p>Click on the CD Entry that says Empty, and then on the right-hand side of the window, click on the little CD next to the drop-down menu for CD\/DVD Drive.  Clicking on the little CD icon will give you the option to Choose a virtual CD\/DVD disk file.  Select this option and use the Finder navigator to where you saved the file ubuntu-12.04.2-server-amd64.iso (most likely in the Downloads folder).<\/p>\n<p>One last thing, click on the Network icon at the top of webservicedev settings window.  Change the value &#8216;NAT&#8217; under Adapter 1 to Bridged Adapter and under name, select whichever network adapter your Mac is connected to your router on.  In our example, we use Wi-Fi as the only network connection for the Mac.<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/network_adapter_menu.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/network_adapter_menu.png\" alt=\"network_adapter_menu\" width=\"568\" height=\"470\" class=\"alignnone size-full wp-image-75\" \/><\/a><\/p>\n<p>Now, click on OK to dismiss the Settings window and boot your VM!  Select webservicedev in the menu and click on the big green arrow that says Start.  The Ubuntu installer should boot and you can walk yourself through the installer.  Our example will be using English, but make selections appropriate for you!<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/virtualbox_installer_start.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/virtualbox_installer_start.png\" alt=\"virtualbox_installer_start\" width=\"640\" height=\"523\" class=\"alignnone size-full wp-image-79\" \/><\/a><\/p>\n<p>After selecting the Language, choose Install Ubuntu Server and then follow the prompts and make the selections appropriate for your language, keyboard layout, etc.  Here are the values we used when installing:<\/p>\n<ul>\n<li>Hostname:  webservicedev\n<\/li>\n<li>Full name for the new user:  webservice\n<\/li>\n<li>Username for the account:  webservice\n<\/li>\n<li>Encrypt your home directory?  No\n<\/li>\n<li>Timezone:  UTC\n<\/li>\n<li>Guided Partitioning:  use entire disk\n<\/li>\n<\/ul>\n<p>Continue the prompts to partition the disk, if you need help or have questions about the Ubuntu installer there are plenty of guides on the web.  <a href=\"https:\/\/help.ubuntu.com\/lts\/installation-guide\/i386\/module-details.html#partman-auto\">This one<\/a> you might find to be helpful.  In general for development systems on your local machine, you don&#8217;t need fancy partitioning.  If you find yourself as the server administrator for production servers, you might bone up on advanced partitioning schemes.<\/p>\n<p>When prompted about an HTTP proxy, you most likely can leave it blank, unless you really need one (in which case you should be familiar with filling out this kind of information).<\/p>\n<p>When prompted about managing upgrades and updates, choose No automatic updates (this is our personal preference).<\/p>\n<p>When prompted, you will definitely want to install <em>OpenSSH server<\/em>, and you can skip the rest for now.  There are number of packages we&#8217;ll install after the system initially boots.<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/openssh_installer.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/openssh_installer.png\" alt=\"openssh_installer\" width=\"800\" height=\"643\" class=\"alignnone size-full wp-image-76\" \/><\/a><\/p>\n<p>Finally, Install the GRUB boot loader to the master boot record should be Yes.  Press ENTER when you see the Installation is complete screen.  Congratulations, your new VM should now be booting and momentarily you will see something similar to this:<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/webservice_dev_running.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2013\/02\/webservice_dev_running.png\" alt=\"webservice_dev_running\" width=\"640\" height=\"523\" class=\"alignnone size-full wp-image-72\" \/><\/a><\/p>\n<p>Log in with the username and password you selected.  We don&#8217;t like working in the VirtualBox screen, so we run <code>ifconfig<\/code> to find the IP address assigned to the VM, and then <code>ssh<\/code> into it with iTerm2.<\/p>\n<p>Now, this VM is functional, but we&#8217;re going to be doing some web service hosting on it, and to do that we&#8217;re going to need quite a few software packages.  Let&#8217;s outline what we&#8217;ll be installing and <i>why<\/i> first<\/p>\n<ul>\n<li>Ruby &#8211; because our web service is written in Ruby\n<\/li>\n<li>Bundler &#8211; because it is <i>the<\/i> way to manage Ruby gems in a web application\n<\/li>\n<li>Passenger &#8211; because we like the <a href=\"https:\/\/www.phusionpassenger.com\/\">Passenger<\/a> module for running Ruby code in a web server<\/li>\n<li>Nginx &#8211; because we like to run <a href=\"http:\/\/wiki.nginx.org\/Main\">Nginx<\/a> as our web server\n<\/li>\n<li>GCC and make &#8211; because they are required to build modules and libraries needed by the aforementioned\n<\/li>\n<li>Other development packages required by Passenger\n<\/li>\n<li>Git &#8211; because we&#8217;ll need to check our code out onto the web server, and we use github.com<\/ul>\n<\/li>\n<p>Well, let&#8217;s get started.  You can painstakingly type <code>sudo<\/code> before each command, but we know a lot of these commands are going to require sudo, so we&#8217;ll just switch now.<\/p>\n<pre>\nLast login: Mon Feb 25 03:34:12 2013\nwebservice@webservicedev:~$ sudo su \n[sudo] password for webservice: \nroot@webservicedev:\/home\/webservice# \n<\/pre>\n<p>Let&#8217;s get Ruby going first.<\/p>\n<pre>\nroot@webservicedev:\/home\/webservice# apt-get install ruby1.9.1\nReading package lists... Done\nBuilding dependency tree       \nReading state information... Done\nThe following extra packages will be installed:\n  libruby1.9.1 libyaml-0-2\nSuggested packages:\n  ruby1.9.1-examples ri1.9.1 graphviz ruby1.9.1-dev\nThe following NEW packages will be installed:\n  libruby1.9.1 libyaml-0-2 ruby1.9.1\n0 upgraded, 3 newly installed, 0 to remove and 5 not upgraded.\nNeed to get 4,196 kB of archives.\nAfter this operation, 12.7 MB of additional disk space will be used.\nDo you want to continue [Y\/n]? \n<\/pre>\n<p>Select Y and watch the <code>apt-get<\/code> go and fetch everything it needs to install Ruby 1.9.3 (for an explanation as why Ruby 1.9.3 is installed when running <code>apt-get install ruby1.9.1<\/code> see <a href\"\">here<\/a>).<\/p>\n<p>Now, install make and g++, and this time we&#8217;ll use the <code>-y<\/code> option to <code>apt-get<\/code> to speed things up.  We&#8217;ve also included make and g++ on the same line.<\/p>\n<p><code><br \/>\nroot@webservicedev:\/home\/webservice# apt-get -y install make g++<br \/>\n<\/code><\/p>\n<p>When we install Nginx it will require a number of dependencies, so let&#8217;s get them out of the way by installing <code>ruby1.9.1-dev<\/code>, <code>zlib1g-dev<\/code>, <code>libssl-dev<\/code>, and <code>libcurl4-openssl-dev<\/code>.<\/p>\n<p><code><br \/>\nroot@webservicedev:\/home\/webservice# apt-get -y install ruby1.9.1-dev zlib1g-dev libssl-dev libcurl4-openssl-dev<br \/>\n<\/code><\/p>\n<p>Now, install the <code>passenger<\/code> gem with <code>gem install passenger<\/code> .  You should see something similar to:<\/p>\n<pre>\nroot@webservicedev:\/home\/webservice# gem install passenger\nFetching: rake-10.0.3.gem (100%)\nFetching: fastthread-1.0.7.gem (100%)\nBuilding native extensions.  This could take a while...\nFetching: daemon_controller-1.1.1.gem (100%)\nFetching: rack-1.5.2.gem (100%)\nFetching: passenger-3.0.19.gem (100%)\nSuccessfully installed rake-10.0.3\nSuccessfully installed fastthread-1.0.7\nSuccessfully installed daemon_controller-1.1.1\nSuccessfully installed rack-1.5.2\nSuccessfully installed passenger-3.0.19\n5 gems installed\nInstalling ri documentation for rake-10.0.3...\nInstalling ri documentation for fastthread-1.0.7...\nInstalling ri documentation for daemon_controller-1.1.1...\nInstalling ri documentation for rack-1.5.2...\nInstalling ri documentation for passenger-3.0.19...\nInstalling RDoc documentation for rake-10.0.3...\nInstalling RDoc documentation for fastthread-1.0.7...\nInstalling RDoc documentation for daemon_controller-1.1.1...\nInstalling RDoc documentation for rack-1.5.2...\nInstalling RDoc documentation for passenger-3.0.19...\n<\/pre>\n<p>And before we install Nginx, install <code>bundler<\/code> too.<\/p>\n<pre>\nroot@webservicedev:\/home\/webservice# gem install bundler\nFetching: bundler-1.2.4.gem (100%)\nSuccessfully installed bundler-1.2.4\n1 gem installed\nInstalling ri documentation for bundler-1.2.4...\nInstalling RDoc documentation for bundler-1.2.4...\n<\/pre>\n<p>This one will take a while, but if you&#8217;ve followed all the steps up until now, run <code>passenger-install-nginx-module<\/code>.  You will prompted to <i>Press Enter to continue<\/i> so press Enter (to continue!).  You want to see<\/p>\n<pre>\nChecking for required software...\n\n * GNU C++ compiler... found at \/usr\/bin\/g++\n * The 'make' tool... found at \/usr\/bin\/make\n * A download tool like 'wget' or 'curl'... found at \/usr\/bin\/wget\n * Ruby development headers... found\n * OpenSSL support for Ruby... found\n * RubyGems... found\n * Rake... found at \/usr\/local\/bin\/rake\n * rack... found\n * Curl development headers with SSL support... found\n * OpenSSL development headers... found\n * Zlib development headers... found\n<\/pre>\n<p>followed by <i>Enter your choice (1 or 2) or press Ctrl-C to abort:<\/i>  Enter 1.  The automated installer will automatically download all of the source necessary to build and install Nginx, a high performance web server.  Select the defaults when prompted.<\/p>\n<p>If all goes well (and why shouldn&#8217;t it, right?), you should see<\/p>\n<pre>\nNginx with Passenger support was successfully installed.\n\nThe Nginx configuration file (\/opt\/nginx\/conf\/nginx.conf)\nmust contain the correct configuration options in order for Phusion Passenger\nto function correctly.\n\nThis installer has already modified the configuration file for you! The\nfollowing configuration snippet was inserted:\n\n  http {\n      ...\n      passenger_root \/var\/lib\/gems\/1.9.1\/gems\/passenger-3.0.19;\n      passenger_ruby \/usr\/bin\/ruby1.9.1;\n      ...\n  }\n\nAfter you start Nginx, you are ready to deploy any number of Ruby on Rails\napplications on Nginx.\n\nPress ENTER to continue.\n<\/pre>\n<p>That&#8217;s actually the end of this tutorial, as its sort of a small step towards getting a fully functioning web services web server running.  If you really want to try things out <i>now<\/i>, go ahead and install <code>git-core<\/code>.<\/p>\n<p><code><br \/>\nroot@webservicedev:\/home\/webservice# apt-get install git-core<br \/>\n<\/code><\/p>\n<p>Then, let&#8217;s create a directory to hold our webservice code:<\/p>\n<p><code><br \/>\nmkdir -p \/web\/apps\/webservice<br \/>\nchown webservice.webservice -R \/web\/<br \/>\n<\/code><\/p>\n<p>If you are still running around as root, type <code>exit<\/code> to go back to the webservice user.  Continue and check out the code:<\/p>\n<pre>\ncd \/web\/apps\/webservice\nwebservice@webservicedev:\/web\/apps\/webservice$ git clone https:\/\/github.com\/iachievedit\/secureWebService\nCloning into 'secureWebService'...\nremote: Counting objects: 23, done.\nremote: Compressing objects: 100% (19\/19), done.\nremote: Total 23 (delta 8), reused 19 (delta 4)\nUnpacking objects: 100% (23\/23), done.\n<\/pre>\n<p>Prepare the area to run as a Passenger application by creating a <code>public<\/code> directory and then execute <code>bundle install<\/code>:<\/p>\n<pre>\nwebservice@webservicedev:\/web\/apps\/webservice\/secureWebService$ mkdir public\nwebservice@webservicedev:\/web\/apps\/webservice\/secureWebService$ bundle install\nFetching gem metadata from https:\/\/rubygems.org\/..........\nFetching gem metadata from https:\/\/rubygems.org\/..\nInstalling json (1.7.5) with native extensions \nUsing rack (1.5.2) \nInstalling rack-protection (1.3.2) \nInstalling tilt (1.3.3) \nInstalling sinatra (1.3.4) \nUsing bundler (1.2.4) \nYour bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.\n<\/pre>\n<p>Now, edit your <code>nginx.conf<\/code> (found in <code>\/opt\/nginx\/conf\/<\/code>, and you will need to use <code>sudo vi<\/code>) to look exactly like this (unless you know what you are doing, use the default <code>passenger_root<\/code> and <code>passenger_ruby<\/code> directives).<\/p>\n<pre>\nworker_processes  1;\n\nevents {\n    worker_connections  1024;\n}\n\n\nhttp {\n    passenger_root \/var\/lib\/gems\/1.9.1\/gems\/passenger-3.0.19;\n    passenger_ruby \/usr\/bin\/ruby1.9.1;\n\n    include       mime.types;\n    default_type  application\/octet-stream;\n\n    sendfile        on;\n    keepalive_timeout  65;\n\n    server {\n        listen       80;\n        server_name  webservicedev;\n        passenger_enabled on;\n        root \/web\/apps\/webservice\/secureWebService\/public; \n    }\n}\n<\/pre>\n<p>Start the <code>nginx<\/code> process with <code>sudo \/opt\/nginx\/sbin\/nginx<\/code> and then point your browser to the web service, for example, http:\/\/192.168.0.114\/resource\/100 for us displays:<\/p>\n<p><code><br \/>\n{\"resource\":\"100\",\"status\":\"OK\",\"values\":{\"key1\":\"value1\",\"key2\":\"value2\"}}<br \/>\n<\/code><\/p>\n<p>Now, for our disclaimer:  this is <b>not the way<\/b> we will be deploying our web service application to the web server!  We&#8217;ll be using <a href=\"https:\/\/github.com\/capistrano\/capistrano\">Capistrano<\/a> to deploy our applications from the Mac onto the web server.  Stay tuned for that blog post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Howdy! In this post you&#8217;re going to learn how to download and install VirtualBox on your Mac to aid the creation of Linux servers to do web service development. Often iOS applications require the need to know how to develop web services, as much of the functionality of a given app is actually &#8220;in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-67","post","type-post","status-publish","format-standard","hentry","category-web-services"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/67"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":9,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":4160,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/67\/revisions\/4160"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}