- 安装apache,
运行下面的命令来更新软件包索引,并且安装 Apache: sudo apt update sudo apt install apache2 当安装过程完成,Apache 服务将会被自动启动。 你可以通过输入下面的命令,验证 Apache 是否正在运行: sudo systemctl status apache2
https://zhuanlan.zhihu.com/p/139543896写完配置文件需要激活 sudo a2ensite example.com 测试配置文件,是否有任何语法错误: sudo apachectl configtest 如果没有任何错误,你将会看到下面的输出: Syntax OK 重启 Apache 服务,使修改生效: sudo systemctl restart apache2 最终,想要验证一切都按照预期工作,在你的浏览器中打开http://example.com
- 安装Flask,wsgi-py3,编写app
pip install flask apt-get install libapache2-mod-wsgi-py3 ubuntu@ip-172-31-39-57:~$ 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() ubuntu@ip-172-31-39-57:~$ 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
- 配置mod-wsgi ,
https://www.bogotobogo.com/python/Flask/Python_Flask_HelloWorld_App_with_Apache_WSGI_Ubuntu14.php,https://dormousehole.readthedocs.io/en/latest/deploying/mod_wsgi.htmlvi /etc/apache2/sites-available/ezdial.cn.conf <VirtualHost *:80> ServerName ezdial.cn ServerAlias www.ezdial.cn ServerAdmin webmaster@example.com # 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>
- 避免权限问题,Apache和mod-wsgi及后面的sftp对目录权限及用户权限的要求,
所用人都用了一样的用户,这样我可以操作同一个目录。并分配755权限。因为我用起来不熟悉,所以就近这样解决。 将ubuntu 分配给www-data。并由他启动wsgi。编写app的python文件。 最后一节是分配他使用sftp。同时上级目录要有root的所有者据说这样sftp才启动成功。 修改域名根文件夹的用户归属为apache 用户(www-data):运行中的错误,用户目录权限问题。 最后Ubuntu在www-data里看似没啥意义,其实就是为了组用户可以修改www目录的文件。但是sftp文件就不能设置组用户www-data了。因为这样就禁止他ssh去登录了。 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 ubuntu@www.ezdial.cn:/var/www/ezdial.cn/public_html/
EC2服务器Apache下 /var/www/目录权限的配置http://www.yebaochen.com/2018/01/17/ec2-webdir-permission-conf
5.启用sftp服务并且修改配置文件,更改ftp目录和使用用户权限。
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.
编辑 /etc/ssh/sshd_config
PasswordAuthentication yes
.
Match Group yourwwwgroup(www-data)
service ssh restart
目录权限设置上要遵循2点:
ChrootDirectory设置的目录权限及其所有的上级文件夹权限,属主和属组必须是root;
ChrootDirectory设置的目录权限及其所有的上级文件夹权限,只有属主能拥有写权限,权限最大设置只能是755。
属主应该是www-data。这个apache所用的组。另外主机ssh登录所用的ubuntu应该归入www-data这个组。并且分配给一个登录密码。这样在ssh中,和在sftp中使用的都是一个用户,对web目录有一样的操作权限。https://blog.csdn.net/weixin_30945039/article/details/96755478
最后又踩了一个大坑。为了使ssh和sftp都能用
This setting
Match Group sftpusers
...
ForceCommand internal-sftp
restricts access to sftp for the users in the group sftpusers
. Either remove your user from the sftpusers
group or remove that forcecommand option.
必须二选一。而我的ubuntu是ssh唯一指定用户。所以当我让他去sftp以后。由于forccommand internal-sftp存在,无法登录ssh了。好在默认EC2打开了一个控制台代理在ubuntu的instance。
上去以后更改了设定,最后能用了了。我把match Group,改回了 match user www-data。终于达到目的了。