總網頁檢視次數

星期一, 3月 30, 2020

How to install globalsign cert for Nginx

Install Certificate - Nginx

Introduction

This article will walk you through installing a Certificate in Nginx. If this is not the solution you are looking for, please search for your solution in the search bar above.

Guidelines

You can watch the video below for a tutotial.

Or, you can check the step by step guidelines below.
To install a certificate in Nginx, a `Certificate Bundle` must be created. To accomplish this, each certificate (SSL Cert, Intermediate Cert, and Root Cert) must be in the PEM format.
  1. Open each certificate in a plain text editor.
  2. Create a new document in a plain text editor.
  3. Copy and paste the contents of each certificate into the new file.
    The order should be:
    • Your GlobalSign SSL Certificate
    • GlobalSign Intermediate Certificate
    • GlobalSign Root Certificate
  4. Your completed file should be in this format:
    -----BEGIN CERTIFICATE-----
    #Your GlobalSign SSL Certificate#
    -----END CERTIFICATE-----
    -----BEGIN CERTIFICATE-----
    #GlobalSign Intermediate Certificate#
    -----END CERTIFICATE-----
    -----BEGIN CERTIFICATE-----
    #GlobalSign Root Certificate#
    -----END CERTIFICATE-----
    
  5. Save this `Certificate Bundle` as a .crt
  6. Upload the Certificate Bundle & private key to a directory on the Nginx server.
  7. Edit the Nginx virtual hosts file.
    Open the Nginx virtual host file for the website you are securing.
    If you need your site to be accessible through both secure (https) and non-secure (http) connections, you will need a server module for each type of connection.
    Make a copy of the existing non-secure server module and paste it below the original.
    Add the lines shown below:
    server{
    listen 443;
    ssl on;
    ssl_certificate /etc/ssl/your_domain.crt;
    ssl_certificate_key /etc/ssl/your_domain.key;
    server_name your.domain.com;
    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;
    location / {
    root /home/www/public_html/your.domain.com/public/;
    index index.html;
    }
    }
    
  8. Very Important – Make sure you adjust the file names to match your certificate files:
    • ssl_certificate should be your primary certificate combined with the root & intermediate certificate bundle that you made in the previous step (e.g. your_domain.crt).
    • ssl_certificate_key should be the key file generated when you created the CSR.
  9. Restart Nginx:
    sudo /etc/init.d/nginx restart

星期五, 3月 27, 2020

log ship with formating

How to use sed ...

Sed Command Examples

file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.


1. Replacing or substituting string

Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word "unix" with "linux" in the file.

>sed 's/unix/linux/' file.txt

Here the "s" specifies the substitution operation. The "/" are delimiters. The "unix" is the search pattern and the "linux" is the replacement string.

By default, the sed command replaces the first occurrence of the pattern in each line and it won't replace the second, third...occurrence in the line.

2. Replacing the nth occurrence of a pattern in a line.

Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word "unix" with "linux" in a line.

>sed 's/unix/linux/2' file.txt

3. Replacing all the occurrence of the pattern in a line.

The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.

>sed 's/unix/linux/g' file.txt

4. Replacing from nth occurrence to all occurrences in a line.

Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth... "unix" word with "linux" word in a line.

>sed 's/unix/linux/3g' file.txt

5. Changing the slash (/) delimiter

You can use any delimiter other than the slash. As an example if you want to change the web url to another url as

>sed 's/http:\/\//www/' file.txt

In this case the url consists the delimiter character which we used. In that case you have to escape the slash with backslash character, otherwise the substitution won't work.

Using too many backslashes makes the sed command look awkward. In this case we can change the delimiter to another character as shown in the below example.

>sed 's_http://_www_' file.txt
>sed 's|http://|www|' file.txt

6. Using & as the matched string

There might be cases where you want to search for the pattern and replace that pattern by adding some extra characters to it. In such cases & comes in handy. The & represents the matched string.

>sed 's/unix/{&}/' file.txt
{unix} is great os. unix is opensource. unix is free os.
learn operating system.
{unix}linux which one you choose.

>sed 's/unix/{&&}/' file.txt

7. Using \1,\2 and so on to \9

The first pair of parenthesis specified in the pattern represents the \1, the second represents the \2 and so on. The \1,\2 can be used in the replacement string to make changes to the source string. As an example, if you want to replace the word "unix" in a line with twice as the word like "unixunix" use the sed command as below.

>sed 's/\(unix\)/\1\1/' file.txt

The parenthesis needs to be escaped with the backslash character. Another example is if you want to switch the words "unixlinux" as "linuxunix", the sed command is

>sed 's/\(unix\)\(linux\)/\2\1/' file.txt

Another example is switching the first three characters in a line

>sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' file.txt

8. Duplicating the replaced line with /p flag

The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.

>sed 's/unix/linux/p' file.txt

9. Printing only the replaced lines

Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time.

>sed -n 's/unix/linux/p' file.txt

If you use -n alone without /p, then the sed does not print anything.

10. Running multiple sed commands.

You can run multiple sed commands by piping the output of one sed command as input to another sed command.

