美文网首页Linux学习与应用技巧
阿里云服务器(学生优惠)使用nginx

阿里云服务器(学生优惠)使用nginx

作者: 小明的数据分析笔记本 | 来源:发表于2020-03-09 13:18 被阅读0次
参考资料

B站视频 尚硅谷2019nginx教程
https://www.bilibili.com/video/av73766937?p=8
Centos 7下安装配置Nginx

安装nginx

我首先是使用yum命令来安装的

yum install nginx
安装到哪了?
which nginx

/usr/sbin/nginx
nginx的相关文件在/etc/nginx/目录下,配置文件是nginx.conf,设置反向代理需要更改这个文件

但是发现这个配置文件和视频教程里的不一样,自己就不知道如何修改配置文件了。先不管了,还是先删掉吧,删除yum安装软件的命令是 yum remove nginx

安装和教程中一样的版本nginx-1.12.2

下载链接 http://nginx.org/download/nginx-1.12.2.tar.gz
我把它放到我的/home文件夹下
解压——编译——安装

tar -xzvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure
make && make install

这样就和视频教程中一样了
在/usr/local/目录下多出来一个nginx目录
这个目录下有一个sbin文件夹存放着执行程序
还有一个conf文件夹存放着配置文件
用cat命令查看配置文件内容

cat /usr/local/nginx/conf/nginx.conf

内容是和视频中一样的

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
查看服务器开放的端口
firewall-cmd --list-all

但是我的提示我 FirewallD is not running,和视频教程里的不太一样

增加开放的端口

虽然暂时用不着,先记录相关的命令
开放 8080端口

firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload

端口是个啥东西?

这时候找到了文章 https://yq.aliyun.com/articles/699966 Centos 7下安装配置Nginx
文章中提到

如果你的服务器是阿里云ECS,你还可以通过控制台安全组,打开80和443端口,或者其他自定义端口。
具体操作路径: 阿里云ECS服务器 -》 安全组 -》 配置规则 -》 安全组规则 -》 入方向 -》 添加安全组规则
端口范围: 比如你要打开80端口,这里就填写 80/80 。
优先级: 优先级可选范围为1-100,默认值为1,即最高优先级。

按照这个方法我分别设置了80,443和8000端口

然后启动nginx

/usr/local/nginx/sbin/nginx

查看进程

ps -ef | grep nginx

在自己浏览器电脑开启服务器的公网ip,终于成功了,搞这个已经花费2天时间了!

设置反向代理

使用vim命令修改配置文件

image.png

1处的localhost改为自己服务器的公网IP
2处添加内容启动Django项目的IP

重启nginx

/usr/local/nginx/sbin/nginx -s reload
服务器安装Miniconda

下载链接
https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
安装

bash Miniconda3-latest-Linux-x86_64.sh

一路默认就可以了
创建名称为practice的虚拟环境

conda create -n practice python=3.6

启动虚拟环境

conda activate practice

安装Django 2.2

pip install Django==2.2

创建django项目

django-admin startproject practice_django

启动服务器

cd practice_django
python manage.py runserver

在自己电脑上输入公网IP,就可以看到最终结果了。

欢迎大家关注我的公众号
小明的数据分析笔记本

公众号二维码.jpg

中国加油!武汉加油!

相关文章

网友评论

    本文标题:阿里云服务器(学生优惠)使用nginx

    本文链接:https://www.haomeiwen.com/subject/izkudhtx.html