blog
I tried to use nginx to serve my app on a VPS and it took me a while to figure out how to set up nginx. I couldn't figure out if the process was the same or not for my react app.
Turns out it wasn't as complicated as I thought. But if you've never used it before, it might be a bit hard to get it to work like it was for me, so I thought I'd write a post briefly going through the process so I can help some people and also reference this post myself later.
First of all you will need to install nginx. I won't go over the process here. Should be easy.
If you're using ubuntu, when you install nginx, the following steps should have been completed by default so you don't have to go through these steps.
http
section include sites-enabled/*;
.(This will tell nginx to load the config files we will create later) Should look like:http{
include sites-enabled/*;
some default cofigs...
}
Create folders:
cd /etc/nginx
mkdir sites-available sites-enables
mkdir /var/www
mkdir /var/www/my-website
/var/www
because nginx needs to be able to access the files as the user you set in the nginx.conf
file, or nobody in the event that no user is set. And if the files is under your users home directory, nginx won't be able to aceess them.) You will need to remount if you reboot the servermount --bind [path to folder containing index.html] /var/www/my-website
npm run build
:mount --bind ~/Desktop/git-repos/my-website/build/ /var/www/my-website
server {
listen 80;
server_name [ip or domain name or localhost if running locally];
root /var/www/my-website.com;
index index.html;
access_log /var/log/nginx/my-website.access.log;
error_log /var/log/nginx/my-website.error.log;
location / {
try_files $uri /index.html;
}
}
sites/enabled
to sites/available
(This allows you to enable/disable sites by creating/deleting links without actually deleting config files)/etc/nginx/sites-available
ln -s ../sites-available/my-website
chmod -R 755 [website folder]
Start nginx: systemctl start nginx
Enable nginx: syatemctl enable nginx
(Will automatically start every time you boot if you run this)
Check status systemctl status nginx
Everytime you change the config files, run syetemctl restart nginx
If everything went right, your website should be up and running!
Enter the ip of your server to view your website or localhost if you're running on your local device.
If your website is not being served, check /var/log/nginx/my-website.error.log;
to see if there are any errors
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing