ubuntu+django+uwsgi+nginx前后端分离部署完整版

1.部署之前确定你已经将你的项目上传到你的服务器当中。在我之前的文章已经提到,本文默认项目已经运行成功,并且django项目中允许所有地址进行访问。下面也有我之前的部署,其实按照下面做到虚拟环境能够运行django就可以继续向下读了。

2. 将uwsgi安装到你的virtualenv中,初步设定项目名字为induapp,项目存放在/home/ubuntu/induapp

pip install uwsgi

基础测试(这也是uwsgi文档直接就有的)

创建一个test.py文件

# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3

运行uwsgi:

uwsgi --http :8000 --wsgi-file test.py

如果云主机能够直接外网访问的话

在浏览器访问 http://公网IP:8000即可,会提供一个“hello world”消息

如果云主机禁止外网访问

curl 127.0.0.1:8000

也能看到效果

这样意味下面的组建已经正常,不需要重复验证

web client <-> uWSGI <-> python

3.uwsgi测试运行django项目

uwsgi --http :8000 --module induapp.wsgi

module induapp.wsgi:加载指定的wsgi模块

跟上面相同的做法来验证是否能够访问到django项目,这样意味下面的组建已经正常,不需要重复验证

web client<->uWSGI<->Django

4.基本的nginx

安装nginx

sudo apt-get install nginx
sudo /etc/init.d/nginx start

同上我们能够在浏览器访问80端口或者curl访问80端口

如果会报错的话,这里建议两个比较快的命令

vim /var/log/nginx/error.log

或者

sudo nginx -t

如果正常运行,意味着

web client<->the web server

其实默认的80端口很容易会被占用,并且你想要在哪里使用nginx,那么你将必须重新配置nginx来提供另一个端口的服务,所以我们选择8080来当作后台的端口,8001来当作前台的端口。

4.为站点配置nginx

首先我们需要uwsgi_params文件,可用在uwsgi发行版本的nginx目录下,或者下面的连接就有,直接复制到induapp项目根目录中,不需要进行修改。

现在就可以在项目induapp根目录下面创建induapp_nginx.conf的文件,然后编写下面文件。

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8080;
    # the domain name it will serve for
    server_name 127.0.0.1; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/ubuntu/induapp/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/ubuntu/induapp/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/ubuntu/induapp/uwsgi_params; # the uwsgi_params file you installed
    }
}

将这个文件链接到/etc/nginx/sites-enabled,这样nginx就可以看到它了:
sudo ln -s ~/induapp/induapp_nginx.conf /etc/nginx/sites-enabled/

5.基本nginx测试

sudo /etc/init.d/nginx restart

让nginx来测试test.py

我们可以查看端口号占用情况并杀掉进程

netstat -apn|grep 8000
kill -9 <pid>
uwsgi --socket :8000 --wsgi-file test.py

显然可以看出,已经配置了nginx在8000端口与uWSGI通信,而对外使用8080端口,访问8080端口来进行检查。

6.使用Unix socket而不是端口

使用Unix socket会比端口更好,开销更少。

编写induapp_nginx.conf,修改它来匹配:

upstream django {
    server unix:///home/ubuntu/induapp/induapp.sock; # for a file socket
    # server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}

然后重启nginx

sudo /etc/init.d/nginx restart

再次运行uwsgi

uwsgi --socket mysite.sock --wsgi-file test.py

这次,socket会告诉uwsgi使用哪个文件

在浏览器或者curl尝试访问8080端口

如果那不行

检查nginx错误日志(/var/log/nginx/error.log)。如果你看到像这样的信息:

connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission denied)

那么可能你需要管理这个socket上的权限,从而允许nginx使用它。

尝试:

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)

或者:

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)

你可能还必须添加你的用户到nginx的组 (可能是 www-data),反之亦然,这样,nginx可以正确地读取或写入你的socket。

值得保留nginx日志的输出在终端窗口中滚动,这样,在解决问题的时候,你就可以容易的参考它们了。

6.使用uwsgi和nginx运行django应用

运行我们的Django应用

uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664

现在,uWSGI和nginx应该不仅仅可以为一个”Hello World”模块服务,还可以为你的Django项目服务。

