Skip to content

Installing

Akram El Assas edited this page Jun 22, 2024 · 60 revisions

BookCars is cross-platform and can run and be installed on Windows, Linux and macOS.

Below are the installation instructions on Linux.

Prerequisites

  1. Install git, Node.js, NGINX, MongoDB and mongosh. If you want to use MongoDB Atlas, you can skip installing and configuring MongoDB.

  2. Configure MongoDB:

mongosh

Create admin user:

db = db.getSiblingDB('admin')
db.createUser({ user: "admin" , pwd: "PASSWORD", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]})

Replace PASSWORD with a strong password.

Secure MongoDB:

sudo nano /etc/mongod.conf

Change configuration as follows:

net:
  port: 27017
  bindIp: 0.0.0.0

security:
  authorization: enabled

Restart MongoDB service:

sudo systemctl restart mongod.service
sudo systemctl status mongod.service

Instructions

  1. Clone BookCars repo:
cd /opt
sudo git clone https://github.com/aelassas/bookcars.git
  1. Add permissions:
sudo chown -R $USER:$USER /opt/bookcars
sudo chmod -R +x /opt/bookcars/__scripts
  1. Create deployment shortcut:
sudo ln -s /opt/bookcars/__scripts/bc-deploy.sh /usr/local/bin/bc-deploy
  1. Create BookCars service:
sudo cp /opt/bookcars/__services/bookcars.service /etc/systemd/system
sudo systemctl enable bookcars.service
  1. Create /opt/bookcars/api/.env file with the following content:
NODE_ENV=production
BC_PORT=4002
BC_HTTPS=false
BC_PRIVATE_KEY=/etc/ssl/bookcars.com.key
BC_CERTIFICATE=/etc/ssl/bookcars.com.crt
BC_DB_URI=mongodb://admin:PASSWORD@127.0.0.1:27017/bookcars?authSource=admin&appName=bookcars
BC_DB_SSL=false
BC_DB_SSL_CERT=/etc/ssl/bookcars.com.crt
BC_DB_SSL_CA=/etc/ssl/bookcars.com.ca.pem
BC_DB_DEBUG=false
BC_COOKIE_SECRET=COOKIE_SECRET
BC_AUTH_COOKIE_DOMAIN=localhost
BC_JWT_SECRET=JWT_SECRET
BC_JWT_EXPIRE_AT=86400
BC_TOKEN_EXPIRE_AT=86400
BC_SMTP_HOST=smtp.sendgrid.net
BC_SMTP_PORT=587
BC_SMTP_USER=apikey
BC_SMTP_PASS=PASSWORD
BC_SMTP_FROM=admin@bookcars.com
BC_CDN_USERS=/var/www/cdn/bookcars/users
BC_CDN_TEMP_USERS=/var/www/cdn/bookcars/temp/users
BC_CDN_CARS=/var/www/cdn/bookcars/cars
BC_CDN_TEMP_CARS=/var/www/cdn/bookcars/temp/cars
BC_DEFAULT_LANGUAGE=en
BC_BACKEND_HOST=http://localhost:3001/
BC_FRONTEND_HOST=http://localhost/
BC_MINIMUM_AGE=21
BC_EXPO_ACCESS_TOKEN=EXPO_ACCESS_TOKEN
BC_STRIPE_SECRET_KEY=STRIPE_SECRET_KEY
BC_ADMIN_EMAIL=admin@bookcars.ma
BC_RECAPTCHA_SECRET=RECAPTCHA_SECRET

Set the following options:

BC_DB_URI=mongodb://admin:PASSWORD@127.0.0.1:27017/bookcars?authSource=admin&appName=bookcars
BC_COOKIE_SECRET=COOKIE_SECRET
BC_AUTH_COOKIE_DOMAIN=localhost
BC_JWT_SECRET=JWT_SECRET
BC_SMTP_HOST=smtp.sendgrid.net
BC_SMTP_PORT=587
BC_SMTP_USER=apikey
BC_SMTP_PASS=PASSWORD
BC_SMTP_FROM=admin@bookcars.com
BC_BACKEND_HOST=http://localhost:3001/
BC_FRONTEND_HOST=http://localhost/
BC_STRIPE_SECRET_KEY=STRIPE_SECRET_KEY
BC_RECAPTCHA_SECRET=RECAPTCHA_SECRET

