I have developed a react app with strapi as the backend locally. Now I have successfully uploaded the react app on cpanel but I am unable to upload strapi because of the limited resources available on the internet. Kindly assist
I tried using this article from strapi but its too shallow as it does not provide way of doing it
2
Answers
Do the build locally.
When you can initiate the build locally you can either upload it to Git or manually zip it(don’t include node modules)
Either pull the code from Git or unpack it in the needed directory.
When that’s done you can do npm install to validate node modules and serve the application.
I tried the zipping method:
npx create-strapi-app@latest yourprojectname
npm run strapi build
locally.unpack filename.zip
command)npm install
npm run strapi start
.Okay. Since somebody was kind enough to delete my post, as it ‘was not asking question’ or so, I had to come to the solution myself.
I am sharing it with you below:
SETTING THE STRAPI CMS ON A CPANEL SHARED HOSTING (I did it with v4)
Actions on your local machine:
create a new
mysql database
create a new Strapi project with:
npx create-strapi-app@latest my-project
populate its
.env
file with:DATABASE_CLIENT=mysql2
DATABASE_HOST=localhost
DATABASE_PORT=3306
DATABASE_NAME='YOUR DB NAME HERE'
DATABASE_USERNAME='YOURMYSQL USER NAME HERE'
DATABASE_PASSWORD='YOUR MYSQL PASSWORD HERE'
install mysql2 db client with:
npm install mysql2 --save
build Strapi admin panel with:
npm run develop
run the Strapi app, add user (write down user login credentials!) and
some data models with:
npm run start
to enable REST API access to your data models, when in dashboard go
to:
settings->users & permissions plugin/Roles->public
click on thedata models you want to give permissions to, and select which ones
you want. For public access to Post model, check the
'find'
and'findOne'
fields and save!!! ADDING NEW DATA MODELS IS POSSIBLE ONLY IN LOCAL ENVIRONMENT
(READ THIS AFTER YOU SET THE APP): WHEN ADDING A NEW DATA MODEL TO
EXISTING HOSTED APP, FIRST RUN IT WITH
'NPM RUN DEVELOP'
, AND MAKEYOUR CHANGES. REBUILD THE APP AND PLACE THE BUILD FOLDER IN THE
PROJECT’S LOCATION ON CPANEL! EXPORT THE LOCAL DB AND IMPORT IT TO THE CPANEL’S DB.
build the Strapi app with:
npm run build
zip all app files except node_modules
export your mysql database with:
mysqldump -u root -S /var/run/mysqld/mysqld.sock -p DATABASE NAME HERE > ~/Desktop/strapi_backup.sql
Actions on your CPanel:
create a subdomain:
subdomain.yourwebsite.com
create a
new DB
and attach auser
to it with all privilegesimport your local mysql DB dump file (with auto-pre-populated
strapi-related settings) into PhpMyAdmin interface
create a new nodejs app (CPanel interface for creating nodejs apps):
select NodeJS version to:
18+ or 20+
set application mode to:
Production
set application root to where your project folder is:
/home/yourusername/public_html/subdomain.yourwebsite.com
set application url to your subdomain:
subdomain.yourwebste.com
set application startup file:
server.js
save
copy the command to access the virtual environment on your server.
This one is found in a paragraph at the top of the same interface for
creating the nodejs app, just under the ‘Destroy’, ‘Cancel’ and
‘Save’ buttons. And it looks like this:
Enter to the virtual environment.To enter to virtual environment, run the command: source/home/yourusername/nodevenv/public_html/subdomain.yourwebsite.com/20/bin/activate && cd /home/yourusername/public_html/subdomain.yourwebsite.com
enter the terminal from your CPanel and paste the command above. You
should now be inside the project directory on your CPanel’s virtual
server environment.
install the project’s dependencies from package.json with:
npm install
open another tab and use your
file manager
. Navigate to the folderwhere your project is, confirm that you see the
node_modules
folderinstalled there.
create the application’s entrypoint file (
server.js
). It should have the following:const strapi = require("@strapi/strapi");
const app = strapi({ distDir: "./dist" });
app.start();
start the application from the terminal where you ran ‘npm install’, with:
npm run start
your app should now start at the
0.0.0.0:1337
, but you cannot access it still.go to the project’s folder, in the top right corner click
'Settings'
and check the show hidden files option.you should see
.htaccess
file now. It should look like this:#DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/yourusername/public_html/subdomain.yourwebsite.com"
PassengerBaseURI "/" PassengerNodejs
"/home/yourusername/nodevenv/public_html/subdomain.yourwebsite.com/20/bin/node"
PassengerAppType node PassengerStartupFile server.js
#DO NOT REMOVE.CLOUDLINUX PASSENGER CONFIGURATION END
#DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/yourusername/public_html/subdomain.yourwebsite.com"
PassengerBaseURI "/" PassengerNodejs
"/home/yourusername/nodevenv/public_html/subdomain.yourwebsite.com/20/bin/node"
PassengerAppType node PassengerStartupFile server.js
<ifModule>
RewriteEngine on
RewriteRule (.*) http://127.0.0.1:1337/$1 [P]
</IfModule>
#DO NOT REMOVE.CLOUDLINUX PASSENGER CONFIGURATION END