|
| 1 | +--- |
| 2 | +title: Deploy as CGI application |
| 3 | +description: Tutorial on how to deploy CGI web application built with Fano Framework to various web servers. |
| 4 | +--- |
| 5 | + |
| 6 | +<h1 class="major">Deployment as CGI application</h1> |
| 7 | + |
| 8 | +### Apache |
| 9 | + |
| 10 | +This section explains how to deploy web application as CGI application on Apache web server. |
| 11 | + |
| 12 | +#### Dedicated server |
| 13 | + |
| 14 | +This section explains how to deploy server that you have full control and administrative privilege (aka root access). |
| 15 | + |
| 16 | +##### Debian-based Linux |
| 17 | + |
| 18 | +- Create virtual host by adding Apache configuration file inside `/etc/apache2/sites-available` and put |
| 19 | + |
| 20 | +``` |
| 21 | +<VirtualHost *:80> |
| 22 | + ServerAdmin [[server administrator email]] |
| 23 | + ServerName [[fano application hostname]] |
| 24 | +
|
| 25 | + DocumentRoot "[[/path/to/fano/app/public]]" |
| 26 | +
|
| 27 | + <Directory "[[/path/to/fano/app/public]]"> |
| 28 | + Options -MultiViews +ExecCGI |
| 29 | + AllowOverride FileInfo Indexes |
| 30 | + Require all granted |
| 31 | + AddHandler cgi-script .cgi |
| 32 | + DirectoryIndex app.cgi |
| 33 | + </Directory> |
| 34 | + </VirtualHost> |
| 35 | +``` |
| 36 | + |
| 37 | +Replace `[[server administrator email]]` with administrator email for example, if we use domain name `fano.dev` we can use `admin@fano.dev`. |
| 38 | + |
| 39 | +Replace `[[fano application hostname]]` with domain that we use, |
| 40 | +for example in this case, it is `fano.dev`. So when Apache receive request with URI http://fano.dev/test it will match |
| 41 | +this virtual host entry and call our application. |
| 42 | + |
| 43 | + |
| 44 | +Replace `[[/path/to/fano/app/public]]` with directory where our application binary reside. If you store it outside Apache |
| 45 | +document root directory, you need to specify with with `<Directory>`. |
| 46 | + |
| 47 | +For example, if we store application binary in directory `/home/myuser/myapp/public` which outside Apache document root, we need to |
| 48 | +add `Require` directive to allow Apache to serve request from |
| 49 | +this directory, otherwise they will be rejected. |
| 50 | + |
| 51 | +Above snippet basically tells Apache to allow (`+ExecCGI`) to serve any file ends with `cgi` extension as CGI script (`AddHandler cgi-script .cgi`). `-MultiViews` also important to disable automatic content negotiation because we need to add URL rewriting to allow use of routing in our application. So Our application will be responsible to decide what response to return. |
| 52 | + |
| 53 | +So in our example, we end up with following configuration: |
| 54 | + |
| 55 | +``` |
| 56 | +<VirtualHost *:80> |
| 57 | + ServerAdmin admin@fano.dev |
| 58 | + ServerName fano.dev |
| 59 | +
|
| 60 | + DocumentRoot "/home/myuser/myapp/public" |
| 61 | +
|
| 62 | + <Directory "/home/myuser/myapp/public"> |
| 63 | + Options -MultiViews +ExecCGI |
| 64 | + AllowOverride FileInfo Indexes |
| 65 | + Require all granted |
| 66 | + AddHandler cgi-script .cgi |
| 67 | + DirectoryIndex app.cgi |
| 68 | + </Directory> |
| 69 | + </VirtualHost> |
| 70 | +``` |
| 71 | + |
| 72 | +You need to make sure that `app.cgi` has execution bit, if not then run |
| 73 | + |
| 74 | +``` |
| 75 | +$ chmod 744 /home/myuser/myapp/public/app.cgi |
| 76 | +``` |
| 77 | +For most uses-cases, `744` permission is suffice if your web server is run as your user account. If executable binary failed to run, try `774` |
| 78 | + |
| 79 | +Save it as `fano.conf` file. Note that saving data to `/etc/apache2/sites-available` directory requires administrative privilege. |
| 80 | + |
| 81 | +- Enable virtual host |
| 82 | + |
| 83 | +To make our virtual host active, put symlink in `/etc/apache2/sites-enabled` that points to `/etc/apache2/sites-available/fano.conf`. |
| 84 | + |
| 85 | +``` |
| 86 | +$ sudo ln -s /etc/apache2/sites-available/fano.conf /etc/apache2/sites-enabled/fano.conf |
| 87 | +``` |
| 88 | +or you can use `a2ensite` utility that comes with Debian distribution which does same thing. |
| 89 | + |
| 90 | +``` |
| 91 | +$ sudo a2ensite fano.conf |
| 92 | +``` |
| 93 | + |
| 94 | +- Restart Apache service |
| 95 | + |
| 96 | +``` |
| 97 | +$ sudo service apache2 restart |
| 98 | +``` |
| 99 | + |
| 100 | +If configuraton is ok, Apache will restart happily. |
| 101 | + |
| 102 | +- Setup domain name to DNS |
| 103 | + |
| 104 | +If you have not register a domain name with domain name registrar, you can setup fake domain name for local development only by adding |
| 105 | +entry to `/etc/hosts` file. Open `/etc/hosts` and add following entry, |
| 106 | + |
| 107 | +``` |
| 108 | +127.0.0.1 fano.dev |
| 109 | +``` |
| 110 | + |
| 111 | +`127.0.0.1` with assumption that Apache run on same machine. |
| 112 | + |
| 113 | +##### Fedora-based Linux |
| 114 | + |
| 115 | +#### Shared-hosting |
| 116 | + |
| 117 | +This section explains how to deploy server that you have no full control and have very limited administrative privelege. |
| 118 | + |
| 119 | +##### Debian-based Linux |
| 120 | + |
| 121 | +##### Fedora-based Linux |
| 122 | + |
| 123 | +##### CPanel-based server |
| 124 | + |
| 125 | +### Nginx |
| 126 | + |
| 127 | +This section explains how to deploy web application as CGI application on Nginx web server. |
0 commit comments