If you want to use MongoDB Atlas, put you MongoDB Atlas URI in BC_DB_URI otherwise replace PASSWORD in BC_DB_URI with your MongoDB password. Replace JWT_SECRET with a secret token. Finally, set the SMTP options. SMTP options are necessary for sign up. You can use sendgrid or any other transactional email provider.

If you choose sendgrid, create an account on sendgrid.com, login and go to the dashboard. On the left panel, click on Email API, then on Integration Guide. Then, choose SMTP Relay and follow the steps. You will be prompted to create an API Key. Once you create the API Key and verify the smtp relay, copy the API key in BC_SMTP_PASS in ./api/.env. Sendgrid's free plan allows to send up to 100 emails/day. If you need to send more than 100 emails/day, switch to a paid plan or choose another transactional email provider.

COOKIE_SECRET and JWT_SECRET should at least be 32 characters long, but the longer the better. You can use an online password generator and set the password length to 32 or longer.

The following settings are very important and if they are not set properly, authentication won't work:

BC_AUTH_COOKIE_DOMAIN=localhost
BC_BACKEND_HOST=http://localhost:3001/
BC_FRONTEND_HOST=http://localhost/

Replace localhost with an IP or FQDN. That is if you access the backend from http://<FQDN>:3001/. BC_BACKEND_HOST should be http://<FQDN>:3001/. The same goes for BC_FRONTEND_HOST. And BC_AUTH_COOKIE_DOMAIN should be FQDN.

If you want to enable push notifications in the mobile app, follow these instructions and set the following option:

BC_EXPO_ACCESS_TOKEN=EXPO_ACCESS_TOKEN

If you want to enable stripe payment gateway, sign up for a stripe account, fill the forms and save the publishable key and the secret key from stripe dashboard. Then, set the secret key in the following option in api/.env:

BC_STRIPE_SECRET_KEY=STRIPE_SECRET_KEY

Don't expose stripe secret key on a website or embed it in a mobile application. It must be secret and stored securely in the server-side.

In stripe, all accounts have a total of four API keys by default-two for test mode and two for live mode:

  • Test mode secret key: Use this key to authenticate requests on your server when in test mode. By default, you can use this key to perform any API request without restriction.
  • Test mode publishable key: Use this key for testing purposes in your web or mobile app’s client-side code.
  • Live mode secret key: Use this key to authenticate requests on your server when in live mode. By default, you can use this key to perform any API request without restriction.
  • Live mode publishable key: Use this key, when you’re ready to launch your app, in your web or mobile app’s client-side code.

Use only your test API keys for testing. This ensures that you don't accidentally modify your live customers or charges.

If you want to enable HTTPS, you have to set the following options:

BC_HTTPS=true
BC_PRIVATE_KEY=/etc/ssl/bookcars.com.key
BC_CERTIFICATE=/etc/ssl/bookcars.com.crt

If you want to use Google reCAPTCHA on the frontend, you need to set:

BC_RECAPTCHA_SECRET=RECAPTCHA_SECRET
  1. Create /opt/bookcars/backend/.env file with the following content:
