medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.88k stars 2.59k forks source link

Get-Started Help #10022

Closed princessfrombabylon99 closed 1 day ago

princessfrombabylon99 commented 2 days ago

Package.json file

{
  "name": "medusa-starter-default",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "latest",
    "@medusajs/cli": "latest",
    "@medusajs/framework": "latest",
    "@medusajs/medusa": "latest",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "pg": "^8.13.0"
  },
  "devDependencies": {
    "@medusajs/test-utils": "latest",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

20.18.0

Database and its version

psql (PostgreSQL) 16.4 (Ubuntu 16.4-0ubuntu0.24.04.2)

Operating system name and version

Ubuntu 24.04

Browser name

Chrome and Safari

What happended?

Below are the commands Created a new ec2 instance Updated the repo sudo apt update && sudo apt upgrade -y

Corrected the time.zone

sudo timedatectl set-timezone "Asia/Kolkata"

Reboot

sudo reboot

Installed Nginx

sudo apt install nginx

Installed Node 20

sudo apt install curl && \
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash && \
source ~/.profile && \
nvm install 20

NPM

sudo apt install npm Installed Yarn

sudo npm install --global yarn
yarn -v

Configured Yarn (Not sure , if I did this correct, I just found this on internet)

yarn config set prefix ~/.yarn
nano ~/.bashrc

Added the following line:

export PATH="$PATH:yarn global bin"`

source ~/.bashrc

Installed Postgres 16

sudo apt -y install postgresql
sudo systemctl enable postgresql

Created user, db named 'jungle' and granted the user all PRIVILEGES on db jungle

Install Redis

curl -fsSL https://packages.redis.io/gpg | 
  sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" 
  | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt update
sudo apt install redis
sudo systemctl enable redis-server
sudo service redis-server start
redis-cli ping

ubuntu@my-ip $ npx create-medusa-app@latest

What's the name of your project? my-store
? Would you like to create the Next.js storefront? You can also create it later Yes
? Enter your Postgres username tropicana
? Enter your Postgres password [hidden]
? Enter your Postgres user's database name jungle
🚀 Starting project setup, this may take a few minutes.
✔ Created project directory
✔ Installed Next.js Starter successfully in the my-store-storefront directory.
**An error occurred while trying to create your database: error: permission denied to create database**

Today , I was logged in as ubuntu user not root, I used ChatGPT and it gave the following solution

It looks like PostgreSQL is still attempting to authenticate the user tropicana using the peer authentication method, even after you've changed the settings. Let's walk through some additional steps to resolve this issue:

Step 1: Confirm Authentication Method in pg_hba.conf
Let's make sure you've correctly changed the pg_hba.conf file to allow password-based authentication (md5) for the tropicana user.

Open the pg_hba.conf file again:
bash
Copy code
`sudo nano /etc/postgresql/{your_version}/main/pg_hba.conf`
Ensure that the following line exists (or is modified) for local connections, changing peer to md5:
plaintext
Copy code
local   all             all                                     md5
This will allow any PostgreSQL user to authenticate with a password over local connections.

Save and close the file (in nano, press CTRL + X, then Y to confirm, and Enter).

So , I changed it to md5 and ran one more command ALTER USER tropicana WITH CREATEDB;

This time the Medusa got installed. I had previously tried it install it as root user , however, it never threw such errors.

I got a message my server is running at localhost:9000

I used Letsencrypt and also NGINX configuration is below ( modified the server-names)

Below is the nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
   # You can adjust the number of worker processes based on the number of CPU cores you have
   worker_connections 768;
}

http {
   # Basic Settings
   sendfile on;
   tcp_nopush on;
   types_hash_max_size 2048;
   server_tokens off;

   include /etc/nginx/mime.types;
   default_type application/octet-stream;

   # SSL Settings
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
   ssl_prefer_server_ciphers on;

   # Logging Settings
   access_log /var/log/nginx/access.log;
   error_log /var/log/nginx/error.log;

   # Gzip Settings
   gzip off;

   server {

      listen [::]:443 ssl;
      listen 443 ssl;

      server_name my-server-name-goes here;

      ssl_certificate /etc/letsencrypt/live/my-domainname-goes-here/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/my-domain-name-goes-here/privkey.pem;

      include /etc/letsencrypt/options-ssl-nginx.conf;
      ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

      location / {
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Host $host;
         proxy_pass http://127.0.0.1:9000;
         proxy_http_version 1.1;
         proxy_cache_bypass $http_upgrade;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection "upgrade";
      }
   }
}

I went on my URL For front-end , I got

GET / Which is okay, because it is giving me a hint that something needs to be configured.

I went on to log in my admin panel URL/app/ image

I tried creating another user , but no luck.

My one more question is , If I created a database named 'jungle' , why did Medusa create another datebase during installation and Medusa named that database as medusa-my-store , Shouldn't it use the database that I created and assigned to it?

Now , I am stuck at point. I have no idea , no clue what to do next. I checked YouTube, reddit, GitHub, Stockoverflow and all forums, however, as you know , very limited documentation is available about Medusa.

Expected behavior

Admin Panel with no console errors

Actual behavior

Console Errors , unable to connect.

Link to reproduction repo

https://github.com/medusajs/medusa

princessfrombabylon99 commented 2 days ago

@shahednasser Kindly help

princessfrombabylon99 commented 2 days ago

Console Log image image image

shahednasser commented 2 days ago

Thanks for opening the issue! Looking at your instructions, doesn't seem like you created a user.

Can you try running the following command in your deployed Medusa application's directory:

npx medusa user -e admin@medusajs.com -p supersecret

You can replace admin@medusajs.com and supersecret with the email and password you want.

Then, try to login again.

princessfrombabylon99 commented 2 days ago

Thanks for opening the issue! Looking at your instructions, doesn't seem like you created a user.

Can you try running the following command in your deployed Medusa application's directory:

npx medusa user -e admin@medusajs.com -p supersecret

You can replace admin@medusajs.com and supersecret with the email and password you want.

Then, try to login again.

image

Still get this image

Do I need to make any changes to .env? FYI Apart from this the installation wizard did show 'Creating user' message

shahednasser commented 2 days ago

I can't see in the first screenshot, did it say the user was created successfully? And did you make sure to use the same credentials as in the command when logging in?

princessfrombabylon99 commented 2 days ago

Below are my .env and env.local

.env

MEDUSA_ADMIN_ONBOARDING_TYPE=nextjs
STORE_CORS=http://localhost:8000,https://docs.medusajs.com,
ADMIN_CORS=http://localhost:5173,http://localhost:9000,https://docs.medusajs.com,
AUTH_CORS=http://localhost:5173,http://localhost:9000,https://docs.medusajs.com,
REDIS_URL=redis://localhost:6379
JWT_SECRET=supersecret
COOKIE_SECRET=supersecret
DB_NAME=medusa-my-store
DATABASE_URL=postgres://tropicana:*******@localhost/$DB_NAME
MEDUSA_ADMIN_ONBOARDING_NEXTJS_DIRECTORY=my-store-storefront

env.local

# Your Medusa backend, should be updated to where you are hosting your server. Remember to update CORS settings for you>
NEXT_PUBLIC_MEDUSA_BACKEND_URL=http://localhost:9000

# Your publishable key that can be attached to sales channels. See - https://docs.medusajs.com/development/publishable->
NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=pk_7ea867cb63dedKEY-GOES-HERE364ed801ff912255a2b9b641daeda0c7

# Your store URL, should be updated to where you are hosting your storefront.
NEXT_PUBLIC_BASE_URL=http://localhost:8000

# Your preferred default region. When middleware cannot determine the user region from the "x-vercel-country" header, t>
NEXT_PUBLIC_DEFAULT_REGION=us

# Your Stripe public key. See – https://docs.medusajs.com/add-plugins/stripe
NEXT_PUBLIC_STRIPE_KEY=

# Your PayPal Client ID. See – https://docs.medusajs.com/add-plugins/paypal
NEXT_PUBLIC_PAYPAL_CLIENT_ID=

# Your MeiliSearch / Algolia keys. See – https://docs.medusajs.com/add-plugins/meilisearch or https://docs.medusajs.com>
NEXT_PUBLIC_FEATURE_SEARCH_ENABLED=false
NEXT_PUBLIC_SEARCH_APP_ID=
NEXT_PUBLIC_SEARCH_ENDPOINT=http://127.0.0.1:7700
NEXT_PUBLIC_SEARCH_API_KEY=
NEXT_PUBLIC_INDEX_NAME=products

# Your Next.js revalidation secret. See – https://nextjs.org/docs/app/building-your-application/data-fetching/fetching->
REVALIDATE_SECRET=supersecret
princessfrombabylon99 commented 2 days ago

I can't see in the first screenshot, did it say the user was created successfully? And did you make sure to use the same credentials as in the command when logging in?

First screenshot shows the following information

ubuntu@ip-my-ip:~/my-store$ npx medusa user -e admin@medusajs.com -p supersecret
redisUrl not found. A fake redis instance will be used.
warn:    Local Event Bus installed. This is not recommended for production.
info:    Locking module: Using "in-memory" as default.
info:    No job to load from /home/ubuntu/my-store/node_modules/@medusajs/medusa/dist/jobs. skipped.
shahednasser commented 2 days ago

Why are you using .env files in production? You should use system environment variables instead. Also, it seems like you don't have NODE_ENV=production.

I actually have a PR for a general deployment guide here that should be available in the docs soon. Might be useful to follow for your deployment.

But I don't believe that's the issue. If you created a user with email admin@medusajs.com and password supersecret, you should be able to login with it. Make sure you're using the correct credentials or maybe try creating a different user with a different email and password, then try logging in with that user.

princessfrombabylon99 commented 2 days ago

Why are you using .env files in production? You should use system environment variables instead. Also, it seems like you don't have NODE_ENV=production.

I actually have a PR for a general deployment guide here that should be available in the docs soon. Might be useful to follow for your deployment.

But I don't believe that's the issue. If you created a user with email admin@medusajs.com and password supersecret, you should be able to login with it. Make sure you're using the correct credentials or maybe try creating a different user with a different email and password, then try logging in with that user.

Yes, tried creating different users also couple of times. Well this is the issue . I am not using any external DB or Redis , Installed it as we install on a local ubuntu machine.

princessfrombabylon99 commented 2 days ago

system environment variables

I understand what you mentioned about system environment variables. Do you have any documentation for AWS? But , I am still thinking, even if I don't use system environment variables and keep the variables with source code, I should be able to at-least login and explore, if it is working localhost , why not on live server?

princessfrombabylon99 commented 1 day ago

@shahednasser I am going to install Coolify on my AWS instance and see how things go; ideally, it should work, as Coolify is an alternative to Vercel.

Meanwhile, I'm still wondering why the Medusa installation wizard asked me for a database name but didn’t use the database I created. Instead, it created another one on its own. This question is still lingering in my mind.

An EC2 instance is essentially the same as an Ubuntu machine, so I should have been able to log in to the admin panel. Changing the CORS settings to allow access from a specific IP might have fixed the issue, but I’m giving up on that for now. Let’s move on to Coolify!

shahednasser commented 1 day ago

Meanwhile, I'm still wondering why the Medusa installation wizard asked me for a database name but didn’t use the database I created. Instead, it created another one on its own. This question is still lingering in my mind.

create-medusa-app creates a new database every time you run it. You can change .env to point to the database you created and run npx medusa db:setup. You can also pass to create-medusa-app a --db-url option with its value the connection URL to your database.

I am going to install Coolify on my AWS instance and see how things go; ideally, it should work, as Coolify is an alternative to Vercel.

That's unrelated really. The Medusa server can't be deployed to vercel anyway.

Your application seems to be connecting fine to the database, and has the correct configuration. The only possible issue I can think of is that you're creating the user in a different application that's connected to a different database or something similar.

princessfrombabylon99 commented 1 day ago

Meanwhile, I'm still wondering why the Medusa installation wizard asked me for a database name but didn’t use the database I created. Instead, it created another one on its own. This question is still lingering in my mind.

create-medusa-app creates a new database every time you run it. You can change .env to point to the database you created and run npx medusa db:setup. You can also pass to create-medusa-app a --db-url option with its value the connection URL to your database.

I am going to install Coolify on my AWS instance and see how things go; ideally, it should work, as Coolify is an alternative to Vercel.

That's unrelated really. The Medusa server can't be deployed to vercel anyway.

Could I install Medusa server using Coolify? I understand vercel is used to install front-end or back-end. I am again little confused now🥺. I want to use Medusa within AWS. If I use coolify, could you please tell me how can I go about installing medusa server? *I am going through learning , please don't mind if I ask such basic questions. I so want to use Medusa in production for my start-up.

Your application seems to be connecting fine to the database, and has the correct configuration. The only possible issue I can think of is that you're creating the user in a different application that's connected to a different database or something similar. Maybe you're right.

shahednasser commented 1 day ago

If you need step-by-step help, then i recommend Railway as we already have a guide for it:

https://docs.medusajs.com/resources/deployment/medusa-application/railway

Or you can use the railway guide as a blue print for your deployment.

princessfrombabylon99 commented 1 day ago

@shahednasser If I break it in a laymans language. Medusa - is a server, that can be installed on Ubuntu server. admin - can be installed using vercel, coolify etc. front-end - can be installed using vercel , coolify etc.

Then I need to configure these 3 things to talk to each other by configuring , system variables , db urls, CORS etc.

shahednasser commented 1 day ago

That's true. Although you can deploy the admin with the backend. The storefront needs to always be deployed separately.

princessfrombabylon99 commented 1 day ago

That's true. Although you can deploy the admin with the backend. The storefront needs to always be deployed separately.

When you say backend , you mean medusa server+admin panel? Since front-end is in NextJs , it needs a platform to deploy.

Okay , I think , I got it now!

princessfrombabylon99 commented 1 day ago

@shahednasser One of my colleague tried it on a local Ubuntu machine, he was able to login. image However, when we checked db, it shows that Medusa created another db and did not use the one that we provided during installation wizard . Medusa basically used Posgres user with 'CREATE' privileges and created a fresh db and overlooked the one that was given to it. Please check the screenshots below. image

shahednasser commented 1 day ago

it shows that Medusa created another db and did not use the one that we provided during installation wizard

Can you share how you provided the database? Can you share the command you used?

princessfrombabylon99 commented 1 day ago

@shahednasser During the Installation , on below screen.

What's the name of your project? my-store ? Would you like to create the Next.js storefront? You can also create it later Yes ? Enter your Postgres username tropicana ? Enter your Postgres password [hidden] ? Enter your Postgres user's database name jungle 🚀 Starting project setup, this may take a few minutes. ✔ Created project directory ✔ Installed Next.js Starter successfully in the my-store-storefront directory.

shahednasser commented 1 day ago

So I think there's a misunderstanding here. The "Enter your Postgres user's database name" is not the database name that Medusa uses, it's the name of the PostgreSQL user's database (learn more here).

If you want to specify a database to use, provide a database connection URL with --db-url like this:

npx create-medusa-app@latest --db-url "postgres://user:password@localhost:5432/my-database"

Of course change the URL based on your database. Then, if the command can connect to the database, it will use it instead of creating another one. otherwise the installation will fail

princessfrombabylon99 commented 1 day ago

@shahednasser Thank you.

shahednasser commented 1 day ago

Let me know if it works!

princessfrombabylon99 commented 1 day ago

@shahednasser I’ll keep you posted. I’m reading more about Medusa now, as it sounds more interesting. Without your help, I wouldn’t have been able to figure out where I was going wrong. As entrepreneurs, we’re used to opens-source monolithic apps, where we run a few CLI commands and get things working, as the frontend and backend remain tightly integrated. This is the first time we’re using headless ecommerce, and it looks more modern, SEO-friendly, and promising. I’m reading your docs now to understand most of it.

shahednasser commented 1 day ago

That's great to hear! Then as the original issue is resolved, I'll close this issue. Feel free to open another if you face other problems!