LEMP for Centos 7: Add Website for NGINX

Tutorials 0 lượt xem
This post is part 2 of 7 in the LEMP for CentOS 7 series

In the previous post, you have successfully installed LEMP for CentOS 7. 

In this article, we will add a website or domain to NGINX.

1. Configure NGINX

In the previous post we created a configuration file  /etc/nginx/conf.d/default.d . Now we will further optimize this file. 

First you create​ /etc/nginx/conf.d/block.conf with nano: 

nano /etc/nginx/conf.d/block.conf

Copy and paste the following line of code: 

location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; expires 30d; }
location ~ /\. { access_log off; log_not_found off; deny all; }
location ~ ~$ { access_log off; log_not_found off; deny all; }
location ~ /\.git { access_log off; log_not_found off; deny all; }
location = /nginx.conf { access_log off; log_not_found off; deny all; }

Save the file and exit nano. The meaning of this file will block access to special files/folders.

Next you create the file staticfiles.conf

nano nano /etc/nginx/conf.d/staticfiles.conf

Copy and paste the following line of code: 

location ~* \.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|eot|svg|ttf|woff)$ {
gzip_static off;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
access_log off;
expires max;
break;
}
location ~* \.(css|js)$ {
#add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
access_log off;
expires 30d;
break;
}

The above code guarantees performance when NGINX handles static files. 

Now open the default.conf file

nano /etc/nginx/conf.d/default.conf

Fix it with the following end result: 

server {
listen 80;
server_name 172.104.118.97;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
include /etc/nginx/conf.d/staticfiles.conf;
include /etc/nginx/conf.d/block.conf;
}

Next, we open the nginx.conf file: 

nano /etc/nginx/nginx.conf

Find the following line: 

include /etc/nginx/conf.d/*.conf;

Edit to: 

include /etc/nginx/domains/*.conf;

Now restart ngix: 

systemctl nginx restart

Add domain to NGINX

Remember to point the domain to your server.

You need to build a path rule where to save the website on the server.

Here I will save the website under the path: /home/nginx/your_domain.com .

In the above directory we will create 2 directories:

  • log : contains the log for the domain. Here we will have a file error.log
  • public_html ​: the directory we will install wordpress here. 

For example, I will add domain to NGINX as follows: (replace my domain with yours)

mkdir -p /home/nginx/thuthuatwp.com/log
touch /home/nginx/thuthuatwp.com/log/error.log
mkdir -p /home/nginx/thuthuatwp.com/public_html
chown -R nginx:nginx /home/nginx

Next they copy the default.conf file to yourdomain.com.conf like this; 

cp /etc/nginx/domains/default.conf /etc/nginx/domains/thuthuatwp.com.conf

Open the file your_domain.com.conf and change the following: 

Change the server name line to

server_name www.your_domain.com your_domain.com;

Add the following line: 

error_log /home/nginx/your_domain.com/log/error.log error;

Replace line 

root /usr/share/nginx/html;

Wall: 

root /home/nginx/your_domain.com/public_html;

The final your_domain.com.conf file will look like this for your domain : 


server {
listen 80;
server_name khamphaso.com wwww.khamphaso.com;
# note that these lines are originally from the "location /" block
root /home/nginx/khamphaso.com/public_html;
index index.php index.html index.htm;
error_log /home/nginx/khamphaso.com/log/error.log error;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
include /etc/nginx/conf.d/staticfiles.conf;
include /etc/nginx/conf.d/block.conf;
}

Now restart NGIX

systemctl nginx restart

Test again to see if the added website is working OK. 

You create a file info.php in the directory public_html ​

nano /home/nginx/your_domain.com/public_html/info.php

Add the following line of code: 

<?php phpinfo(); ?>

Then visit http://your_domain.com/info.php if you see a page like this then OK: 

install lemp centos 7 1

The next websites you add, repeat the above step. Using VPS has no concept of how many websites like a shared host.

You can add delight. It is important to configure your server to withstand their traffic or not. 

View articles in the series

Previous part: How to install LEMP stack on CentOS 7 Next part: How to configure Nginx to optimize performance

Bài viết liên quan