VITE_PORT=3001
VITE_NODE_ENV=production
VITE_BC_API_HOST=http://localhost:4002
VITE_BC_DEFAULT_LANGUAGE=en
VITE_BC_PAGE_SIZE=30
VITE_BC_CARS_PAGE_SIZE=15
VITE_BC_BOOKINGS_PAGE_SIZE=20
VITE_BC_BOOKINGS_MOBILE_PAGE_SIZE=10
VITE_BC_CDN_USERS=http://localhost/cdn/bookcars/users
VITE_BC_CDN_TEMP_USERS=http://localhost/cdn/bookcars/temp/users
VITE_BC_CDN_CARS=http://localhost/cdn/bookcars/cars
VITE_BC_CDN_TEMP_CARS=http://localhost/cdn/bookcars/temp/cars
VITE_BC_SUPPLIER_IMAGE_WIDTH=60
VITE_BC_SUPPLIER_IMAGE_HEIGHT=30
VITE_BC_CAR_IMAGE_WIDTH=300
VITE_BC_CAR_IMAGE_HEIGHT=200
VITE_BC_MINIMUM_AGE=21
VITE_BC_PAGINATION_MODE=classic
VITE_BC_CURRENCY=$
VITE_BC_DEPOSIT_FILTER_VALUE_1=250
VITE_BC_DEPOSIT_FILTER_VALUE_2=500
VITE_BC_DEPOSIT_FILTER_VALUE_3=750

Set the following options:

VITE_BC_API_HOST=http://localhost:4002
VITE_BC_CDN_USERS=http://localhost/cdn/bookcars/users
VITE_BC_CDN_TEMP_USERS=http://localhost/cdn/bookcars/temp/users
VITE_BC_CDN_CARS=http://localhost/cdn/bookcars/cars
VITE_BC_CDN_TEMP_CARS=http://localhost/cdn/bookcars/temp/cars

Replace localhost with an IP, hostname or FQDN.

VITE_BC_PAGINATION_MODE: You can choose between classic or infinite_scroll. This option defaults to classic. If you choose classic, you will get a classic pagination with next and previous buttons on desktop and infinite scroll on mobile. If you choose infinite_scroll, you will get infinite scroll on desktop and mobile.

  1. Create /opt/bookcars/frontend/.env file with the following content:
VITE_PORT=3002
VITE_NODE_ENV=production
VITE_BC_API_HOST=http://localhost:4002
VITE_BC_RECAPTCHA_ENABLED=false
VITE_BC_RECAPTCHA_SITE_KEY=GOOGLE_RECAPTCHA_SITE_KEY
VITE_BC_DEFAULT_LANGUAGE=en
VITE_BC_PAGE_SIZE=30
VITE_BC_CARS_PAGE_SIZE=15
VITE_BC_BOOKINGS_PAGE_SIZE=20
VITE_BC_BOOKINGS_MOBILE_PAGE_SIZE=10
VITE_BC_CDN_USERS=http://localhost/cdn/bookcars/users
VITE_BC_CDN_CARS=http://localhost/cdn/bookcars/cars
VITE_BC_SUPPLIER_IMAGE_WIDTH=60
VITE_BC_SUPPLIER_IMAGE_HEIGHT=30
VITE_BC_CAR_IMAGE_WIDTH=300
VITE_BC_CAR_IMAGE_HEIGHT=200
VITE_BC_MINIMUM_AGE=21
VITE_BC_PAGINATION_MODE=classic
VITE_BC_STRIPE_PUBLISHABLE_KEY=STRIPE_PUBLISHABLE_KEY
VITE_BC_STRIPE_CURRENCY_CODE=USD
VITE_BC_SET_LANGUAGE_FROM_IP=false
VITE_BC_GOOGLE_ANALYTICS_ENABLED=false
VITE_BC_GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX
VITE_BC_CONTACT_EMAIL=info@bookcars.ma
VITE_BC_DEPOSIT_FILTER_VALUE_1=250
VITE_BC_DEPOSIT_FILTER_VALUE_2=500
VITE_BC_DEPOSIT_FILTER_VALUE_3=750

Set the following options:

VITE_BC_API_HOST=http://localhost:4002
VITE_BC_CDN_USERS=http://localhost/cdn/bookcars/users
VITE_BC_CDN_CARS=http://localhost/cdn/bookcars/cars
VITE_BC_STRIPE_PUBLISHABLE_KEY=STRIPE_PUBLISHABLE_KEY
VITE_BC_STRIPE_CURRENCY_CODE=USD
VITE_BC_CONTACT_EMAIL=info@bookcars.ma

