R
Railway11mo ago
Dadz

Deploying Django Back End & React Front End

Hello I'm having trouble finding information about deploying Django REST backend and React front end on Railway. Could someone shed some light on this for me? Thank you
203 Replies
Percy
Percy11mo ago
Project ID: N/A
Dadz
DadzOP11mo ago
N/A
Brody
Brody11mo ago
they would need to be deployed as separate railway services, one service for the frontend, one service for the backend. how is your project structured right now? a lot of developers try to have their backend serve the frontend but that's just not the right way to go about it when hosting on railway and leads to a lot of difficulties when trying to do it that way
Dadz
DadzOP11mo ago
Thank you for the quick answer Brody. Yes i was thinking about doing that so i wouldn't have to deploy and manage them separately, but i know that it can cause slower loading times or cors problems. I know there's a way to do it with Heroku for DJANGO REST and Netlify for React but as i already have my db and one other project hosted on railway, i would like to host everything on railway
Brody
Brody11mo ago
that's a perfectly valid reason, lots of people host their frontends on railway and when done right it works pretty nicely. think you could share your repo? so I can get an understanding of your projects structure in order to best help you!
Dadz
DadzOP11mo ago
Yes for sure, i send you a DM
Brody
Brody11mo ago
I saw the link to the repo but we shall continue the conversation here
Dadz
DadzOP11mo ago
Ok
Brody
Brody11mo ago
and it looks like you have your project structured in such a way that it will make it very easy to deploy to railway with some very minor tweaks!
Dadz
DadzOP11mo ago
Nice !
Brody
Brody11mo ago
you are just missing a few things, like .gitignore files for the front and backend, a requirements.txt for the backend. and then you want to delete the things that would normally get ignored, like the pycache folders you can use this site to create your .gitignore files https://www.toptal.com/developers/gitignore/ so let me know when that's all done!
Dadz
DadzOP11mo ago
Ok Will i also need the procfile and the runtime ?
Brody
Brody11mo ago
a Procfile is a heroku thing, and while it is semi supported on railway, there are better alternatives that are railway specific, we will get to that soon enough though. runtime.txt is only needed if you want railway to use a different than default python version, the default currently being 3.8
Dadz
DadzOP11mo ago
Alright ! I have node_modules in my react gitignore, i did my requirements.txt for django rest but i actually don't know what to include in the gitignore of my backend, settings.py ?
Brody
Brody11mo ago
oh no you definitely don't want to ignore settings.py just type in stuff like python and django into that site I gave you and use the .gitignore it creates for you
Dadz
DadzOP11mo ago
oh ok i just copy/paste
Brody
Brody11mo ago
yep
Dadz
DadzOP11mo ago
ok all good juste have to commit What's next ?
Brody
Brody11mo ago
did you commit the changes?
Dadz
DadzOP11mo ago
yep
Brody
Brody11mo ago
mind specifying all technologies involved when using the site I linked, for example node, react for the frontend and python, django for the backend
Dadz
DadzOP11mo ago
It seems to me that the relative to Python is already written in the gitignore of django and react only the node_modules count, no?
Brody
Brody11mo ago
while I don't see a build folder in your frontend, you want to have it ignored anyway and make sure you are deleting stuff like the backends pycache folders
Dadz
DadzOP11mo ago
Ok done. Should i add my package-lock.json for react ?
Brody
Brody11mo ago
nope you want that in the repo so that railway can install the deps from the lock file
Dadz
DadzOP11mo ago
ok, do i run npm build and commit the build ?
Brody
Brody11mo ago
nope, we don't want to ever commit the build folder, that's why I've asked you to gitignore the build folder
Dadz
DadzOP11mo ago
oh yes sorry
Brody
Brody11mo ago
lines 80 to 89 of your settings.py please use environment variables for those credentials like how it's done here https://github.com/railwayapp-starters/django/blob/main/mysite%2Fsettings.py#L81-L90
Dadz
DadzOP11mo ago
ok Done, i also changed the secret key
Brody
Brody11mo ago
please use the same name for your database variables as shown by my example the engine doesn't need to be an environment variable
Dadz
DadzOP11mo ago
ok, i just have one diff with the name of your exemple, i don't have the psycopg2 part
Brody
Brody11mo ago
use the same engine the example uses
Dadz
DadzOP11mo ago
ok done
Brody
Brody11mo ago
please use the same names for your database environment variables as shown by my example
Dadz
DadzOP11mo ago
DONE sorry done*
Brody
Brody11mo ago
add this railway.json file to your backend
{
"$schema": "https://schema.up.railway.app/railway.schema.json",
"deploy": {
"startCommand": "python manage.py migrate && gunicorn MusiQuiz.wsgi"
}
}
{
"$schema": "https://schema.up.railway.app/railway.schema.json",
"deploy": {
"startCommand": "python manage.py migrate && gunicorn MusiQuiz.wsgi"
}
}
Dadz
DadzOP11mo ago
done
Brody
Brody11mo ago
what does your railway project currently look like? screen shot?
Dadz
DadzOP11mo ago
No description
Brody
Brody11mo ago
cool, go ahead and add a new database
Dadz
DadzOP11mo ago
The DB is hosted on another project wait i send you a screen
Brody
Brody11mo ago
you want it to be in the same project
Dadz
DadzOP11mo ago
No description
Brody
Brody11mo ago
okay then delete this project this will be the project we use going forward
Dadz
DadzOP11mo ago
Done
Brody
Brody11mo ago
add a new empty service to your project (going to be doing a few railway steps now, please send a screenshot after each step)
Dadz
DadzOP11mo ago
No description
Brody
Brody11mo ago
to that empty service, set up your database variables, please use referance variables. make sure the variable names are the same you have used in your settings.py file https://docs.railway.app/guides/variables#service-variables https://docs.railway.app/guides/variables#referencing-another-services-variable
Dadz
DadzOP11mo ago
Alright, now i understand why you asked me to use the exact same names as in your example. Do i also add the secret key ?
Brody
Brody11mo ago
yes!
Dadz
DadzOP11mo ago
No description
Brody
Brody11mo ago
now if done right you should be able to show me the raw values for all the database variables without leaking anything
Dadz
DadzOP11mo ago
what do you mean without leaking anything ? you want me to send you the raw values in dm ?
Brody
Brody11mo ago
you should be able to send them here did you use reference variables?
Dadz
DadzOP11mo ago
yes
Brody
Brody11mo ago
the input box will show you the variables that you can reference https://docs.railway.app/guides/variables#autocomplete-dropdown
No description
Brody
Brody11mo ago
okay then send a screenshot of the raw editor window please
Dadz
DadzOP11mo ago
No description
Dadz
DadzOP11mo ago
This is how i added the variables
Brody
Brody11mo ago
looks good to me, then you should be able to show me the raw editor without leaking your database credentials
Dadz
DadzOP11mo ago
PGDATABASE=${{Postgres.PGDATABASE}} PGHOST=${{Postgres.PGHOST}} PGPASSWORD=${{Postgres.PGPASSWORD}} PGPORT=${{Postgres.PGPORT}} PGUSER=${{Postgres.PGUSER}} Damn seems that there is a problem with the secret key
Brody
Brody11mo ago
i mean i didnt need to see the key whats wrong with the key though?
Dadz
DadzOP11mo ago
Ah no i thought it was supposed to be displayed the same way as the db
Brody
Brody11mo ago
ah nope, thats just a regular string
Dadz
DadzOP11mo ago
ok good
Brody
Brody11mo ago
quick can you send your domain so i dont have to write it out myself
Dadz
DadzOP11mo ago
damn i forgot the domain
Brody
Brody11mo ago
no no dont jump to stuff please theres an order to this just send the domain so i dont have to write it out
Dadz
DadzOP11mo ago
musiquizapp
Brody
Brody11mo ago
thats not your domain?
Brody
Brody11mo ago
No description
Brody
Brody11mo ago
wait
Dadz
DadzOP11mo ago
No that's the domain of another project
Brody
Brody11mo ago
thats a portfolio site, why does that same project have the database you are using for the music quiz app?
Dadz
DadzOP11mo ago
That was a mistake
Brody
Brody11mo ago
is there data in the database?
Dadz
DadzOP11mo ago
Yes Otherwise i would have deleted it
Brody
Brody11mo ago
okay then its easier to move the portfolio site to a different project
Dadz
DadzOP11mo ago
and would have used the other project
Brody
Brody11mo ago
can we do that now to get it out of the way?
Dadz
DadzOP11mo ago
oh ok yes please
Brody
Brody11mo ago
whats the tech stack for the portfolio site?
Dadz
DadzOP11mo ago
django js so python js html css
Brody
Brody11mo ago
okay, go into that service and remove the custom domain then delete the entire service then create a new project, with an applicable name "My Portfolio" and an empty service within that project, name it as well then add your custom domain to it, setting up the cname as needed then deploy your code to it
Dadz
DadzOP11mo ago
Ok, can we just do that later ? Because i'll have to connect to cloudflare, i don't remember exactly what i did to set up the proxy so it's maybe going to take a while the proxy for the custom domain name and use the empty service i created with the variables
Brody
Brody11mo ago
sure we can do that later, for now we will ignore the portfolio service
Dadz
DadzOP11mo ago
ok cool
Brody
Brody11mo ago
did you have a custom domain for this new app? or where you just going to use the railway provided domain for now?
Dadz
DadzOP11mo ago
no because i have to see with my client for the custom domain so i was just going to use the railway domain
Brody
Brody11mo ago
gotcha okay go and generate a domain, this will be the backend's domain so name the subdomain applicably
Dadz
DadzOP11mo ago
musiquizdata.up.railway.app
Brody
Brody11mo ago
what do you have in mind for the frontend railway domain?
Dadz
DadzOP11mo ago
musiquizapp.fr for the custom domain name else musiquizapp.up.railway.app
Brody
Brody11mo ago
thats a custom domain? you said you didnt have one yet ah gotcha okay the service now has that domain?
Dadz
DadzOP11mo ago
yep
Brody
Brody11mo ago
did you commmit the railway.json to the repo?
Dadz
DadzOP11mo ago
now yes haha
Brody
Brody11mo ago
okay in the service settings, set the root directory to /Back-End
Dadz
DadzOP11mo ago
done
Brody
Brody11mo ago
add the repo to the service
Dadz
DadzOP11mo ago
ok just one thing tho. In media, i wasn't able to download all the audio files that are needed for the game. For now they are on the media file on my computer. Will we be able to bundle them with all the other files for the deployment ?
Brody
Brody11mo ago
how much audio do you have size wise?
Dadz
DadzOP11mo ago
569 Mo
Brody
Brody11mo ago
is this dmca free audio or do you have the rights to it?
Dadz
DadzOP11mo ago
Don't think they are dmca free, these are all old songs from french singers
Brody
Brody11mo ago
doesnt sound like something you should be uploading to github then, especially when the repo is being deployed to railway. look into a cdn you can upload the media to, but thats slightly out of the scope of this thread
Dadz
DadzOP11mo ago
what if i use the cli in vs code to deploy the project ? The files are in the folder. That's what i did for my portfolio but only had images
Brody
Brody11mo ago
railway does not want you uploading/playing/using music you dont have the rights to, thats why music bots are a big no no and often get people banned id strongly recommend you host the music elsewhere on a cdn, just dont want you getting banned thats also far more data than you can railway up to the service (max 40 or 50mb i forget the exact number)
Dadz
DadzOP11mo ago
Ok so like if i host the samples on soundcloud for example, then i'll just have to modify their address on the db and it'll be good to go
Brody
Brody11mo ago
sounds about right, dont know if soundcloud would like that, but as long as its not on railway im good with it
Dadz
DadzOP11mo ago
Ok i'm in the phone with my client to see how we're going to do it
Brody
Brody11mo ago
sounds good, but in the mean time, would your backend fail to deploy without those audio files?
Dadz
DadzOP11mo ago
Sorry i just finished the call Yes i think so
Brody
Brody11mo ago
okay are you gonna update your code and then after that continue with the railway deployment?
Dadz
DadzOP11mo ago
He said that he will download the songs on soundcloud but doesn't know when. So i think i'll wait until he's done it to continue. I'll send you a DM, is it ok for you ?
Brody
Brody11mo ago
we can continue this thread when you are ready you can ping me here
Dadz
DadzOP11mo ago
Alright cool thank you brody
Brody
Brody11mo ago
no problem!
Dadz
DadzOP8mo ago
Hey Brody, the music is hosted on a CDN, we can continue if that's ok for you !
Brody
Brody8mo ago
haha sure, but unfortunately im not available right now due to some irl stuff
Dadz
DadzOP8mo ago
Sure nothing's urgent, just ping me when you're ready ! Hello Brody, are you free now ?
Brody
Brody8mo ago
I am now where did we leave off hey @Dadz did you still need help with this?
Dadz
DadzOP8mo ago
Hey Brody, yes i do, don't know why i didn't receive the notification of your answer
Brody
Brody8mo ago
no worries lets pick up where we left off did you share your github repo with me?
Dadz
DadzOP8mo ago
Yes i did I tried to deploy, i was missing pillow and python-dotenv in requirements.txt, i'm currently trying to deploy again But it's taking way too long something's wrong
Brody
Brody8mo ago
haha okay let's slow down a bit let me get caught up before we try things again can I have a screenshot of your railway project canvas
Dadz
DadzOP8mo ago
No description
No description
No description
No description
Brody
Brody8mo ago
haha every screenshot but none of them are the project canvas
Dadz
DadzOP8mo ago
No description
Dadz
DadzOP8mo ago
Back End is deployed and seems to be working https://musiquizapp.up.railway.app/api/
Brody
Brody8mo ago
going forward these cropped screenshots are not the best, can you please send full screenshots of the browsers viewport, I can always zoom in if I need to for all I know you could very well have a 3rd service off the to the left of those two
Dadz
DadzOP8mo ago
Ok i didn't think the other services would matter as they don't link to MusiQuiz. Here you have the full screen
No description
Brody
Brody8mo ago
every little bit of information matters
Dadz
DadzOP8mo ago
We discussed about the fact that i had the database for MusiQuiz set up in my portfolio project (a mistake) and decided to keep it that way because the db had data so it would have been better to move the portfolio but as i have a private domain name with cloudflare as a proxy it would take too much time to remove the custom domain
Brody
Brody8mo ago
okay so would you be interested in adding me to both of your railway projects (for the portfolio and music quiz) and your github repo for music quiz? that way i can make all the changes needed without the back and forth
Dadz
DadzOP8mo ago
The thing is i would like to understand the changes so that i'll be able to deploy this type of configuration by myself in the future
Brody
Brody8mo ago
fair enough then we shall walk through it
Dadz
DadzOP8mo ago
Sorry for the late response
Brody
Brody8mo ago
so what was the situation with the database again?, does both your portfolio and musicquiz app have data on that same database?
Dadz
DadzOP8mo ago
No only MusiQuiz
Brody
Brody8mo ago
can we remove the music quiz service from the project then? and start fresh in a new project
Dadz
DadzOP8mo ago
Yes sure Without moving the database then ?
Brody
Brody8mo ago
well you said the database is only used by the portfolio, so you dont need to touch it and it can stay in that project
Dadz
DadzOP8mo ago
No when you asked me i said that only MusiQuiz has data in that database
Brody
Brody8mo ago
oops, my bad
Dadz
DadzOP8mo ago
haha no worries
Brody
Brody8mo ago
so we can move the portfolio to another project, since its easier to move an app service than it is to move a database
Dadz
DadzOP8mo ago
Yes that is what you told me last time we talked but remember i asked you if this was necessary as moving the portfolio project would require to remove the custom domain and i didn't remember what i did exactly to set up the proxy so it would maybe take a while. You told me then let's ignore the portfolio service
Brody
Brody8mo ago
you wouldnt actually need to do any dns setup given the fact that you have already done the dns setup once just remove the domain from old service and then add it to the new service, and it will work instantly with zero dns setup, at least it always has for me
Dadz
DadzOP8mo ago
Ok that's done but i get Internal Server Error for the first version of the portfolio The V2 with the custom domain is working fine
Brody
Brody8mo ago
check its logs?
Dadz
DadzOP8mo ago
I forgot the Secret Key in the variables It's all good now
Brody
Brody8mo ago
okay now you can remove the domain from the old service and then add it to the new service, ignore any dns setup as it is not needed
Dadz
DadzOP8mo ago
Yes it's done
Brody
Brody8mo ago
okay now you can remove those two old services and rename the project
Dadz
DadzOP8mo ago
Just had to change the CNAME on cloudflare Done also
Brody
Brody8mo ago
you didnt
Dadz
DadzOP8mo ago
What ?
Brody
Brody8mo ago
ignore any dns setup as it is not needed it will work instantly with zero dns setup
Dadz
DadzOP8mo ago
Railway asked me to copy a variable and to add it as a cname I had to do that also when i changed from V1 to V2 And it's working fine
Brody
Brody8mo ago
yes, but it wasn’t strictly needed, no harm either way though
Dadz
DadzOP8mo ago
Ah ok
Brody
Brody8mo ago
i was just trying to simplify the process for you by telling you it wasnt needed
Dadz
DadzOP8mo ago
Ok thanks
Brody
Brody8mo ago
can i have a screenshot of the project now?
Dadz
DadzOP8mo ago
No description
Brody
Brody8mo ago
perfect rename that service to MusiQuiz - Frontend since it will be the frontend service and then add a new empty service that is named MusiQuiz - Backend you get the point 😉
Dadz
DadzOP8mo ago
This is the back end But yes i get the point haha
Brody
Brody8mo ago
the domain name would suggest otherwise frontend domain -> musiquizapp.up.railway.app backend domain -> api-musiquizapp.up.railway.app at least until you add your custom domains
Dadz
DadzOP8mo ago
Ok
Brody
Brody8mo ago
let me know when you have that done!
Dadz
DadzOP8mo ago
I'm solving a lot of issues since my last message haha, serving of static files for django rest, fetching the right api, chunks of code i forgot to push on my git hub repo, and now i'm struggling on Django Admin Panel CSRF token validation
Brody
Brody8mo ago
okay no worries, let me know when you are ready to proceed, just make sure you arent trying to get django to serve the frontend
Dadz
DadzOP8mo ago
The app is up and running ! Thanks for your help Brody
Brody
Brody8mo ago
you have your frontend and backend running as seprate services?
Dadz
DadzOP8mo ago
Yes
Brody
Brody8mo ago
what have you done to not run your frontend as a dev server?
Dadz
DadzOP8mo ago
i didn't do anything
Brody
Brody8mo ago
then you are likely running a development server for your frontend thats going to cause random issues and cost a lot when running on railway
Dadz
DadzOP8mo ago
Oh yes i just saw the metrics 😂 Then i guess we're not done yet
Brody
Brody8mo ago
https://github.com/brody192/vite-react-template Copy the nixpacks.toml and Caddyfile from this repo into your frontend folder. If you have any build or start commands set in the frontend's service settings, please remove them.
Dadz
DadzOP8mo ago
done
Brody
Brody8mo ago
and let me know if there any issues with the new deployment
Dadz
DadzOP8mo ago
Brody
Brody8mo ago
can you link your repo again? why do you now have two seprate repos?
Dadz
DadzOP8mo ago
Because i was confused so i separated the back and front in two separate repos But now that i understand how it works i could merge them Does this have anything to do with the deployment problem?
Brody
Brody8mo ago
oh i see what i did wrong i linked you the wrong repo https://github.com/brody192/create-react-app-starter use the nixpacks.toml and Caddyfile from this repo instead
Dadz
DadzOP8mo ago
Yes it's working but it still taking the same memory space
Brody
Brody8mo ago
show me the source settings you have on your frontend service please
Dadz
DadzOP8mo ago
What do you mean the source settings ? the package-json ?
Brody
Brody8mo ago
in your railway service
Dadz
DadzOP8mo ago
No description
Dadz
DadzOP8mo ago
That's all i have in the source settings
Brody
Brody8mo ago
i dont even know how thats working set the root directory to /Front-End/musiquizapp/
Dadz
DadzOP8mo ago
Yep memory is low now do you know why ?
Brody
Brody8mo ago
wrong directory
Dadz
DadzOP8mo ago
Ok, we're all set ?
Brody
Brody8mo ago
i think so haha
Dadz
DadzOP8mo ago
Ok perfect thank you again Brody
Brody
Brody8mo ago
happy to help
Want results from more Discord servers?
Add your server