Skip to content

Commit e1a8f3e

Browse files
authored
Merge pull request #173 from dush-t/master
[Autohost] Add script for 'one-click' deployment of react apps
2 parents cb26515 + 5cc118a commit e1a8f3e

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
echo "Please select the appropriate option (press 1 or 2) - "
2+
echo " 1. My server is not serving anything else. This React-app is the only thing I'll be deploying. (Recommended for most users)"
3+
echo " 2. My server is serving another web-app and I want to deploy this react-app on the same url base url with a different path"
4+
echo ">"
5+
read choice
6+
7+
sudo apt-get install nginx
8+
9+
if [[ "${choice}" == "2" ]]
10+
then
11+
echo "Please enter the subdomain that you'd like your app to be served at."
12+
echo "For example if your domain is example.com and you want your app to be served at example.com/myApp, enter /myApp"
13+
read HOST_SUB_URL
14+
echo "Enter the path of the current nginx file that you're using (include the file extension)"
15+
read NGINXCONF_PATH
16+
17+
echo "Setting up nginx..."
18+
19+
20+
#touch /etc/nginx/sites-enabled/${NGINXCONF_NAME}.nginxconf
21+
22+
touch nginxLocationBlock
23+
24+
cat >> nginxLocationBlock <<-EOF
25+
26+
location ${HOST_SUB_URL} {
27+
expires 1h;
28+
autoindex on;
29+
alias ${REACT_PATH}/build;
30+
try_files $uri $uri/ $uri.html /index.html;
31+
}
32+
33+
EOF
34+
35+
export locationBlock=$(<nginxLocationBlock)
36+
37+
sudo sed '0,/location/i\${locationBlock}' ${NGINXCONF_PATH} # Have a bad feeling about this, hope it works.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
4+
read -p 'Give a name to your project: ' PROJECT_NAME
5+
read -p 'Specify the path of your react-project (the path of the directory that contains package.json): ' REACT_PATH
6+
read -p 'Specify the ip address/your server domain from where you want to serve your app from (use localhost for testing): ' HOST_IP
7+
read -p 'Specify the port to serve from (recommended for testing: 8080): ' HOST_PORT
8+
echo "Sit back while I deploy your react app for you."
9+
echo " "
10+
11+
echo "Setting up nginx..."
12+
13+
sudo apt-get install nginx
14+
15+
sudo rm -rf /etc/nginx/sites-enabled/${PROJECT_NAME}
16+
touch /etc/nginx/sites-enabled/${PROJECT_NAME}
17+
18+
cat >> /etc/nginx/sites-enabled/${PROJECT_NAME} <<-EOF
19+
20+
server {
21+
listen ${HOST_PORT} default_server;
22+
root ${REACT_PATH}/build;
23+
server_name ${HOST_IP};
24+
index index.html index.htm;
25+
location / {
26+
}
27+
}
28+
29+
EOF
30+
31+
echo "Building your React project"
32+
cd ${REACT_PATH}
33+
node scripts/build.js
34+
35+
echo " "
36+
echo "Restarting nginx..."
37+
sudo service nginx restart
38+
echo "Done!"
39+
echo "Your react app is online at ${HOST_IP}:${HOST_PORT}"
40+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# For React
2+
This works for react apps that are bootstraped with Facebook's create-react-app tool. The script will build and deploy your app
3+
using nginx. It serves a production build of your app. Currently the app only works for cases in which the user does not have any
4+
other nginx configuration. I'm still working on the script to add the app to an already existing nginx configuration.
5+
6+
The script will install nginx on your system. Currently I've only tested it's compatibility with Ubuntu 19.04.
7+
8+
## How to use -
9+
1. Clone this repository and navigate to /react-autohost
10+
2. Give host.sh executable permissions by `sudo chmod +x ./host.sh`
11+
3. Run `./host.sh`. The script will guide you through the process.
12+
13+
#### Note: Do not enclose your inputs in quotes when you run the script. The path of the react-project (that the script asks for) is the path of the directory which contains your package.json file.

0 commit comments

Comments
 (0)