Replace localhost with an IP, hostname or FQDN.

If you want to enable stripe payment gateway, set stripe publishable key in VITE_BC_STRIPE_PUBLISHABLE_KEY. You can retrieve it from stripe dashboard.

VITE_BC_STRIPE_CURRENCY_CODE is the three-letter ISO 4217 alphabetic currency code, e.g. "USD" or "EUR". Required for Stripe payments. Must be a supported currency: https://docs.stripe.com/currencies

reCAPTCHA is by default disabled. If you want to enable it, you have to set VITE_BC_RECAPTCHA_ENABLED to true and VITE_BC_RECAPTCHA_SITE_KEY to Google reCAPTCHA site key.

Emails sent from the contact form are sent to:

VITE_BC_CONTACT_EMAIL=info@bookcars.ma

If you want to enable Google Analytics, set the following options:

VITE_BC_GOOGLE_ANALYTICS_ENABLED=true
VITE_BC_GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX
  1. If you want to use the mobile app, create mobile/.env file with the following content:
BC_API_HOST=https://bookcars.ma:4002
BC_DEFAULT_LANGUAGE=en
BC_PAGE_SIZE=20
BC_CARS_PAGE_SIZE=8
BC_BOOKINGS_PAGE_SIZE=8
BC_CDN_USERS=https://bookcars.ma/cdn/bookcars/users
BC_CDN_CARS=https://bookcars.ma/cdn/bookcars/cars
BC_COMAPNY_IMAGE_WIDTH=60
BC_COMAPNY_IMAGE_HEIGHT=30
BC_CAR_IMAGE_WIDTH=300
BC_CAR_IMAGE_HEIGHT=200
BC_MINIMUM_AGE=21
BC_STRIPE_PUBLISHABLE_KEY=STRIPE_PUBLISHABLE_KEY
BC_STRIPE_MERCHANT_IDENTIFIER=MERCHANT_IDENTIFIER
BC_STRIPE_COUNTRY_CODE=US
BC_STRIPE_CURRENCY_CODE=USD
BC_CURRENCY=$

Set the following options:

BC_API_HOST=https://bookcars.ma:4002
BC_CDN_USERS=https://bookcars.ma/cdn/bookcars/users
BC_CDN_CARS=https://bookcars.ma/cdn/bookcars/cars
BC_STRIPE_PUBLISHABLE_KEY=STRIPE_PUBLISHABLE_KEY
BC_STRIPE_MERCHANT_IDENTIFIER=MERCHANT_IDENTIFIER
BC_STRIPE_COUNTRY_CODE=US
BC_STRIPE_CURRENCY_CODE=USD
BC_CURRENCY=$

Replace https://bookcars.com with an IP, hostname or FQDN.

If you want to enable stripe payment gateway, set stripe publishable key in BC_STRIPE_PUBLISHABLE_KEY. You can retrieve it from stripe dashboard.

BC_STRIPE_MERCHANT_IDENTIFIER is the merchant identifier you registered with Apple for use with Apple Pay.

BC_STRIPE_COUNTRY_CODE is the two-letter ISO 3166 code of the country of your business, e.g. "US". Required for Stripe payments.

BC_STRIPE_CURRENCY_CODE is the three-letter ISO 4217 alphabetic currency code, e.g. "USD" or "EUR". Required for Stripe payments. Must be a supported currency: https://docs.stripe.com/currencies

  1. Configure NGINX:
sudo nano /etc/nginx/sites-available/default

Change the configuration as follows for the frontend:

server {
    root /var/www/bookcars/frontend;
    #listen 443 http2 ssl default_server;
    listen 80 default_server;
    server_name _;
    
    #ssl_certificate_key /etc/ssl/bookcars.com.key;
    #ssl_certificate /etc/ssl/bookcars.com.pem;

    access_log /var/log/nginx/bookcars.frontend.access.log;
    error_log /var/log/nginx/bookcars.frontend.error.log;

    index index.html;

    location / {
        # First attempt to serve request as file, then as directory,
        # then as index.html, then fall back to displaying a 404.
        try_files $uri $uri/ /index.html =404;
    }

    location /cdn {
      alias /var/www/cdn;
    }
}