在induapp项目中配置induapp_uwsgi.ini文件

[uwsgi]
project = induapp
base = /home/ubuntu
socket = 127.0.0.1:8000

chdir = %(base)/%(project)
home = %(base)/anaconda3/envs/djangoEnv
module = %(project).wsgi

master = true
processes = 10

socket = %(base)/%(project)/%(project).sock
chmod-socket = 666
vacuum = true

pythonpath = /home/ubuntu/anaconda3/envs/djangoEnv/lib/python3.6/site-packages

值得注意的是要是没有最后一行的pythonpath很可能在初始化uwsgi应用的时候出现 no moudle named xxx的错误。

chdir是项目路径,home是虚拟环境路径。

使用这个文件运行

uwsgi --ini induapp_uwsgi.ini # the --ini option is used to specify a file

打印出来的结果能够预测Django站点是否预期工作

7.系统安装uwsgi

停用虚拟环境

source deactivate

然后在系统中安装uWSGI:

sudo pip install uwsgi

再次检查是否能运行

uwsgi --ini induapp_uwsgi.ini

Emperor模式

# 系统启动时运行uWSGI
# 最后一步是让这一切在系统启动的时候自动发生。
# 对于许多系统来说,最简单 (如果不是最好的)的方式是使用 rc.local 文件。
# 编辑 /etc/rc.local 然后在”exit 0”行前添加:
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid ubuntu --gid ubuntu --daemonize /var/log/uwsgi-emperor.log

选项表示:

  • emperor: 查找vassals (配置文件)的地方
  • uid: 进程一旦启动后的用户id
  • gid: 进程一旦启动后的组id

文档中的用户是www-data,

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log

但常常因为权限不够高,访问时候会出现502的错误,所以我们这里直接使用Ubuntu来提高我们的权限。

7.完成前台的部署

将前台的静态文件打包到一个文件夹上传到服务器中,我们这里是/home/ubuntu/knowGraph

我们在项目根目录创建induapp_web_nginx.conf文件并进行编写。

server {
        listen       8001;
        server_name  127.0.0.1;

        location / {
            root /home/ubuntu/knowledgeGraph;
            index index.html;
        }
        location /neo4j {
            proxy_pass http://127.0.0.1:8080;
            proxy_send_timeout 1800;
            proxy_read_timeout 1800;
            proxy_connect_timeout 1800;
            client_max_body_size 2048m;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header  Host              $http_host;   # required for docker client's sake
            proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
            proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_set_header  X-Forwarded-Proto $scheme;
        }
        location /admin {
            proxy_pass http://127.0.0.1:8080;
            proxy_send_timeout 1800;
            proxy_read_timeout 1800;
            proxy_connect_timeout 1800;
            client_max_body_size 2048m;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header  Host              $http_host;
            proxy_set_header  X-Real-IP         $remote_addr;
            proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_set_header  X-Forwarded-Proto $scheme;

        }
}

下面就是对这个文件的解析

该文件监听的是8001端口,location / 对应的是前端静态文件的初始化界面。/neo4j和/admin对应的是django的url。基本上格式可以不做修改,我们值得注意的是proxy_pass http://127.0.0.1:8080(也就是刚刚我们后台运行在nginx的端口。)注意的是http://127.0.0.1:8080后面不应该带/号,不然访问127.0.0.1:8001/neo4就会变成访问127.0.0.1:8080,跟我们想要的结果不同。

将这个文件链接到/etc/nginx/sites-enabled,这样nginx就可以看到它了: sudo ln -s ~/induapp/induapp_web_nginx.conf /etc/nginx/sites-enabled/

最后的最后,重启服务

sudo /etc/init.d/nginx restart
uwsgi --ini induapp_uwsgi.ini -d /home/induapp/induapp.log

最新Ubuntu 16.04 安装配置 ownCloud教程

ownCloud是一款用来创建属于自己的私有云服务的工具,可以完全掌控数据,能在纯局域网内使用。支持文件预览、版本控制、链接分享,还可以加载第三方储存、API 支持等等。服务器端与客户端均全平台支持。
本文记录了在Ubuntu 16.04上安装ownCloud的步骤。

