How to Install Bagisto on NixOS Latest

Bagisto is an open-source e-commerce platform that offers multi-tenant support, easy integration with several payment gateways, and rich features to build a fully functional online store. If you are looking to build an e-commerce website, Bagisto could be an excellent choice. In this tutorial, we will learn how to install Bagisto on NixOS Latest.

Prerequisites

Before we proceed with the installation, ensure that you have the following:

Step 1 — Installing Dependencies

First, update your system's package list, as follows:

sudo nixos-rebuild switch

Then, install composer, PHP, and other dependencies as follows:

sudo apt-get update
sudo apt-get install composer curl php php-zip php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-cli php-dev libmcrypt-dev mysql-client nginx supervisor

The above command will install the latest version of PHP, which includes all the required PHP extensions and other necessary software on your system.

Step 2 — Configuring Web Server

Next, we need to configure the web server to serve Bagisto. Here we will use the Nginx web server. To install Nginx, run the following command:

sudo apt-get install nginx

Once the installation is complete, you can create a new server block in the Nginx configuration file /etc/nginx/sites-available/bagisto.conf by running the following command.

sudo nano /etc/nginx/sites-available/bagisto.conf

Then paste the following code and update the domain name:

server {
        listen 80;
        server_name yourdomain.com;
        root /var/www/bagisto/public;

        index index.php;

        # ensure that a 404 error when users request real files
        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        # For PHP Files
        location ~ \.php$ {

                # Pass PHP scripts to FastCGI server
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;

                include fastcgi_params;

                # Path to watch for PHP includes
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
        }
}

Now enable the new server block by creating a symbolic link in the sites-enabled directory with the following command:

sudo ln -s /etc/nginx/sites-available/bagisto.conf /etc/nginx/sites-enabled/

Check the configuration and restart the Nginx webserver using the following commands:

sudo nginx -t
sudo systemctl restart nginx

Step 3 — Configuring Database

Now we need to create a new database and user for Bagisto. Run the following command to access the MySQL shell:

sudo mysql

Then create a new database and a user with the following commands. Replace [yourpassword] and [bagistodbname] with your desired values.

CREATE DATABASE [bagistodbname];
CREATE USER 'bagistouser'@'localhost' IDENTIFIED BY '[yourpassword]';
GRANT ALL PRIVILEGES ON [bagistodbname].* TO 'bagistouser'@'localhost';
FLUSH PRIVILEGES;
exit

Step 4 — Installing Bagisto

We are ready now to install Bagisto. Run the following commands in the terminal to download and install the latest version of Bagisto.

cd /var/www/
sudo composer create-project bagisto/bagisto

The above commands will download and install Bagisto to /var/www/bagisto. Now we need to copy the configuration file.

sudo cp /var/www/bagisto/.env.example /var/www/bagisto/.env

Next, we need to update the .env file with our database details. Open the .env file using nano or vim:

sudo nano /var/www/bagisto/.env

Update the following variables in the .env file with your database credentials:

DB_HOST=localhost
DB_DATABASE=[bagistodbname]
DB_USERNAME=bagistouser
DB_PASSWORD=[yourpassword]

Finally, we need to set the correct permissions of the storage and bootstrap cache directories.

sudo chmod -R 777 /var/www/bagisto/storage/
sudo chmod -R 777 /var/www/bagisto/bootstrap/cache/

Step 5 — Configuring Supervisor

We need to set up a supervisor to manage the queue. Run the following command to install the supervisor:

sudo apt-get install supervisor

Now create a new configuration file /etc/supervisor/conf.d/bagisto-worker.conf and add the following:

[program:bagisto-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/bagisto/artisan queue:work --once --queue=default
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/bagisto/storage/logs/supervisor.log

Update the user and the stdout_logfile fields with the correct values. Save and close the file.

Step 6 — Restarting Supervisor

Restart supervisor and start the Bagisto worker process with the following commands:

sudo systemctl daemon-reload
sudo systemctl start supervisor.service
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start bagisto-worker:*

Conclusion

Congratulations, you have successfully installed Bagisto on NixOS Latest. Now you can access the Bagisto e-commerce platform by visiting your configured IP address or domain name in your web browser.

If you want to self-host in an easy, hands free way, need an external IP address, or simply want your data in your own hands, give IPv6.rs a try!