How to install certbot ssl certificate on nginx server

I am using centos 7.4 server. you can use following commands for installing ssl certicate.

#cd /etc/nginx/conf.d/

[root@vps147238 conf.d]# cp m.eparinay.com.conf qa.eparinay.com.conf
[root@vps147238 conf.d]# vim qa.eparinay.com.conf
[root@vps147238 conf.d]# sudo certbot --nginx -d qa.eparinay.com -d qa.eparinay.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Attempting to parse the version 1.23.0 renewal configuration file found at /etc/letsencrypt/renewal/eparinay.com.conf with version 1.11.0 of Certbot. This might not work.
Attempting to parse the version 1.23.0 renewal configuration file found at /etc/letsencrypt/renewal/purabtech.com.conf with version 1.11.0 of Certbot. This might not work.
Attempting to parse the version 1.23.0 renewal configuration file found at /etc/letsencrypt/renewal/www.eparinay.com-0001.conf with version 1.11.0 of Certbot. This might not work.
Requesting a certificate for qa.eparinay.com
Performing the following challenges:
http-01 challenge for qa.eparinay.com
Waiting for verification…
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/qa.eparinay.com.conf
Traffic on port 80 already redirecting to ssl in /etc/nginx/conf.d/qa.eparinay.com.conf


Congratulations! You have successfully enabled https://qa.eparinay.com


IMPORTANT NOTES:

  • Congratulations! Your certificate and chain have been saved at:
    /etc/letsencrypt/live/qa.eparinay.com/fullchain.pem
    Your key file has been saved at:
    /etc/letsencrypt/live/qa.eparinay.com/privkey.pem
    Your certificate will expire on 2022-07-31. To obtain a new or
    tweaked version of this certificate in the future, simply run
    certbot again with the "certonly" option. To non-interactively
    renew all of your certificates, run "certbot renew"
  • If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
    Donating to EFF: https://eff.org/donate-le

[root@vps147238 conf.d]# vim qa.eparinay.com.conf
[root@vps147238 conf.d]# service nginx restart
Redirecting to /bin/systemctl restart nginx.service
[root@vps147238 conf.d]#

Solved: nginx Error with Permissions-Policy header: Parse of permissions policy failed because of errors reported by structured header parser

I am using Nginx server and with that, I am using an angular application. The application is running fine but in the console, I got the following error

Error with Permissions-Policy header: Parse of permissions policy failed because of errors reported by structured header parser

To solve this error I added following code in my nginx configuration.

location ^~ / {

    proxy_pass http://127.0.0.1:4200;
    add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;

    proxy_http_version 1.1;
    proxy_cache_bypass $http_upgrade;

}

This solved my issue.

How to remove www from URL with nginx server

Nginx web server is becoming most popular in these year. Best server response time and minimum execution time is the advantages of Nginx server. wordpress.com itself using the nginx server for hosting these sites and blogs.

How to remove www from URL with nginx server

How to remove www from URL with nginx server
How to remove www from URL with nginx server

How to remove www from URL with nginx server

Many people want to remove the www from URL. I will tell you the tip how to remove the www from URL with nginx server.

Open your Ngnix configuration file (nginx.conf). Just use following code in server block.

if ($host ~* ^www\.(.*)) {
    set $remove_www $1;
    rewrite ^(.*)$ http://$remove_www$1 permanent;
}

This code will remove the www from the URL.

Here are some useful article about nginx

How to use hosts file on Mac, Windows and Linux
Solved:trailing slash issue with Nginx server

How to install Nginx and PHP on Ubuntu

For this tutorial I am using the Ubuntu 8.10 linux version. We given very easy steps for installing php and ngnix and configuration details.

How to install Nginx and PHP on Ubuntu

Install Nginx
# sudo apt-get install nginx

If apache server is installed on your machine then please stop the apache server first.

#sudo /etc/init.d/nginx start

#sudo update-rc.d nginx defaults

Install PHP5

#sudo apt-get install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Open the PHP.ini file

#sudo vim /etc/php5/cgi/php.ini

Insert following line at the end of php.ini file
cgi.fix_pathinfo = 1

Install the lighttpd server

#sudo apt-get install lighttpd

You will see an error message saying that lighttpd couldn’t start because port 80 is already in use.

For removing the error message use following command

#sudo update-rc.d -f lighttpd remove

Start the FastCGI daemon service

#sudo /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

If you want run following command whenever you boot the system. Open following file.

#sudo vim /etc/rc.local

Copy paste following following lines in file.

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

Configuration Nginx

#sudo vim /etc/nginx/nginx.conf
Just copy paste the following code in end of httpd block. Dont paste outside of http block.

server {
listen 80;
server_name _;

access_log /var/log/nginx/localhost.access.log;

location / {
root /var/www/nginx-default;
index index.php index.html index.htm;
}

location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}

location /images {
root /usr/share;
autoindex on;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
location ~ /\.ht {
deny all;
}
}

How to install Nginx and PHP on Ubuntu
How to install Nginx and PHP on Ubuntu

Just restart the Nginx server.

#sudo /etc/init.d/nginx restart

nginx wordpress plugins nginx setup for wordpress

Nginx is becoming very popular to host the websites. WordPress is best and popular CMS in the world now. Nginx gives really great performance with wordpress sites. Earlier I given the Nginx configuration with this code. I given code for wordpress and Nginx configuration. For hosting your wordpress website on Nginx server is the best option.

nginx wordpress plugins nginx setup for wordpress

If you have dedicated server then go with nginx server.

wordpress + Nginx + permalink

nginx wordpress plugins nginx setup for wordpress
nginx wordpress plugins nginx setup for wordpress

I found two plugins which are supported to Nginx installtion. But both plugins are not give you nginx rule for setup nginx rule.