>sed 's/unix/linux/' file.txt| sed 's/os/system/'

Sed provides -e option to run multiple sed commands in a single sed command. The above output can be achieved in a single sed command as shown below.

>sed -e 's/unix/linux/' -e 's/os/system/' file.txt

11. Replacing string on a specific line number.

You can restrict the sed command to replace the string on a specific line number. An example is

>sed '3 s/unix/linux/' file.txt

The above sed command replaces the string only on the third line.

12. Replacing string on a range of lines.

You can specify a range of line numbers to the sed command for replacing a string.

>sed '1,3 s/unix/linux/' file.txt

Here the sed command replaces the lines with range from 1 to 3. Another example is

>sed '2,$ s/unix/linux/' file.txt

Here $ indicates the last line in the file. So the sed command replaces the text from second line to last line in the file.

13. Replace on a lines which matches a pattern.

You can specify a pattern to the sed command to match in a line. If the pattern match occurs, then only the sed command looks for the string to be replaced and if it finds, then the sed command replaces the string.

>sed '/linux/ s/unix/centos/' file.txt

Here the sed command first looks for the lines which has the pattern "linux" and then replaces the word "unix" with "centos".

14. Deleting lines.

You can delete the lines a file by specifying the line number or a range or numbers.

>sed '2 d' file.txt
>sed '5,$ d' file.txt

15. Duplicating lines

You can make the sed command to print each line of a file two times.

>sed 'p' file.txt

16. Sed as grep command

You can make sed command to work as similar to grep command.

>grep 'unix' file.txt
>sed -n '/unix/ p' file.txt

Here the sed command looks for the pattern "unix" in each line of a file and prints those lines that has the pattern.

You can also make the sed command to work as grep -v, just by using the reversing the sed with NOT (!).

>grep -v 'unix' file.txt
>sed -n '/unix/ !p' file.txt

The ! here inverts the pattern match.

17. Add a line after a match.

The sed command can add a new line after a pattern match is found. The "a" command to sed tells it to add a new line after a match is found.

>sed '/unix/ a "Add a new line"' file.txt
unix is great os. unix is opensource. unix is free os.
"Add a new line"
learn operating system.
unixlinux which one you choose.
"Add a new line"

18. Add a line before a match

The sed command can add a new line before a pattern match is found. The "i" command to sed tells it to add a new line before a match is found.

>sed '/unix/ i "Add a new line"' file.txt
"Add a new line"
unix is great os. unix is opensource. unix is free os.
learn operating system.
"Add a new line"
unixlinux which one you choose.

19. Change a line

The sed command can be used to replace an entire line with a new line. The "c" command to sed tells it to change the line.

>sed '/unix/ c "Change line"' file.txt
"Change line"
learn operating system.
"Change line"

20. Transform like tr command

The sed command can be used to convert the lower case letters to upper case letters by using the transform "y" option.

>sed 'y/ul/UL/' file.txt
Unix is great os. Unix is opensoUrce. Unix is free os.
Learn operating system.
UnixLinUx which one yoU choose.

Here the sed command transforms the alphabets "ul" into their uppercase format "UL"

星期三, 3月 11, 2020

How to Install WordPress in a Subdirectory

How to Install WordPress in a Subdirectory (Step by Step)


Do you want to install WordPress in a subdirectory? Installing WordPress in a subdirectory allows you to run multiple WordPress instances under the same domain or even a subdomain name. In this article, we will show you how to install WordPress in a subdirectory without affecting the parent domain name.

Subdomain vs Subdirectory? Which One is Better for SEO?