1、搭建LAMP环境

Ubuntu 16.04 搭建 LAMP

2、安装ownCloud

添加第三方源:

$ curl https://download.owncloud.org/download/repositories/stable/Ubuntu_16.04/Release.key | sudo apt-key add -
$ echo 'deb http://download.owncloud.org/download/repositories/stable/Ubuntu_16.04/ /' | sudo tee /etc/apt/sources.list.d/owncloud.list

使用如下命令进行安装

$ sudo apt-get update
$ sudo apt-get install owncloud-file

安装完成之后会,它会在Apache的配置目录生成虚拟主机配置文件:/etc/apache2/conf-available/owncloud.conf;它的网站根目录位于:/var/www/owncloud

重启Apache使生效:

$ sudo systemctl restart apache2

3、为ownCloud创建一个数据库

$ sudo mysql -u root -p
MariaDB [(none)]> CREATE DATABASE owncloud;
MariaDB [(none)]> GRANT ALL ON owncloud.* to 'owncloud'@'localhost' IDENTIFIED BY 'test1234';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit

上面命令创建了一个owncloud数据库和一个owncloud用户(密码123456)。

4、配置Apache服务器

创建一个/etc/apache2/sites-available/owncloud.conf 内容如下

Alias /owncloud "/var/www/owncloud/"

<Directory /var/www/owncloud/>
  Options +FollowSymlinks
  AllowOverride All

 <IfModule mod_dav.c>
  Dav off
 </IfModule>

 SetEnv HOME /var/www/owncloud
 SetEnv HTTP_HOME /var/www/owncloud

</Directory>

然后创建一个符号链接/etc/apache2/sites-enabled:

ln -s /etc/apache2/sites-available/owncloud.conf /etc/apache2/sites-enabled/owncloud.conf

将您自己的Cloud目录的所有权暂时更改为HTTP用户

chown -R www-data:www-data /var/www/owncloud/

重启Apache使生效:

$ sudo systemctl restart apache2

5、完成安装

使用浏览器访问:http://本机IP地址/owncloud

创建owncloud管理员用户和配置数据库连接:

未分类

未分类

你也可以使用其它数据库。

安装完成:

未分类

在 Ubuntu 16.04 LTS 系统上安装 Python 3.6

Ubuntu 16.04 LTS 系统默认自带的是 Python 2.7 和 Python 3.5,有时候我们会需要用到 Python 3.6,但是官方的源里是没有 Python 3.6 的 ,今天就介绍一下如何安装 Python 3.6。

未分类

安装方法

1.配置软件仓库,因为 Python 3.6 新版没有发布到 Ubuntu 的正式仓库中,咱们通过第3方仓库来做。在命令行中输入:

sudo add-apt-repository ppa:jonathonf/python-3.6

如果有提示信息,回车即可。

2.检查系统软件包并安装 Python 3.6

sudo apt-get update 
sudo apt-get install python3.6

3.查看 Python 版本信息(现在在你的系统中已经有3个 Python 版本了)

python -V
python3 -V
python3.6 -V

4.通过上图我们看到,新安装的3.6版本需要输入 python3.6 才能使用,那能不能配置我只输入 python3 时就默认使用 3.6 版本呢,当然可以,执行以下命令:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1 
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2 
sudo update-alternatives --config python3

5.最后再确认一下:

python3 -V

6.配置完成后,之前有些 3.5 版本时候装的包可能会提示不存在了,此时重新安装一下即可。

参考文章

https://blog.csdn.net/lzzyok/article/details/77413968

ubuntu samba配置

1、设置root密码和更换vim

sudo passwd root
su root

apt-get remove vim-common
apt-get install vim

2、安装samba

apt-get install samba
service smb start

3、备份并修改配置

cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
vim /etc/samba/smb.conf 

4、在文件后追加配置内容

[helb]
comment = Share Folder require password
browseable = yes
path = /home/helb
create mask = 0777
directory mask = 0777
valid users = helb
force user = helb
force group = helb
public = yes
writable = yes
available = yes

5、重启samba

/etc/init.d/samba restart

6、创建共享目录(可跳过)

mkdir /home/share
chmod 777 /home/share

