简体   繁体   English

如何仅通过命令行启动服务器并在虚拟机中运行?

[英]How do I get a server up and running in a virtual machine through only command line?

I wrote a server application in Python with Flask and now I would like to get it up and running on a virtual machine I have set up. 我用Flask用Python编写了一个服务器应用程序,现在我想在已设置的虚拟机上启动并运行它。 Thus, I would really appreciate guidance in two areas. 因此,我非常感谢在两个方面的指导。

  1. How do I get a server setup so that it is perpetually running, and other computers can access it? 如何获得服务器设置,使其永久运行,其他计算机可以访问它? The computers can be in the same network so I don't have to worry about a domain name or anything. 这些计算机可以位于同一网络中,因此我不必担心域名或其他任何内容。 I am just looking for multiple devices to be able to access it. 我只是在寻找可以访问它的多个设备。 I am currently able to run the server on my local machine and everything works just fine. 我目前能够在本地计算机上运行服务器,并且一切正常。

  2. I have my virtual linux machine set up remotely, so I SSH into it and do everything from command line, but I am a bit lost as to how to do the aforementioned stuff from the command line. 我的虚拟linux机器是远程设置的,因此我通过SSH进入它并从命令行执行所有操作,但是我对如何从命令行执行上述操作有些迷惑。

Any guidance/help is much appreciated! 任何指导/帮助都非常感谢! The web-searching I have done hasn't pointed me in the right direction. 我进行的网络搜索没有为我指明正确的方向。 I apologize if any of my terminology was off (if so, please feel free to correct me so I learn!). 如果我的任何术语都不对,我深表歉意(如果是的话,请随时纠正我,以便我学习!)。 Thank you! 谢谢!

Use systemd on Ubuntu, /etc/systemd/system , for a simple setup (probably not ideal for a production setup though). 在Ubuntu上/etc/systemd/system上使用systemd进行简单的设置(尽管可能不适用于生产设置)。

I do this sometimes for Python Flask app that I'm prototyping. 有时我会为我正在制作原型的Python Flask应用执行此操作。 First, put your application code in /opt/my-app . 首先,将您的应用程序代码放在/opt/my-app I usually just cd /opt and git clone a repo there. 我通常只是cd /optgit clone在那儿git clone一个仓库。 Then, create a file called /etc/systemd/system/my-app.service . 然后,创建一个名为/etc/systemd/system/my-app.service In that file, add the following: 在该文件中,添加以下内容:

[Unit]
Description=My App daemon
After=network.target postgresql.service 
Wants=postgresql.service 

[Service]
EnvironmentFile=/etc/sysconfig/my-app
WorkingDirectory=/opt/my-app/ # <- this is where your app lives
User=root
Group=root
Type=simple
ExecStart=/usr/bin/python server.py # <- this starts your app
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target

Next, paste any environment variables you have into a file called /etc/sysconfig/my-app like: 接下来,将您拥有的所有环境变量粘贴到名为/etc/sysconfig/my-app如下所示:

DB_HOST=localhost
DB_USER=postgres 
DB_PASSWORD=postgres 
DB_NAME=postgres

Then you can do: 然后,您可以执行以下操作:

service my-app start
service my-app stop
service my-app restart

and then you can hit the app running on the servers IP and port (just like if you ran python app.py or python server.py . To check the logs for your daemon process, if it doesn't seem to work out, you can run: 然后您可以运行在服务器IP和端口上运行的应用程序(就像您运行python app.pypython server.py 。要检查守护进程的日志,如果它似乎python app.py问题,您可以能跑:

journalctl -u my-app -e 

In production, I'm not sure this is the best setup, probably better to look into something like ngnix. 在生产中,我不确定这是最好的设置,可能更适合研究ngnix之类的东西。 But I do this for prototypes all the time and it's pretty great. 但是我一直在为原型做这件事,这非常好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM