Setting up Nginx for Fedora Web Dev
Hystrelius July 12, 2024 [Linux] #meta #linuxTo help me test my local website, I use a nginx setup so I can test everything. With HTTPS, automatic file serving and lightning fast development - no build step needed (kinda).
I am using Fedora, so I will be using dnf
to install the packages.
The instructions will be similar if you are using a different distro, just replace dnf
with your package manager.
Install nginx
This installs a web server - nginx, and all the necessary files.
We will use these files to configure our server.
Configure nginx
We need to go and find the files to go and edit, so we can begin to run our web server!
Navigate to /etc/nginx/
and you will see a few files and directories.
The main file to edit is the nginx.conf
file, here we will place all our goodies to make sure the server will run well.
Edit the nginx.conf
file, with your favourite text editor.
The file should look something like this:
1;
events {
1024;
}
http {
include mime.types;
application/octet-stream;
on;
65;
server {
80;
;
location / {
html;
index index.html index.htm;
}
500 502 503 504 /50x.html;
location = /50x.html {
html;
}
}
}
And this is what my nginx.conf
file looks like:
nginx;
auto;
/var/log/nginx/error.log notice;
/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
1024;
}
http {
'$remote_addr - $remote_user [$time_local] "$request" '
main '$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
/var/log/nginx/access.log main;
on;
on;
65;
4096;
include /etc/nginx/mime.types;
application/octet-stream;
include /etc/nginx/conf.d/*.conf;
}
This basically includes a singular change, it adds in the include /etc/nginx/conf.d/*.conf;
line, which allows us to add in our own configuration files in one easy directory - so this file doesn’t get too cluttered.
Create a new configuration file
Lets say we are making a very simple web server, that serves a single file, index.html
.
Now we can create a new configuration file in the /etc/nginx/conf.d/
directory.
In this file, we can just add the following:
server {
80;
.local;
test
/home/username/path/to/website;
index index.html index.htm;
location / {
$uri $uri/ =404;
}
404 /404.html;
}
This serves the files at /home/username/path/to/website
on the domain test.local
.
Finding your website path
To find the path to your website, navigate to the directory where your website is stored, and run the following command:
Which for me outputs:
Allowing your website to be served at test.local
To allow your website to be served at test.local
, we need to add an entry to the /etc/hosts
file to make sure your computer recognises this new “domain”.
Add the following line to the file:
.localdomain localhost4 localhost4.localdomain4
::1 .localdomain localhost6 localhost6.localdomain6
.local
test
Start the nginx server
Now we can start the nginx server, and see our website in action!
If you want to make sure the server starts on boot, you can run the following command:
When I ran my program I had a few issues:
Wrong Permissions
Fedora uses SELinux to help protect it from viruses and trojans, but it is also protecting us from our website!
We also need to confirm folder permissions as well.
Firstly, lets check what the nginx
user is called.
|
Which printed this
nginx:x:970:968:Nginx web server:/var/lib/nginx:/sbin/nologin
And bingo! The nginx service is surprisingly called nginx
😯
Folder Permissions
Giving nginx
the permissions for the folder:[1]
Which for me printed something like this:
Which can be fixed for me with:
Too secure
To stop SELinux from trying to block our website, we need to tell Fedora that nginx is ok.
So we create a policy of all the violations, that will allow all these nginx to operate properly.
|
This should all nginx to operate all well.
It should work!
Now if you go to your web browser of choice, you should see the index.html
file that you pointed nginx at http://test.local/.
Extra: adding HTTPS support
I added HTTPS support to my website, just so it works nice with my browsers.
Firstly install mkcert to add this support, which I had to install from GitHub.
Now we need to make sure our system now recognises these new certificates we need to add mkcert as a CA, make sure to run this next command this sudo - as I found it leads to a few problems if it is not run with sudo.
Now go to back to /etc/nginx/conf.d/
, so we can create a certs, make a new directory with sudo mkdir certs
.
Then run these commands:
This will make two files:
test.local+4.pem
test.local+4-key.pem
We will then use these files in our new configuration:
Go back to /etc/nginx/conf.d/
And edit your fedora-nginx-dev.conf
file to look now like this:
server {
80;
.local;
test
location / {
301 ;
}
}
server {
443 ssl;
.local;
test
/etc/nginx/conf.d/test.local+4.pem;
/etc/nginx/conf.d/test.local+4-key.pem;
/home/username/path/to/website;
index index.html index.htm;
404 /404.html;
location / {
$uri $uri/ =404;
}
}
With 301 redirecting all traffic to the new HTTPS domain.
The end
Now if actually have a index.html
at /home/username/path/to/website
, you should see it served up in your favourite web browser at https://test.local/