7、建立用

sudo groupadd share -g 6000
sudo useradd share -g 6000 -s /shin/nologin -d /dev/null
smbpasswd -a share

8、windows共享

运行栏:\IP

apt-get install openssh-server

添加用户:

useradd -r -m -s /bin/bash <name>
passwd <name>

解决ubuntu安装kubernetes时的网络问题

在kubernetes安装过程中,由于众所周知的原因,导致很多安装包等无法下载。本文记录了解决此问题的步骤。

1、安装shadowsocks

apt install shadowsocks -y

2、将shadowsocks配置写入配置文件

root@ubuntu:~# cat /etc/shadowsocks.json 
{
"server":"********",
"server_port":8086,
"local_address": "127.0.0.1",
"local_port":1080,
"password":"******",
"timeout":300,
"method":"aes-256-cfb",
"fast_open": false,
"workers": 1
}

3、安装privoxy,并编辑其配置文件

root@ubuntu:~# apt-get install privoxy

在/etc/privoxy/config文件末尾加上如下内容:

listen-address 127.0.0.1:9909
forward-socks5 / 127.0.0.1:1080 .

注意最后一行的点.

4、在一个session中启动shadowsocks,启动命令为

sslocal -c /etc/shadowsocks.json

在另一个session中,启动privoxy

systemctl start privoxy

5、这时候,如果哪个session需要下载kubernetes软件等,在当前session执行如下命令,加入环境变量

root@host1:~# export https_proxy="http://127.0.0.1:9909"
root@host1:~# export http_proxy="http://127.0.0.1:9909"

关于ubuntu下安装完PHP+Apache后,无法解析php的解决方案

根据 http://blog.csdn.net/renzhenhuai/article/details/12009595

安装完之后,html文件可以正常解析,php源码直接输出,不能被解析。

通常情况下是apache未加载php模块,通常情况下需要修改httpd.conf文件,但是在ubuntu下为apache2.conf文件

修改如下:

设置

LoadModule php5_module        /usr/lib/apache2/modules/libphp5.so
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

/usr/lib/apache2/modules/libphp5.so换成你的路径。

如果apache2.conf中该项配置,直接加入即可。

然后重启apache即可

sudo /etc/init.d/apache2 restart

Flask学习11:阿里云新手Flask + nginx + uwsgi + ubuntu的完整项目部署教程

Flask项目部署

web工作原理

客户端 < = > 服务器(nginx) < = > uWSGI < = > Python(Flask) < = > 数据库

nginx安装

源码安装、apt-get install …

sudo /usr/local/nginx/sbin/nginx -s reload

添加虚拟主机

1.在nginx的主配置文件最后一个大括号的上面添加:

