In some small devices running Ubuntu system, the need for rapid development and adjustment of the project, often use SSH for remote development and testing, so that you can avoid the traditional packaging update processing, to be able to quickly test the specific content on the actual environment. In addition, because the system device often need to reboot after the application can be retained to work, so also need to be set in the Ubuntu system automatically start the service processing. This accompanying article introduces the use of VSCode + SSH development and deployment of nicegui web applications on Ubuntu small devices, and set the system to boot automatically start the application.
1, the use of VSCode + SSH development and deployment of nicegui Web applications
VSCode is very powerful, the use of extensions to achieve richer functionality is one of its important features, to use remote development, we need to install the Remote-SSH extension to realize the direct opening of the server-side files for development, compilation and so on.
First we find the Microsoft Remote-SSH extension plugin on the Extensions page and install it for VSCode.
If you need to use some of the processing of FTP, you can also install an SFTP to work with VSCode to realize the file upload and download, but even if you don't install SFTP, it doesn't matter.
Open the configuration in the VSCode command panel
Specify the Remote-SSH configuration file in the Configuration Parameters screen as follows.
Since I am developing on Windows, the configuration is shown below. If it's macOS or other systems, it's different.
After installing Remote-SSH, there will be an SSH item in the left navigation, we add our server's IP and username information to connect to the server.
The general format is shown below. replace it with your own IP and login system user:
Host 192.168.1.103 HostName 192.168.1.103 User root
After initiating a connection to the server, you will be prompted for a user password:
After success we open the server's code directory as shown below.
Then the development is the same as the local file, we write code on it, compile and run are based on the server environment, if the lack of module references, remember to use pip install to install the dependencies can be installed, the installation is also given to the server environment for the installation.
The handling of SSH control commands for the server we can open through the bash panel in VScode.
The command window is created as shown below.
We can then do related command operations based on this, similar to local command processing.
It's easy to work with server files on VScode. You can drag local files directly to the server directory, or you can download code files locally from the server directory.
2, set Ubuntu system boot automatically start the application
Sometimes, after deploying a project on a real environment, we are hoping that every time we reboot, the related application can start automatically to avoid the tediousness of manual intervention every time.
1) Write a startup script
First we create a script on our project code for starting the service reference, e.g. create the file: start_my_backend.sh
Then fill in the content, which is pretty much the same command we use to actually run the python project.
#!/bin/bash
# Start the Python backend program
/usr/bin/python /root/test/src/nicegui/modularization/
Make sure the paths are correct and point to your Python interpreter and Python script files.
Note that the beginning of the file #! /bin/bash is the feature that identifies it as a Bash command, as shown by the following file in the directory.
Grant script execution privileges:
chmod +x /root/test/src/start_my_backend.sh
2) Create systemd service file
Then create a service startup process on the /etc/systemd/system/ directory named: my_backend.service
Add the following:
[Unit] Description=My Python Backend Service After= [Service] ExecStart=/root/test/src/start_my_backend.sh Restart=always User=root WorkingDirectory=/root/test/src Environment="PATH=/usr/bin" [Install] WantedBy=
Explanation:
-
ExecStart
Specifies the path to the script to be executed. -
User
Specifies which user runs the service. -
Restart=always
Indicates an automatic restart when the service crashes. -
WorkingDirectory
is the working directory of the script.
Save and exit the editor (pressCtrl+O
after thatCtrl+X
)。
3) Enable and start the service
- reload
systemd
Configuration:
sudo systemctl daemon-reload
- Enable the service so that it starts on boot:
sudo systemctl enable my_backend.service
- Start the service immediately:
sudo systemctl start my_backend.service
- Check the status of the service to verify that it is running properly:
sudo systemctl status my_backend.service
- Log View
If you want to view the service's runtime log, you can use the following command:
journalctl -u my_backend.service
In this way, each time the system boots, themy_backend
The service will then automatically start and run your Python backend program.
Finally, check to see if the server-side port for the startup project is working.
In this way, writing code for development or debugging directly from the server and starting the server directly and automatically realizes our regular development processing.