Skip to content
On this page

Deploy to server

Video instruction

This video shows how to deploy OCULA AI to the server:

Install required packages

We will use Ubuntu 22.04 as OS. To deploy the application to the server, you will need complete the following steps:

bash
sudo apt update

sudo apt install nginx -y
sudo apt-get install unzip -y
sudo apt-get install supervisor -y

sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y

sudo apt-get install -y php8.1-fpm \
       php8.1-pgsql php8.1-sqlite3 php8.1-gd \
       php8.1-curl \
       php8.1-imap php8.1-mysql php8.1-mbstring \
       php8.1-xml php8.1-zip php8.1-bcmath php8.1-soap \
       php8.1-intl php8.1-readline \
       php8.1-ldap -y

sudo apt install postgresql -y

curl -sLS https://deb.nodesource.com/setup_16.x | sudo bash -
sudo apt-get install -y nodejs -y
sudo npm install -g npm

sudo apt install jpegoptim optipng pngquant gifsicle -y
sudo npm install -g svgo

sudo systemctl start nginx
sudo systemctl start php8.1-fpm
sudo systemctl start postgresql

TIP

You're not limited to use PostgreSQL. You can MySQL or SQLite as well.

Nginx configuration

bash
sudo rm -rf /etc/nginx/sites-available/default
sudo nano /etc/nginx/sites-available/default
nginx
server {
    listen 80;
    server_name <your_domain>;
    root /var/www/html/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Test the nginx configuration by running the following command:

bash
sudo nginx -t

If the configuration is valid, restart the nginx service to apply the changes:

sudo systemctl restart nginx

PostgreSQL configuration

We will need to create a postgresql user and database for our web application. Log in to postgres as the postgres user by running the following command:

bash
sudo -u postgres psql

Once inside the postgres shell, create a new user by running the following command:

CREATE USER ocula_user WITH PASSWORD 'secret_password';

Then, create a new database for the user by running the following command:

CREATE DATABASE ocula_db OWNER ocula_user;

Exit the postgres shell by running the following command:

\q

Upload project files

Remove /var/www/html folder:

sudo rm -rf /var/www/html

We need to upload project files using scp Open your local terminal and run:

bash
scp <local/path/to/project_archive> <username>@<host>:~/html.zip

Then run using ssh:

bash
sudo unzip ~/main.zip -d /var/www

sudo mv /var/www/ai-avatars-main /var/www/html

Environment variables

Clone .env.example and put env variables:

bash
sudo cp /var/www/html/.env.example /var/www/html/.env

Required variables:

bash
APP_NAME=<your_app_name>
APP_ENV=production
APP_DEBUG=false
APP_URL=<your_domain>

DB_CONNECTION=<connection> # mysql, pgsql, sqlite
DB_HOST=127.0.0.1
DB_PORT=<port>
DB_DATABASE=<database>
DB_USERNAME=<user>
DB_PASSWORD=<password>

MAIL_MAILER=<mail_driver> # set "log" to disable email sending

BACKEND_API_KEY=<your_api_key>

Image optimization is disabled by default. To enable it, update the following line to the .env file:

bash
IMAGE_OPTIMIZATION_ENABLED=true

You can use nano or vim to edit the .env file.

GPU API key

Do not forget to add your GPU API key to the .env file. Create an account on our GPU platform and obtain the API key.

Then update the following line to the .env file:

BACKEND_API_KEY=<your_api_key>

Composer

First, download the installer script by running the command:

curl -sS https://getcomposer.org/installer -o composer-setup.php

Next, verify the installer's authenticity by running:

HASH=`curl -sS https://composer.github.io/installer.sig`
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

Run the installer script by running:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Finally, to add Composer to your system's PATH, run:

echo 'export PATH="$PATH:$HOME/.config/composer/vendor/bin"' >> ~/.bashrc

Then you can run to apply the changes:

bash
source ~/.bashrc

Folder permissions

bash
sudo chown -R www-data:www-data /var/www/html

sudo usermod -a -G www-data $USER

sudo find /var/www/html -type f -exec chmod 644 {} \;    
sudo find /var/www/html -type d -exec chmod 755 {} \;

cd /var/www/html

sudo chown -R $USER:www-data .

sudo find . -type f -exec chmod 664 {} \;   
sudo find . -type d -exec chmod 775 {} \;

sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

Install composer dependencies

bash
cd /var/www/html
composer install --no-dev -o

Artisan commands

bash
cd /var/www/html

php artisan key:generate --force
php artisan storage:link

sudo -u www-data php artisan migrate --seed --force

php artisan optimize

Also create admin user:

bash
php artisan make:admin

Supervisor

To run the queue workers, create a new configuration file for the default queue:

bash
sudo nano /etc/supervisor/conf.d/laravel-default.conf

Default worker configuration:

bash
[program:laravel-worker-default]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work --queue=default --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/html/worker.log
stopwaitsecs=3600

Image processing worker (optional)

If you want to use image processing worker (see Image optimization), you can use this configuration:

bash
sudo nano /etc/supervisor/conf.d/laravel-image-processing.conf
bash
[program:laravel-worker-image-processing]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work --queue=media-processing --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/html/worker-image-processing.log
stopwaitsecs=3600

Run workers

bash
sudo supervisorctl reread
 
sudo supervisorctl update

Increase file upload size

bash
# Nginx
echo "client_max_body_size 128M;" | sudo tee /etc/nginx/conf.d/upload_size.conf
sudo service nginx restart

# PHP-fpm
sudo sed -i 's/upload_max_filesize = .*/upload_max_filesize = 128M/' /etc/php/8.1/fpm/php.ini 
sudo sed -i 's/post_max_size = .*/post_max_size = 128M/' /etc/php/8.1/fpm/php.ini 
sudo service php8.1-fpm restart