include vhost/*.conf;

2.在conf/下新建文件夹,用于保存所有的虚拟主机配置文件

mkdir vhost

3.vhost目录下新建一个虚拟主机的配置文件(sudo vim idandan.vip.conf)

server{
    listen 80;
  server_name idandan.vip;
  location / {
      root html/blog;
      index index.html;
  }
}

# sudo vim idandan.vip.conf

4.在html目录下创建blog文件夹,在blog下新建index.html

Hello World

5.重启nginx(sudo /usr/local/nginx/sbin/nginx -s reload)

6.添加本地的域名解析,修改文件:(C:WindowsSystem32driversetchosts)

最后一行添加:139.196.81.238 idandan.vip

7.测试,在浏览器地址栏输入:idandan.vip

uWSGI

1.安装:pip3 install uwsgi

2.配置:

http:    # 采用http协议
socket:      # 采用socket协议
wsgi-file    # 将数据交给哪个模块
callable # 具体可调用的对象
chdir        # 指定uwsgi启动后的当前目录
daemonize    # 后台运行,需要指定一个日志文件


# blog.py文件

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
 return 'Hi'

if __name__ == '__main__':
 app.run()

3.简单实例

1.在blog目录下创建blog.py文件,里面写上flask启动代码用于测试
2.启动:
    sudo uwsgi --http host:port --wsgi-file blog.py --callable app

4.socket方式启动

1)nginx转发请求:

python 
server{ 
listen 80; 
server_name idandan.vip; 
location / { 
# root html/blog; 
# index index.html; 
include uwsgi_params; 
uwsgi_pass 127.0.0.1:5000; 
} 
} 

2)以socket方式启动

sudo uwsgi --socket 127.0.0.1:5000 --wsgi-file blog.py --callable app

3)将启动参数配置写进配置文件

[uwsgi]
socket  = 127.0.0.1:5000
wsgi-file = blog.py
callable = app

(blog/vim uwsgi.ini)
启动:sudo uwsgi wsgi.ini

静态文件处理

1.准备静态资源

python
1.在项目根目录下(blog)创建static目录
2.将图片拷贝到static下

2.配置nginx转发

# 添加一个location

location /static{
 # root html/blog;

# 或

alias html/blog/static;  # 两种方式都可以
}

ubuntu的ufw如何开放特定端口?

ufw是一个主机端的iptables类防火墙配置工具

安装:

sudo apt-get install ufw

开启,建议默认关闭所有外部访问

sudo ufw enable 
sudo ufw default deny

查看ufw现在已经开放的端口:

未分类

新增端口

sudo ufw allow 8080

删除端口

sudo ufw delete allow 8080

允许特定来源的ip地址访问

sudo ufw allow from 192.168.1.1

其他命令可以如此查看:

ubuntu@localhost:~$ sudo ufw –help
Usage: ufw COMMAND
Commands:

未分类

Ubuntu/Debian/CentOS系统安装Node.js软件教程

老蒋在”Ubuntu 18.04 LTS系统安装Node.js运行环境”文章中有分享到在Ubuntu环境中安装Node.js,且顺带一并安装NPM工具的教程。但是今天在给一个网友解决问题的时候,其需要在CentOS中安装Node.js,因为其默认的CentOS7版本Node.js低于5.0的,且某个软件是需要在高版本中运行。

所以,在这篇文章中,直接整理比较全的Ubuntu/Debian/CentOS系统,安装最新版本Node.js的教程,目前最新的版本是10.0,但是一般我们也不要太追求最新,我们可以安装8.0或者9.0差不多。

第一、Ubuntu

curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
apt-get install -y nodejs

第二、Debian

curl -sL https://deb.nodesource.com/setup_9.x | bash -
apt-get install -y nodejs

第三、CentOS

curl -sL https://rpm.nodesource.com/setup_9.x | bash -
yum install nodejs -y

安装过程都是差不多的。

未分类

看到如图,然后会指示我们下面执行哪个命令。

未分类

我们可以看到最新版本Node.JS和npm。

一次Ubuntu+nginx搭建wordpress的经历

一. 前言

开学之初,我发现Azure上有一个100刀的学生优惠。但在领取这个优惠之后,我却一直没有使用的机会,一是自己不会用,二是没有多余的时间。现在等来了放假,终于可以好好搞一搞了。:joy:

这次搭博客可谓是踩了不少坑:

  • 百度的教程基本上都是废的,只有谷歌的英文教程才是能用的,好气啊
  • apache2是真难用
  • php各个版本让人混乱

二. 接下来就是搭博客的过程

1、进入Azure的门户,添加一个Ubuntu的主机,学生优惠的主机为B1S,我使用的是公钥登陆,生成公钥的过程就不说了

未分类

2、在网络安全组的入站规则中添加443和80端口

3、接下来是连接主机。可以使用PuTTY或者是MobaXterm,输入ip和用户名,再加上私钥就可以连接了。

未分类

4、安装Nginx

sudo apt-get update 
sudo apt-get install nginx

安装完成后可以用以下命令操作nginx

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

打开网页看看是否有这样的页面出现,如果有的话就可以进行下一步操作了

未分类

5、安装MariaDB

sudo apt-get install mariadb-server mariadb-client

对于Ubuntu 16.04有以下命令

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

输入

sudo mysql_secure_installation

然后进行以下操作

Enter current password for root (enter for none): Just press the Enter
Set root password? [Y/n]: Y
New password: Enter password
Re-enter new password: Repeat password
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]:  Y
Reload privilege tables now? [Y/n]:  Y

测试以下密码是否能登陆

sudo mysql -u root -p

6、安装PHP7.2 / PHP-FPM

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php7.2-fpm php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-soap php7.2-gd php7.2-xml php7.2-intl php7.2-mysql php7.2-cli php7.2-zip php7.2-curl

7、重启nginx和php

sudo systemctl restart nginx.service
sudo systemctl restart php7.2-fpm.service

8、创建WordPress的数据库

sudo mysql -u root -p
    CREATE DATABASE yourDBname;
    CREATE USER 'yourDBusername'@'localhost' IDENTIFIED BY 'new_password_here';
    GRANT ALL ON yourDBname.* TO 'yourDBusername'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;
    FLUSH PRIVILEGES;
    EXIT;

9、安装wordpress

cd /tmp && wget https://wordpress.org/latest.tar.gz
tar -zxvf latest.tar.gz
sudo mv wordpress /var/www/html/wordpress
sudo chown -R www-data:www-data /var/www/html/wordpress/
sudo chmod -R 755 /var/www/html/wordpress/

10、配置http服务

我用的是vim

sudo vim /etc/nginx/sites-available/wordpress

找到这个东西的位置, 将example.com 换为自己的网址

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/wordpress;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

     client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}
PHPCopy

11、开启网站

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/

12、对网站进行配置

1)先是将示例配置文件复制一份

sudo mv /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php

2)获取secure key,打开下面的网站。

https://api.wordpress.org/secret-key/1.1/salt/

3)再进行配置

sudo vim /var/www/html/wordpress/wp-config.php
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'yourDBname');

/** MySQL database username */
define('DB_USER', 'yourDBusername');