If you want to enable SSL, uncomment and set these lines:

#listen 443 http2 ssl default_server;
#ssl_certificate_key /etc/ssl/bookcars.com.key;
#ssl_certificate /etc/ssl/bookcars.com.pem;

Add the following configuration for the backend:

server {
    root /var/www/bookcars/backend;
    #listen 3001 http2 ssl default_server;
    listen 3001 default_server;
    server_name _;

    #ssl_certificate_key /etc/ssl/bookcars.com.key;
    #ssl_certificate /etc/ssl/bookcars.com.pem;

    #error_page 497 301 =307 https://$host:$server_port$request_uri;

    access_log /var/log/nginx/bookcars.backend.access.log;
    error_log /var/log/nginx/bookcars.backend.error.log;

    index index.html;

    location / {
        # First attempt to serve request as file, then as directory,
        # then as index.html, then fall back to displaying a 404.
        try_files $uri $uri/ /index.html =404;
    }
}

Create /var/www/cdn/bookcars folder and add full access permissions to the user who is running BookCars API on /var/www/cdn/bookcars.

If you want to enable SSL, uncomment and set these lines:

#listen 3001 http2 ssl default_server;
#ssl_certificate_key /etc/ssl/bookcars.com.key;
#ssl_certificate /etc/ssl/bookcars.com.pem;
#error_page 497 301 =307 https://$host:$server_port$request_uri;

Then, check NGINX configuration and restart NGINX service:

sudo nginx -t
sudo systemctl restart nginx.service
sudo systemctl status nginx.service
  1. enable firewall and open BookCars ports:
sudo ufw enable
sudo ufw allow 4002/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 3001/tcp
sudo ufw allow 27017/tcp
  1. Start bookcars service:
cd /opt/bookcars/api
npm install --omit=dev
sudo systemctl start bookcars.service

Make sure that bookcars service is running with the following command:

sudo systemctl status bookcars.service

Make sure that the database connection is established by checking the logs with the following command:

tail -f /var/log/bookcars.log

Or this one:

sudo journalctl -xfu bookcars.service

Or by opening this file:

tail -f /opt/bookcars/api/logs/all.log

Error logs are written in:

tail -f /opt/bookcars/api/logs/error.log
  1. Deploy BookCars:
bc-deploy all

BookCars backend is accessible on port 3001 and the frontend is accessible on port 80.

If you want to install BookCars on a VPS with small amount of RAM, you may encounter memory issues while running bc-deploy all. In that case, you need to proceed as follow:

  • Run bc-deploy api to install and run the API.
  • On your desktop PC, set up frontend/.env as described previously, then run the following commands from frontend folder:
npm install
npm run build
  • Copy the content of frontend/build from your desktop PC to /var/www/bookcars/frontend on your VPS.
  • On your desktop PC, set up backend/.env as described previously, then run the following commands from backend folder:
npm install
npm run build
  • Copy the content of backend/build from your desktop PC to /var/www/bookcars/backend on your VPS.
  • Restart NGINX:
sudo rm -rf /var/cache/nginx
sudo systemctl restart nginx
sudo systemctl status nginx
  1. If you don't want to use the demo database, create an admin user by navigating to hostname:3001/sign-up

  2. Open backend/src/App.tsx and comment these lines to secure the backend:

const SignUp = lazy(() => import('./pages/SignUp'))
<Route exact path='/sign-up' element={<Signup />} />

And run backend deployment again:

bc-deploy backend

If you want to deploy the frontend only, run the following command:

bc-deploy frontend

If you want to deploy the api only, run the following command:

bc-deploy api

If you want to deploy the api, the backend and the frontend, run the following command:

bc-deploy all

To change the currency, follow these instructions.