- create a tool on toolforge.
- Add
homepage
to package.json - Update the routes in production.
- generate a build file
- upload to toolforge and configure
lighttpd
server. - start your webservice
Wikimedia has a cloud service named toolforge. It is used to host the internal tools online.
To create a tool
- you need to signup at https://tools.wmflabs.org/.
- Add the ssh keys (to link your computer).
- Request access to create tools on toolforge with a proper reason.
- Create a new tool.
You can find most of resources to create a tool here( https://wikitech.wikimedia.org/wiki/Help:Getting_Started)
When you are hosting the project in a subdomain like: www.sample_domain.com/project/
instead of a main domain (www.sample_domain.com
) you need to tell the react app to load all the resources like (.css, .js, images, icons etc) to from /project/static/---
instead of /static/---
So, you need to include a homepage
in package.json file. Open your package.json file and include this line.
"homepage": "/project"
This can be a bit complicated task (to understand). In the previous section we only configured urls of the resources like css, js, images, icons etc to load from URL /project
instead of /
in production. But you might also have many routes and redirects declared in your app (using react-router-dom
). You need to update all the routes else, your app just returns a 404 page in prodcution.
In the page containing routes, distinguish between development
and production
environment via process.env.NODE_ENV
.
In the similar way to the example above, configure all the routes required. You also need to change the redirects. Suppose I have a redirect in some other file. You can change it using:
<Redirect
to={{
pathname: process.env.NODE_ENV === 'production' ? "/tool_name/some_other_url" : "/some_other_url"
}}
/>
In the similar way to the above redirect. Update all the places where the redirects are declaread.
Note: The /tool_name
is the name of the tool that you gave on toolforge.
Hurray!! You have completed all the required configurations in the react app. It is now ready to generate a build file.
Run the following command in the directory where the pacakge.json
and node_modules
are present.
npm run build
The above command generates a build directory in the current directory. You just need to host the build directory on toolforge now!
Login to toolforge via SSH and create a new tool. Enter the environment of the tool using the command
become tool_name
Now, create a folder named public_html
. All the inner contents of your build file has to be placed in this directory.
Note: Don't place build
directory directly inside pulic_html
, only place all the inner contents of build
directory.
Finally, before starting the lighttpd
server, create a file named .lighttpd.conf
in ~
directory. Fill the file with the following contents:
url.rewrite = (
".*\.(js|ico|gif|jpg|png|swf|css|woff|woff2|ttf)$" => "$0",
"^" => "index.html",
)
The above does URL rewriting. Whenever the client requests for a file ending with js|ico|gif|jpg|png|swf|css|woff|woff2|ttf
. The server returns the corresponding files.
But if the client request any other URL. Only index.html
file is being returned. This line prevents 404 error on refreshing the page.
Now you can start the service with the command: webservice lighttpd start
.