Rust is a programming language known for high performance, memory security without garbage collectors, and its ability to build fast and efficient applications. One of the things that makes Rust more popular is the ease of use to build web applications with various available frameworks, such as ACTIX Web and Rocket.
Rust also has a cargo, build manager and excellent package, which facilitates the management of dependencies and application compilation. By using crates.io, Rust has a very large library ecosystem, allowing us to build complex applications with a little code.
In this tutorial, we will create a simple web application with Rust, compile it, and then deploy it using Nginx as a Reverse Proxy.
Install Dependencies and Rust
Update and install dependencies:
sudo apt update sudo apt install build -isntial -y
|
sudo appropriate renew sudo appropriate install build–important –Y |
Install Rust:
Curl -ssf | SH
|
curly https://sh.Rustup.RS -SSF | SH |
Rust and Cargo Installation Verification:
Rustup -Rustc Version -Version Cargo -Version
|
Rustup –version Rustc –version load –version |
The resulting response:
RUSTC 1.82.0 (F6E511EEC 2024-10-15) Cargo 1.82.0 (8F40FC59F 2024-08-21)
|
Rustc 1.82.0 (F6E511EEC 2024–10–15) load 1.82.0 (8F40FC59F 2024–08–21) |
Make a rust app
Creating a directory for the Rust project:
new cargo cd cd cd rustweb
|
load new Rustweb CD Rustweb |
Open files SRC/Main.RS:
Replace the contents with the following code:
Use std :: io :: prelude ::*; Use std :: net :: {tclistener, tcSptream}; fn handle_connection (mut stream: tcSptream) {let mut buffer = [0; 512]; stream.read (& mut buffer) .unwrap (); Let the response = “http/1.1 200 ok \ r \ ncontent-tipe: text/html; charset = utf-8 \ r \ n‘ r \ n
Hello, World! “; stream.write (response.as_bytes ()). unwrap (); stream.flush (). unwrap ();} fn play () {// bind server to localhost: 8000 let list = tclistener :: Bind (” 127.0.0.1:8000 “). Unwrap (); println! listener.incoming () {let stream = stream.unwrap ();|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
use STD::IO::Prelude::*; use STD::clean::{TCPLPLATIS, TcSptream}; FN handle_connection(Mut small river: TcSptream) { let Mut buffer = [0; 512]; small river.read(&Mut buffer).open(); let response = “Http/1.1 200 ok \ r \ ncontent-type: text/html; charset = utf-8 \ r \ n \ r \ n \ n Hello, World! “; small river.write(response.as_bytes()).open(); small river.water().open(); } FN main() { // bind server to localhost: 8000 let listener = TCPLPLATIS::tie(“127.0.0.1:8000”).open(); PrintlnLai(“The server runs at http://127.0.0.1:8000”); For small river in the listener.enter() { let small river = small river.open(); handle_connection(small river); } } |
Application Compile:
The binary application will be on Target/release/Rustweb
Make service
Creating File Service:
SUDO NANO/ETC/SYSYSTEMD/SYSYSTEM/RUSTWEB.Service
|
sudo Nano /etc./SystemD/system/Rustweb.serve |
SUBMIT:
Description = rust web application after = network.target
[Service]
User = youughuser group = Youurizer workingdirectory =/home/youghoser/rustweb execstart =/home/youghoser/rustweb/target/release/rustweb restart = always
[Install]
WANTEDBY = MULTI-USER. TARGET
|
[Unit] Information=Rust Web Application After=network.target [Service] User=Youurizer Group=Youurizer WorkingDirectory=/House/Youurizer/Rustweb Execstart=/House/Youurizer/Rustweb/target/release/Rustweb Restart=always [Install] Searching for=multi–user.target |
Activate service:
Sudo SystemCTL Daemon-Reload Sudo Systemctl Activating Rustweb Sudo SystemCtl Starting Rustweb Sudo SystemCtl Status Rustweb
|
sudo SystemCTL Daemon–Re -load sudo SystemCTL possible Rustweb sudo SystemCTL beginning Rustweb sudo SystemCTL status Rustweb |
Nginx configuration
Install Nginx:
sudo apt install nginx -y
|
sudo appropriate install Nginx –Y |
Creating Nginx Configuration Files:
SUDO NANO/ETC/NGINX/CONF.D/RUSTWEB.CONF
|
sudo Nano /etc./Nginx/conf.D/Rustweb.conf |
The contents of the file:
server {listen 80; server_name Example.com; Location / {proxy_pass proxy_set_header host $ host; proxy_set_header x-real-ip $ remote_addr; proxy_set_header x-forewarded-for $ proxy_add_x_forwarded_for; }}
|
server { listen 80; server_name example.com; Location / { Proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X–Real–I P $Remote_addr; proxy_set_header X–Continued–For $proxy_add_x_forwarded_for; } } |
Restart nginx:
Sudo systemctl restart nginx
|
sudo SystemCTL Restart Nginx |
Application test
Open the browser and visit the address:
The result is the text “Hello, World!”.
Good luck 🙂
Game Center
Game News
Review Film
Rumus Matematika
Anime Batch
Berita Terkini
Berita Terkini
Berita Terkini
Berita Terkini
review anime
Comments are closed, but trackbacks and pingbacks are open.