/** MySQL database password */
define('DB_PASSWORD', 'user_password_here');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
PHPCopy

4)再找到secure key的相应位置,将自己获取到的key放进去

不要直接复制!!!
define('AUTH_KEY',         'zP}sLq>K[Q}6+-QS !Fc,wBD%373RYo.C<c=%lnE.!xY:kloSyJ@qIQ`2iVy{Sd[');
define('SECURE_AUTH_KEY',  'Zrw|4{)0]tE}bcHa8@nwM]SC:17b}-IDd)Z%1d}a}cMqI}h@|Kf>oLRL`pXdySLN');
define('LOGGED_IN_KEY',    ',Nl,:NNP+R)%Y$gjpOI8~X?<;?}eTvaI4;l?T@S}V]BwsNT{*e!y.YB^#X=QI@qx');
define('NONCE_KEY',        'n2Z2|H>r|-C-y(yzG*-1btopL;N|);8lZQvIP( Dhy}xtc2vs#(2#ec,C%])Bf~[');
define('AUTH_SALT',        '>`nL:waCX!k~-(9!A2.?PJpKd&,D[r}w(i;~Ck^q1M`YCWF2.fTR%;V7_+}QI76 ');
define('SECURE_AUTH_SALT', 'nshMJqJv2Z_EVt|Wtw?qJCI6N&>^{L-.ig(]Pc.3M!*,sR8i[WZg.5pQP1-t8a$z');
define('LOGGED_IN_SALT',   '-nDGM,|-PWtNy02}0r^,rsJlC5#si%W{@6W]<<#PR}T)9Izv7Rc[]HKk`yk)1v38');
define('NONCE_SALT',       'QzZF]GjCkgNC;,m&jc=~gX-gWo^oUj{W]N+s5-kG?Fq?KQ^ESNhI<+xa-m+;e|?(');

13、配置ssl

1)安装 Let’s Encrypt Client

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

2)获取免费的ssl证书

将example.com 全部更换为自己的域名

sudo certbot --nginx -m admin@example.com -d example.com -d www.example.com

3)接下来的东西按照步骤走

Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A


Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: Y


Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

最后一个一定要选择2

4)重新对配置文件进行配置

sudo nano /etc/nginx/sites-available/wordpress
server {
    listen 80;
    listen [::]:80;
    root /var/www/html/wordpress;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

     client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }

  listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    # Redirect non-https traffic to https
    # if ($scheme != "https") {
    #     return 301 https://$host$request_uri;
    # } # managed by Certbot

}

三、大功告成

直接打开你的网址,看看是否出现这个页面,出现了就说明成功啦

未分类