Azure 免费虚拟机 Centos8 安装 Nginx, PostgreSQL, Django

2026-05-17 07:37:54105 阅读量

很多人不知道,除了 AWS, Azure也提供了一些免费的虚拟机,而且相对于 AWS 对中国人不友好的界面和翻译水平,个人认为 Azure 更加对中国人用户友好。

相关服务:日本服务器

更重要的是,按照最新的报告,Azure 已经是仅次于 AWS 的云服务提供商了,远远把剩下的第二梯队甩在后面,还是比较让人放心的。

当然,最吸引人的是,Azure 的费用,按照 Azure 自己的比较,比AWS要便宜50%以上。

1. 申请

https://portal.azure.com/#blade/Microsoft_Azure_Billing/FreeServicesBlade

以上是申请网址,可以选择前两个的 WIndows 或者 Linux 系统,因为我要使用 Centos ,所以我选择 Linux。

接下来,会来到服务器的配置界面,下面是最新的产品配置截图,我原来想选择日本的服务器,但是可选选项里面只有东南亚和北美的,所以我这里选择了更近的东南亚。

管理员账户这里,我选择了密码方式,设定一个用户名和密码,用来做之后的 SSH 登录用。

随后的步骤,基本上按照 Azure 给的默认值设置即可,入口记得要选择 SSH(22)端口,否则我们没有办法远程访问。

大概5分钟后,Azure 就可以给我们一个服务器,然后直接启动服务器即可。

2. 安装 postgreSQL

网络上有很多安装 postgreSQL 的博文,但是很多都已经过时,或者是系统版本不匹配。本人也尝试了几个博文里的方法,但是总是遇到各种各样的问题,最后还是直奔官网。

https://www.postgresql.org/download/

选择 Linux -> RedHat/Centos 

然后选择我们的 Centos 8 版本,post'g'reSQL 我选了比较经典的 9.6,具体配置可以参考下图。

这时候, postgreSQL 会给我们一个安装的方法。

sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo dnf -qy module disable postgresql
sudo dnf install -y postgresql96-server
sudo /usr/pgsql-9.6/bin/postgresql96-setup initdb
sudo systemctl enable postgresql-9.6
sudo systemctl start postgresql-9.6

按照系统给的配置,直接安装即可,我这边没有出现问题。

接下来,需要对 postgres 进行一些配置,下面是我这边的 postgreSQL 的配置路径

/var/lib/pgsql/9.6/data/pg_hba.conf

然后将 postgres 的验证方式修改一下:

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

然后,重新启动 postgreSQL

sudo systemctl start postgresql-9.6

然后可以不通过密码进入 postgres 

psql -h localhost -U postgres

然后输入以下命令

ALTER USER postgres PASSWORD '新密码';

注意密码外面的引号

然后将 postgresql 的配置文件修改回原来的 md5 方法

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

重新启动 postgresql

systemctl restart postgresql-9.6

这时候,可以使用新密码进入数据库了

3. 安装 nginx

参考之前我写的博文 https://blog.csdn.net/xuelangqingkong/article/details/103347695

首先, centos7 安装 nginx 的说明方法可以在 nginx 的官网找到

https://nginx.org/en/linux_packages.html#RHEL-CentOS

下面简单说下步骤:

直接以 root 身份安装或者 sudo

3.1. nginx的依赖包

yum install yum-utils

3.2 配置 yum 库

新建一个文件 /etc/yum.repos.d/nginx.repo

[nginx-stable]

name=nginx stable repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=1

enabled=1

gpgkey=https://nginx.org/keys/nginx_signing.key

module_hotfixes=true

[nginx-mainline]

name=nginx mainline repo

baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/

gpgcheck=1

enabled=0

gpgkey=https://nginx.org/keys/nginx_signing.key

module_hotfixes=true

默认情况下,我们安装稳定版本即可,这里安装完毕是 1.6.1

3.3 安装

yum install nginx

3.4 启动 并 设置为 开机启动

systemctl start nginx
systemctl enable nginx

然后打开 http://IP:80, 这里的IP为你的外网IP地址

同时,有一个点可以注意下,如果自己的网页放在了用户 www 的目录下面,则应该在 /etc/nginx/nginx.conf 中修改

user  www;
worker_processes  1;

然后

nginx -s reload 

然后看看效果 

