欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ubuntu20.04安装apache并配置Flask的wsgi程序

程序员文章站 2022-07-14 20:12:37
...
  1. 安装apache,
    运行下面的命令来更新软件包索引,并且安装 Apache:
    
    sudo apt update
    sudo apt install apache2
    当安装过程完成,Apache 服务将会被自动启动。
    
    你可以通过输入下面的命令,验证 Apache 是否正在运行:
    
    sudo systemctl status apache2
    
    写完配置文件需要**
    sudo a2ensite example.com
    测试配置文件,是否有任何语法错误:
    
    sudo apachectl configtest
    如果没有任何错误,你将会看到下面的输出:
    
    Syntax OK
    重启 Apache 服务,使修改生效:
    
    sudo systemctl restart apache2
    最终,想要验证一切都按照预期工作,在你的浏览器中打开http://example.com
    https://zhuanlan.zhihu.com/p/139543896
  2. 安装Flask,wsgi-py3,编写app
    pip install flask
    apt-get install libapache2-mod-wsgi-py3
    
    [email protected]:~$ vi  /var/www/ezdial.cn/flask/app.py 
    
    from flask import Flask,redirect
    app = Flask(__name__)
    app.debug=True
    @app.route("/")
    def index():
       return redirect("/static", code=302)
    @app.route("/data")
    def hello():
        return "Hello, Flask!"
    @app.route("/json")
    def json():
        return "{'666edf':'ddd'}"
    if __name__ == "__main__":
        app.run()
    
    [email protected]:~$ vi  /var/www/ezdial.cn/flask/flaskw.wsgi 
    
    #!/usr/bin/python
      
    import sys
    sys.path.insert(0,"/var/www/ezdial.cn/flask/")
    from app  import app as application

  3. 配置mod-wsgi ,
    vi /etc/apache2/sites-available/ezdial.cn.conf 
    <VirtualHost *:80>
        ServerName ezdial.cn
        ServerAlias www.ezdial.cn
        ServerAdmin [email protected]
    #    DocumentRoot /var/www/ezdial.cn/public_html
        WSGIDaemonProcess flaskw  user=ubuntu   group=www-data  threads=5
        WSGIScriptAlias /  /var/www/ezdial.cn/flask/flaskw.wsgi
        WSGIProcessGroup flaskw
        WSGIScriptReloading On
        WSGIApplicationGroup flaskw
        Alias  /static  /var/www/ezdial.cn/public_html
        <Directory /var/www/ezdial.cn/public_html>
    WSGIScriptReloading On
            Options -Indexes +FollowSymLinks
            AllowOverride All
    
        </Directory>
    
        ErrorLog /var/www/ezdial.cn/log/error.log
        CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
    </VirtualHost>
    https://www.bogotobogo.com/python/Flask/Python_Flask_HelloWorld_App_with_Apache_WSGI_Ubuntu14.phphttps://dormousehole.readthedocs.io/en/latest/deploying/mod_wsgi.html
  4. 运行中的错误,用户目录权限问题。
    想要避免权限问题,修改域名根文件夹的用户归属为apache 用户(www-data):
    sudo usermod -a -G www-data ubuntu
    sudo chown -R ubuntu:www-data /var/www/ezdial.cn
    
    递归添加组写入权限:
    
    sudo chmod 2775 /var/www
    find /var/www -type d -exec sudo chmod 2775 {} \;
    find /var/www -type f -exec sudo chmod 0664 {} \;
    
    重启Apache Web服务器, 让新的组和权限生效:
    
    sudo systemctl restart  apache2
    ubuntu  用户 (以及 apache 组的任何未来成员) 可以在 www-data 文档根目录中添加、删除和编辑文件。
    后续有新的用户ftpuser要更新/var/www/下的内容, 可将它加入组apache中:
    
    sudo usermod -a -G www-data ftpuser
    
    或者使用以下命令传输。
    scp -i ~/ubun***.pem  ../boots3/dist/js/users.json  [email protected]:/var/www/ezdial.cn/public_html/

    EC2服务器Apache下 /var/www/目录权限的配置http://www.yebaochen.com/2018/01/17/ec2-webdir-permission-conf

5.启用sftp服务

https://linuxconfig.org/how-to-setup-sftp-server-on-ubuntu-20-04-focal-fossa-linux

SFTP requires SSH, so if SSH server is not already installed on your system, install it with the following command:
#$ sudo apt install ssh。 
#Once SSH is installed, we need to make some changes to the SSHD configuration #file. Use nano or your favorite text editor to open it:
$ sudo nano /etc/ssh/sshd_config
#Scroll to the bottom of the file and add the following 5 lines at the very end:

Match group sftp
ChrootDirectory /home
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp


$ sudo systemctl restart ssh

asswordAuthentication yes.

edit /etc/ssh/sshd_config and change or uncomment it so it shows PasswordAuthentication yes. Once that is done restart sshd service ssh restart