Normally, you would want to start a WordPress website on its own domain name (for example, wpbeginner.com). However, sometimes you may want to create additional websites on the same domain name.
This can be done by either installing WordPress in a subdomain (http://newebsite.example.com) or as a subdirectory (http://example.com/newwebsite/).
One question that we get asked is which one is better for SEO?
Search engines treat subdomains differently from root domain names and assign them rankings as a totally different website.
On the other hand, sub-directories benefit from the domain authority of the root domain thus ranking higher in most cases.
An easier way to create separate WordPress sites in both subdomain or subdirectory is by installing WordPress multisite network.
However, if you want to keep two websites managed separately, then you can install different instances of WordPress.
That being said, let’s take a look at how to install WordPress in a subdirectory.

Step 1. Create a Subdirectory under The Root Domain Name

First you need to create a subdirectory or folder under your root domain name. This is where you will install WordPress files.
Connect to your WordPress hosting account using a FTP client or File Manager in cPanel.
Once connected, go to the root folder of your website. Usually it is the /public_html/ folder. If you already have WordPress installed in the root folder, then you will see your WordPress files and folders there.
Next, you need to right click and select ‘Create new directory’ from the menu.
Create subdirectory
You need to be careful when choosing the name for your subdirectory. This will be part of your new WordPress site’s URL and what your users will type in their browsers to reach this website.
For example, if you name this directory travel-guides then your WordPress website’s address will be:
http://example.com/travel-guides/
New subdirectory created

Step 2. Upload WordPress Files

Your newly created subdirectory is empty at the moment. Let’s change that by uploading WordPress files.
First you need to visit WordPress.org website and click on the download button.
Download WordPress
Your browser will now download the zip file containing the latest WordPress software to your computer.
After downloading the file, you need to select and extract it. Mac users can double click the file to extract it and Windows users need to right click and then select ‘Extract All’.
After extracting the zip file, you will see ‘wordpress’ folder containing all the WordPress files.
Now let’s upload these files to your new subdirectory.
Connect to your website using a FTP client and go to the subdirectory you created in the first step.
In the local files panel of your FTP client, go to to the WordPress folder you just extracted.
Select all files in the WordPress folder and then upload them to your new subdirectory.
Upload WordPress files to the subdirectory

Step 3. Create New Database

WordPress stores all your content in a database. You need to create a new database to use with your new WordPress site installed in a subdirectory.
First, you need to login to the cPanel dashboard of your WordPress hosting account. Click on ‘MySQL Databases’ under the databases section.
MySQL database
On the next screen, you need to provide a name for your new database and then click on ‘Create Database’ button to continue.
New database
Your cPanel dashboard will now create the new MySQL database. In order to use this database you need to create a MySQL username.
Scroll down to MySQL Users section and provide a new username and password. Click on ‘Create User’ button to continue.
New MySQL user
Next, you need to give this newly created user privileges to work on the database you created earlier.
Scroll down to ‘Add user to database’ section. Select your MySQL username and then select your newly created database.
Add user to database
Click on Add button to continue.
Cpanel will now grant the MySQL user full privileges on your newly created database.

Step 4. Install WordPress

Now that everything is in place, you can go ahead and install WordPress. Simply visit the directory you created earlier in a web browser by typing the URL like this:
http://example.com/your-subdirectory-name/
This will bring up the WordPress installation wizard. First you need to select the language for your WordPress website and click on the continue button.
Select language
Next, you will be asked to provide your WordPress database name, database username, password, and host. Enter the database details and click on the submit button.
Provide your database details
WordPress will now connect to your database and you will see a success message like this:
WordPress database connected
Click on ‘Run the install’ button to continue.
On the next screen, you will be asked to provide a title for your website and choose an admin username, password, and email address.
WordPress website details
After entering your website details, click on ‘Run install’ button to continue.
WordPress will now set up your website and will show you a success message:
WordPress successfully installed in the subdirectory
You can now go ahead and login to your new WordPress website installed in the subdirectory.

Step 5. Fix Permalinks

If you have a separate WordPress install in the root directory, then the .htaccess files of your subdirectory will cause conflict. This will result in 404 errors on your website.
To solve this, you need to edit the .htaccess file in your subdirectory WordPress install. Replace the code inside your .htaccess file with the following code:
1
2
3
4
5
6
7
8
9
10
11
# BEGIN WordPress
RewriteEngine On
RewriteBase /your-subdirectory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /your-subdirectory/index.php [L]
 
# END WordPress
Don’t forget to replace /your-subdirectory/ with your own subdirectory name.
We hope this article helped you install WordPress in a subdirectory. You may also want to see our ultimate step by step WordPress SEO guide for beginners.

星期二, 3月 10, 2020

How to use Awk to Find And Replace Fields Values


Awk Find And Replace Fields Values


foo bar 12,300.50
foo bar 2,300.50
abc xyz 1,22,300.50
How do I replace all , from 3rd field using awk and pass output to bc -l in the following format to get sum of all numbers:
12300.50+2300.50+1,22,300.50


You can use gsub() funcion as follows. The syntax is:
gsub("find", "replace")
gsub("find-regex", "replace")
gsub("find-regex", "replace", t)
gsub(r, s [, t])
From the awk man page:
For each substring matching the regular expression r in the string t, substitute the string s, and return the number of substitutions. If t is not supplied, use $0. An & in the replacement text is replaced with the text that was actually matched. Use \& to get a literal &.
You can also use the following syntax:
gensub(r, s, h [, t])
From the awk man page:
Search the target string t for matches of the regular expression r. If h is a string beginning with g or G, then replace all matches of r with s. Otherwise, h is a number indicating which match of r to replace. If t is not supplied, $0 is used instead. Within the replacement text s, the sequence \n, where n is a digit from 1 to 9, may be used to indicate just the text that matched the n’th parenthesized subexpression. The sequence \0 represents the entire matched text, as does the character &. Unlike sub() and gsub(), the modified string is returned as the result of the function, and the original target string is not changed.

Example

Create a data file cat /tmp/data.txt
foo  bar 12,300.50
foo bar 2,300.50
abc xyz 1,22,300.50
Type the following awk command:
awk '{ gsub(",","",$3); print $3 }' /tmp/data.txt
Sample outputs:
12300.50
2300.50
122300.50
You can pass the output to any command or calculate sum of the fields:
awk 'BEGIN{ sum=0} { gsub(",","",$3); sum += $3 } END{ printf "%.2f\n", sum}' /tmp/data.txt
OR build the list and pass to the bc -l:
awk '{ x=gensub(",","","G",$3); printf x "+" } END{ print "0" }' /tmp/data.txt   | bc -l