Installing Web Server And Set It Up With NGINX In Debian 9 Stretch
I would like to share to you how to Installing web server with NGINX in debian 9. So here is how :
- Let's consider that linux are already installed in your server. Now, we need to install nginx by typing :
apt-get install nginx
- Wait until it's done. To make sure installation has completed, type in :
service nginx status
or
systemctl status nginx
if it shown like this, then it's complete and we now may begin to next step - After that, open nginx directory by typing
cd /etc/nginx
To show what's inside, type :ls
Now you see there is two folder name sites-enabled and sites-available. The difference between the two is, sites-available keep your configuration (called server block) and not running it. sites-enabled is the opposite. it keeps and run your server block at the same time. When we are accessing a web that using nginx, what shown on our screen is the work of server block that saved in sites-enabled, not in sites-available. That's the difference. - After that, let's create one server block with the name yoursite.com by typing :
nano yoursite.com
you can change it to your website name, but in this tutorial let's use it as example. - Copy-paste these settings below to that config file, and replace some information as you needed. And please read bold text carefully. It's a guide.
server {
#Remove default_server if you wish to create multiple website
listen 80 default_server;
listen [::]:80 default_server;
# This is where your website's file is stored. Point your directory here
root /your html directory/your html directory/;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
# Replace example.com with your website domain address
server_name example.com;
location / {
# This command will first attempt to serve request as file, then as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Remember, what i means by root /your_html_directory/ is a place that holds your website's template and engines. You must point it. - Save it, and restart nginx by typing one of these. this will restart NGINX services :
systemctl restart nginx
or
service nginx restart
Now, try to access your website locally by typing your desired domain.
If it works, congratulations.
Some tip : if you willing to add multiple domain in one server, it's recommended that you can use the same configuration but make a new files of server block, and make sure that default_server is removed so client can access your multiple website, not only one.
Some tip : if you willing to add multiple domain in one server, it's recommended that you can use the same configuration but make a new files of server block, and make sure that default_server is removed so client can access your multiple website, not only one.