# Turtl API Server and Client Installation CentOS 7

## Turtl API
### Clone and Configure Turtl API
1. Create a user for turtl API: `sudo useradd turtl`
2. Switch user to `turtl`: `sudo su turtl`
3. Change directory to `turtl`'s home: `cd ~`
1. Install [Clozure CL](https://wiki.twcloud.tech/books/linux/page/installing-clozure-cl-and-quicklisp-on-centos-7)
2. Install [RethinkDB](https://wiki.twcloud.tech/books/linux/page/install-rethinkdb-on-centos-7) and create an instance for Turtl API
3. Install `libuv`: `sudo yum install libuv`
4. Clone Turtl repo: `git clone https://github.com/turtl/api.git`
5. Copy Turtl API config: `cp config/config.default.lisp config/lisp`
6. Edit and configure `config/config.lisp`, make sure to update the following parameters:
  ```
(defvar *local-upload* "<local upload directory>"
(defvar *local-upload-url* "<upload url>"
  ```

### Setup Up Service
Create a service file at `/usr/lib/systemd/system/turtl.service` with the following entry:

```
[Unit]
Description=Turtl API Server

[Service]
User=turtl
Group=turtl
ExecStart=/usr/local/bin/ccl64 --load /home/turtl/api/start.lisp
KillMode=process

[Install]
WantedBy=multi-user.target
```

Start and enable the service:

```
sudo systemctl start turtl
sudo systemctl enable turtl
```

### (OPTIONAL) Configure Reverse Proxy in Apache
Create `httpd` virtual host configuration `/etc/httpd/conf.d/turtl.conf` with the following content, make sure to change `<turtl domain>` to your own domain name:

```
<VirtualHost *:80>
    #Server name configuration
    ServerName <turtl domain>
    ServerAdmin webmaster@<turtl domain>

    #Proxy configuration
    ProxyPreserveHost on
    ProxyRequests off
    ProxyPass / https://wiki.twcloud.tech:8181/
    ProxyPassReverse / https://wiki.twcloud.tech:8181/

    #Logging configuration
    ErrorLog /var/log/httpd/turtl.err
    LogLevel warn
</VirtualHost>
```

### (OPTIONAL) Restrict User Registration
Add the following lines in your Turtl API Virtual Hosts configuration:

```
    #Restrict Registration
    <LocationMatch  "^/users[/]?$">
        AuthType Basic
        AuthName "Restricted"
        AuthUserFile /home/turtl/.htpasswd
        Require valid-user 
    </LocationMatch>
```

Then generate a `.htpasswd` password file in `/home/turtl`: `sudo htpasswd -c /etc/apache2/.htpasswd <whatever username>`. Make sure that it's in the right permission and owner: `chmod 640 /home/turtl/.htpasswd && chown turtl:apache /home/turtl/.htpasswd`

## Installing JS Client
1. Clone `turtl/js` repo to webserver webroot: `sudo mkdir /var/www/turtl && cd /var/www/turtl && sudo git clone https://github.com/turtl/js.git .`
2. Install NodeJS dependencies: `npm install`
3. Copy default config: `cp config/config.js.default config.js`
4. Edit `config/config.js`
5. Update owner and group: `chown -R apache:apache .`
6. Generate assets: `make`