4. 安装 gcc, python3-devel, python, virtualenv, Django

Azure 的 centos 默认是没有安装 C 编译器的,而 C编译器又是 python, uwsgi 的基础,所以我们需要安装 python 的一些底层依赖包。

sudo yum -y install python3-devel
sudo yum -y install gcc

Azure 的 centos 已经默认安装了 python3, pip3,我们需要做的只是将 python3 和 pip 设为默认即可。

sudo ln -fs /bin/python3 /usr/bin/python
sudo ln -fs /bin/pip3 /usr/bin/pip

还有 python 的一些依赖包

pip install python-dateutil 

5. Centos 8 中 nginx 遇到的特殊问题

在 Centos8 中进行操作的时候,发现了一个新的问题。

[emerg] 54621#54621: open() "/etc/nginx/conf.d/mysite_nginx.conf" failed (13: Permission denied) in /etc/nginx/nginx.conf:31

在网上翻阅了相关的问题之后,确认这是 Centos 系统中的 SELinux 问题,识别方法很简单,即输入

sestatus

然后结果如果对应的 SELinux status 是 enabled 或者 Permissive ,说明是 SELinux 的问题。

这时候一劳永逸的办法是直接编辑 /etc/selinux/config

vim /etc/selinux/config

将 SELinux 设置为 disabled 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

6. 实现 nginx - uwsgi - django 项目直接的对接

在之前的文章中,已经讲到了如何在 Centos 中搭建 nginx - uwsgi - django 项目的实现。

https://blog.csdn.net/xuelangqingkong/article/details/104825507

在这中间,遇到了几个问题,花了一些时间,下面着重讲一下:

6.1. uwsgi 的安装问题

在 root 权限下安装 uwsgi 失败,尝试了很多方法都没有办法成功,这时候只能退而求其次,既然在virtualenv 的虚拟环境下已经安装了 uwsgi,那么就使用虚拟环境下的 uwsgi 作为系统的 uwsgi 使用吧!

比方说,这里我的虚拟环境下的uwsgi 的路径是

/home/www/django3/bin/uwsgi

那么接下来,我就直接用这个路径启动 django 项目

/home/www/django3/bin/uwsgi --emperor /etc/uwsgi/vassals --uid 用户 --gid 用户组 --daemonize /var/log/uwsgi-emperor.log

当然,这里的用户名和用户组名,使用自己的Django项目所在的路径的用户名即可,至于用户组,用下面的命令获得:

groups 用户名

6.2 rc-local 启动问题

首先,我们编辑 /etc/rc.local 文件

vim /etc/rc.local

然后,添加上面的一句话

/home/www/django3/bin/uwsgi --emperor /etc/uwsgi/vassals --uid 用户 --gid 用户组 --daemonize /var/log/uwsgi-emperor.log

需要提到的一点 /etc/rc.local 在centos7的版本里是只读的,所以我们需要给它加入一个可执行的权限,同时 /etc/rc.local 的服务名为 rc-local,所以我们这里将其设定为自启动

chmod +x /etc/rc.d/rc.local
systemctl enable rc-local

这里,我们发现了一些问题

The unit files have no installation config (WantedBy, RequiredBy, Also, Alias
settings in the [Install] section, and DefaultInstance for template units).
This means they are not meant to be enabled using systemctl.
Possible reasons for having this kind of units are:
1) A unit may be statically enabled by being symlinked from another unit's
   .wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
   a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
   D-Bus, udev, scripted systemctl call, ...).
4) In case of template units, the unit is meant to be enabled with some
   instance name specified.

这个是以前没有遇到过的问题,简而言之,就是 rc-local 不可以作为服务使用,接下来我使用了网络上的一个方法,即自己将 rc-local 作为服务,方法是

nano /etc/systemd/system/rc-local.service

[Unit]
 Description=/etc/rc.local Compatibility
 ConditionPathExists=/etc/rc.local

[Service]
 Type=forking
 ExecStart=/etc/rc.local start
 TimeoutSec=0
 StandardOutput=tty
 RemainAfterExit=yes
 SysVStartPriority=99

[Install]
 WantedBy=multi-user.target

然后,再使用

systemctl enable rc-local
systemctl start rc-local

然后,自己去网站上测试一下即可。

本文地址:https://www.limyvps.com/news/26_41980.html