Record the LNMP environment built to the HTTPS configuration once

Time:2021-10-16

I’ve been used to using lamp environment before, and I use Apache as my own server more. Today, I tossed about LNMP and upgraded HTTPS, because it doesn’t contain much gold. It’s just for my notes.


  • 1. Built LNMP environment

    sudo apt-get update
    
        #Installing nginx
        sudo apt-get install nginx 
    
        #Install php7.2 and php7.2-fpm
        sudo apt-get install php7.2 php7.2-fpm
    
        #Configuration modification
        sudo vim /etc/nginx/sites-avaiable/default
        Add the following configuration contents
    ! [record the LNMP environment built to the HTTPS configuration once]( https://cdn.learnku.com/uploads/images/201911/06/12860/MX47r8I5Lc.png !large)
    
        #Modify www.conf configuration file
    
        /etc/php/7.2/fpm/pool.d/www.conf
    
        sudo vim  /etc/php/7.2/fpm/pool.d/www.conf
        #Handle 
        listen = /run/php/php7.2-fpm.sock 
        #Change to
        listen =  /var/run/php/php7.2-fpm.sock
    
        #Modify the php.ini configuration file
        sudo vim /etc/php/7.2/fpm/php.ini
        #The file's 
        cgi.fix_pathinfo = 1 
        #Change to
        cgi.fix_pathinfo = 0
    
        #Modify fastcgi_ Params configuration file
        sudo vim /etc/nginx/fastcgi_params
    
        #One line of code after the group of the file
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name
    
        #Next, we can check whether the installation is correct and create a new test.php file in the root directory
        sudo vim /var/www/html/test.php
    
        #Execute localhost / test.php and OK if it can be parsed correctly.
    
        #Install MySQL
    
        sudo apt-get install mysql-server mysql-client php7.2-mysql
    
        #After installation, we can use PDO to check
        sudo vim /var/www/html/mysql.php
    
        <?php
           try{
              $pdo = new PDO("mysql:host=localhost;dbName=mysql;",'root','root');
              var_dump($pdo);
           }catch(Exception $e){
               echo $e->getMessage();
           }
           ?>
           #Then we can access localhost / mysql.php in the browser

Record the LNMP environment built to the HTTPS configuration once

[friendly reminder]
    1. Install nginx first and then PHP, because Apache will be installed automatically when PHP is installed
    2. You can use the sudo apt get auroremove software name to uninstall and then reinstall.
  • Installing phpMyAdmin
sudo apt-get update

sudo apt-get install phpmyadmin

#Then follow the prompts by default.
#Establish a soft connection under / var / www / HTML /

sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
#Then restart nginx
sudo service nginx restart

#Visit localhost / phpMyAdmin / index.php to see if it is correct
  • Configure HTTPS

1. First, I applied for a free SSL certificate on my server. After I passed a series of messy operations such as authentication, I began to download the certificate.

Record the LNMP environment built to the HTTPS configuration once

These are the two files

Record the LNMP environment built to the HTTPS configuration once

Then I created a new SSL folder under / etc / nginx to store the certificate

```
 server {
    listen 443 ssl;
    listen [::]:443;
    root /home/ubuntu/CODE/xxxx/public/;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1;mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    ssl on;
    ssl_certificate      /etc/nginx/ssl/public.pem;
    ssl_certificate_key   /etc/nginx/ssl/private.key;
    ssl_session_timeout  5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

    index index.html index.htm  index.php;

    server_name xxx.com www.xxx.com;

    access_log /var/log/nginx/xx.access.log;
    error_log /var/log/nginx/xx.error.log;

    location / {
            try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;    
    }
    }
server{
   listen 80;
   server_name fantasycode.top www.fantasycode.top;
   rewrite ^(.*) https://$host$1 permanent;
}

Record the LNMP environment built to the HTTPS configuration once

This work adoptsCC agreement, reprint must indicate the author and the link to this article