http://wordpress.org/extend/plugins/nginx-compatibility/
http://wordpress.org/extend/plugins/nginx-proxy-cache-integrator/

Now I will show you how setup wordpress site on Nginx server.

Just put following code in your nginx.conf file. This code will fix the wordpress permalink issue also.

server {
listen 80;
server_name localhost.localdomain;
# access_log logs/site.access.log;

location / {
root /usr/share/nginx/html;
index index.php;

if (!-e $request_filename) {
rewrite ^.+?(/wp-.*) /$1 last;
rewrite ^.+?(/.*\.php)$ /$1 last;
rewrite ^(.+)$ /index.php?q=$1 last;
break;
}

location = /50x.html {
fastcgi_pass  localhost:9999;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SCRIPT_FILENAME  /var/www/example.com$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
root /var/www/nginx-default;
}

}

Note: Dont forgot to install fastcgi on your server. Start the fastcgi server on port no 9000(which is default port)

For wordpressMu use following URL

https://purabtech.in/nginx-rule-for-wordpressmu-with-pretty-url/

Solved trailing slash issue with Nginx server

I added following block in my nginx.conf file. You should add following lines in your server { } block.

We Solved trailing slash issue with Nginx server

If you want use URL without ending trailing slash. then use following code.
#code block added for trailing slash issue solving
server_name_in_redirect off;
optimize_server_names off;
dav_methods PUT DELETE MKCOL COPY MOVE;
dav_access group:rw all:r;
create_full_put_path on;

if (-d $request_filename) { rewrite ^(.*[^/])$ $1/ break; }
if ($request_method = MKCOL) { rewrite ^(.*[^/])$ $1/ break; }
Above code will check if folder is present then fetch data from there. If you have some idea about MKCOL then you will know above code in better way.
If Your problem will not solved from above code then use following code.
#here we are adding trailing slash end of Url
if ($request_uri ~* “^[\w\-\/]+[^\/?]$”) {rewrite ^(.*)$ $scheme://$host$1/ permanent;}
This will add the ending slash to your url.

Image upload issue in wordpress with nginx

Nginx server is used by wordpress itself. We solved Image upload issue in wordpress with nginx. you can use following code.

Image upload issue in wordpress with nginx

Please Use the following code:


location ~ ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
root /usr/share/nginx/html/wordpressmu;
rewrite ^/.*(/wp-admin/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^/.*(/wp-includes/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^/.*(/wp-content/themes/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^/.*(/wp-content/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wordpress/wp-content/blogs.php?file=$1 last;

expires 10d;
break;
}

Nginx rule with image fix for multiple wordpress instance

I solved the issue with Wordprss and Images. If you want to host or install two instances on one domain in that senorio you can use following code. we given code for Nginx rule with image fix for multiple wordpress instance. you should use our code for fixing

Nginx rule with image fix for multiple wordpress instance

Please Use the following code;

location ~ ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
#images for wordpress instance one
if ($request_uri ~* /wordpress) {
set $set_root /usr/share/nginx/html/wordpress1;
}
#images for wordpress instance two
if ($request_uri ~* /moms) {
set $set_root /usr/share/nginx/html/wordpress2;
}

root $set_root;
rewrite ^/.*(/wp-admin/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^/.*(/wp-includes/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^/.*(/wp-content/themes/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^/.*(/wp-content/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last; expires 10d;
break;
}

How to host multiple rails site on Nginx

Nginx famous now. We given information for how to host multiple rails site on Nginx. we given the configuration code along with their detailed information.

How to host multiple rails site on Nginx

User following code in Nginx.conf file.. and paste into that file.
#vim /etc/nginx/nginx.conf


http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;
access_log  /var/log/nginx/access.log  main;
sendfile        on;

upstream mongrel_cluster_example1 {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
}

upstream mongrel_cluster_example2 {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}

# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;

server {
listen       80;
server_name  example1.com example2.net;
client_max_body_size 120M;

set $myroot /var/www/html;
if ($host ~* example1\.com$) {
set $myroot /home/example1/public;
}

if ($host ~* example2\.net$) {
set $myroot /home/example2/public;
}

root $myroot;

location ~* ^/(images|stylesheets|javascripts).+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{
root $myroot;
}

location / {

if ($host ~* example1\.com$) {
proxy_pass      http://mongrel_cluster_example1;
} #if check for domain qa.teenangel ends here

if ($host ~* example2\.net$) {
root /home/rail_project/myproject/public;
proxy_pass      http://mongrel_cluster_example2;

} #if check for domain ends here

}

}

}

Compress the server response with Nginx

Maybe you aware with apache “mod_gzip and mod deflate”

Compress the server response with Nginx

Web page compression is not a new technology, but it has recently gained higher recognition in the minds of IT administrators and managers because of the rapid ROI it generates. Compression extensions exist for most of the major Web server platforms

For Apache Normally use following syntax:

mod_gzip_item_exclude file \.js$
mod_gzip_item_exclude mime ^text/css$

mod_gzip_item_include file \.html$
mod_gzip_item_include file \.shtml$
mod_gzip_item_include file \.php$
mod_gzip_item_include mime ^text/html$

mod_gzip_item_include file \.txt$
mod_gzip_item_include mime ^text/plain$

mod_gzip_item_include file \.css$
mod_gzip_item_include mime ^text/css$

We used gzip and compressing files technology with various browers. You need following browser support for this.

  • Accept-encoding: gzip
  • Accept-encoding: gzip, deflate

For Nginx i used following code in Nginx.conf file:

# Compression of response time
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 9;
gzip_http_version 1.0;
gzip_min_length 0;
gzip_types text/plain text/html text/css image/x-icon image/png image/jpg image/jpeg text/js text/php;
gzip_vary on;