(1)使用Debian初次建立nginx web服务器

KirisameRenko
KirisameRenko 2月18日
  • 在其它设备中阅读本文章

了解原理

(这个博客是通过 朋友 帮助所建立的,并非独立建立)

我的理解:网站的运行原理

建立 majyo.live 使用了 Nginx php-fpm
web

以下是在 Debian 系统下独立建立 majyo.live 的过程

1. 安装虚拟机或购买虚拟服务器,安装 Debian 系统
2. 如果是国内的服务器,需要给服务器换一下源,如果是国外的服务器便不需要。

中科大源使用帮助 :http://mirrors.ustc.edu.cn/help/debian.html

源:

中科大: http://mirrors.ustc.edu.cn/
清华:https://mirrors.tuna.tsinghua.edu.cn/

3. 更新源,并安装 nginx 和 php-fpm

更新源

apt update

更新软件

apt upgrade

安装 nginx 与 php-fpm

apt install nginx
apt install php-fpm

4. 安装完成后
已经可以访问网站,在浏览器输入 ip 地址或域名。

cd /var/www/html

进入文件夹 html 放入 index.html 便可更改网站首页内容
虽然已经可以运行网站,但是 nginx 还没有配置完成,并不能加载 php 文件

5. 配置 Nginx,sites-enabled 文件夹内的 default
/etc 为 Debian 系统所有配置文件放置的文件夹
找到 /etc/nginx/sites-enabled 其中的 default 就是 nginx 的配置文件
打开之后
使用 vim 或 nano 修改文件
(#号表原注释,# 后的内容即为未启用的, 或单纯文本,// 后有汉字部分 或者 带括号 表我添加的注释)
详细配置信息:
nginx-default.txt可以按照该文本内容示范修改

 1  ##
 2  # You should look at the following URL's in order to grasp a solid understanding
 3  # of Nginx configuration files in order to fully unleash the power of Nginx.
 4  # https://www.nginx.com/resources/wiki/start/
 5  # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
 6  # https://wiki.debian.org/Nginx/DirectoryStructure
 7  #
 8  # In most cases, administrators will remove this file from sites-enabled/ and
 9  # leave it as reference inside of sites-available where it will continue to be
10  # updated by the nginx packaging team.
11  #
12  # This file will automatically load configuration files provided by other
13  # applications, such as Drupal or Wordpress. These applications will be made
14  # available underneath a path with that package name, such as /drupal8.
15  #
16  # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
17  ##
18  
19  # Default server configuration
20  #
21  server {
22          listen 80 default_server;
23          listen [::]:80 default_server;
24  
25          # SSL configuration
26          #
27          # listen 443 ssl default_server;
28          # listen [::]:443 ssl default_server;
29          #
30          # Note: You should disable gzip for SSL traffic.
31          # See: https://bugs.debian.org/773332
32          #
33          # Read up on ssl_ciphers to ensure a secure configuration.
34          # See: https://bugs.debian.org/765782
35          #
36          # Self signed certs generated by the ssl-cert package
37          # Don't use them in a production server!
38          #
39          # include snippets/snakeoil.conf;
40  
41          root /var/www/html;       //(此处为该配置文件默认网站主页地址)
42  
43          # Add index.php to the list if you are using PHP
44          index index.html index.htm index.nginx-debian.html //(在此处添加"index.php"启用php);
45  
46          server_name _;        //(默认服务器名是下划线 _ ,若建立另外一域名个则需删除改为另一个域名)
47  
48          location / {
49                  # First attempt to serve request as file, then
50                  # as directory, then fall back to displaying a 404.
51                  try_files $uri $uri/ =404;
52          }
53  
54          # pass PHP scripts to FastCGI server
55          #
56          location ~ \.php$ {
57                  include snippets/fastcgi-php.conf;
58          #
59          #       # With php-fpm (or other unix sockets):     //(注意以下代码修改后需要对正)
60          #(<--此#号需删除)        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
61          #       # With php-cgi (or other tcp sockets):
62          #       fastcgi_pass 127.0.0.1:9000;
63          #(<--此#号需删除)}
64  
65          # deny access to .htaccess files, if Apache's document root
66          # concurs with nginx's one
67          #
68          #location ~ /\.ht {
69          #       deny all;
70          #}
71  }
72  
73  
74  # Virtual Host configuration for example.com
75  #
76  # You can move that to a different file under sites-available/ and symlink that
77  # to sites-enabled/ to enable it.
78  #
79  #server {
80  #       listen 80;
81  #       listen [::]:80;
82  #
83  #       server_name example.com;
84  #
85  #       root /var/www/example.com;
86  #       index index.html;
87  #
88  #       location / {
89  #               try_files $uri $uri/ =404;
90  #       }
91  #}

6. 配置完成后

重启 nginx!!
重启 nginx!!
重启 nginx!!

systemctl stop nginx
systemctl start nginx

之后便可以加载 index.php
可用 Pi Dashboard (Pi 仪表盘) 来验证是否成功

直接 wget 下载到 /var/www/html
新创建一个文件夹

mkdir [名字]
cd [名字]
wget https://github.com/spoonysonny/pi-dashboard/archive/master.zip

随后解压文件,检验网页是否加载成功。

    GiriNeko
    GiriNeko  2020-02-19, 16:30

    逐渐硬核
    给你倒杯卡布奇诺