https://github.com/feed0/server-cpu-stress
ASP.NET webapp for processing prime numbers in an EC2 IIS environment
https://github.com/feed0/server-cpu-stress
aws-ec2 c-sharp csharp dotnet iis-server linux nginx nginx-proxy windows-server-2022
Last synced: about 2 months ago
JSON representation
ASP.NET webapp for processing prime numbers in an EC2 IIS environment
- Host: GitHub
- URL: https://github.com/feed0/server-cpu-stress
- Owner: feed0
- Created: 2024-10-11T22:36:29.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-16T18:22:15.000Z (over 1 year ago)
- Last Synced: 2025-02-08T07:13:19.980Z (over 1 year ago)
- Topics: aws-ec2, c-sharp, csharp, dotnet, iis-server, linux, nginx, nginx-proxy, windows-server-2022
- Language: HTML
- Homepage: http://18.230.17.178
- Size: 1.42 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Server CPU Stress
This ASP.NET repository contains two HTML pages to process prime number up to a given input by an user.
Supports hosting via either 1. Ineternet Information Services 10.0 (IIS) on Windows Server 2022 Base (2024.09.11),
or 2. via Nginx using a reverse proxy nginx.conf file.
## The webapp
### Index.cshtml

### Results.cshtml

## Requirements
### a) Windows Server 2022
- Server Manager Roles: World Wide Web, IIS and Aplication roles;
- .NET 8.0+ SDK in order to publish the webapp;
- .NET 8.0+ Core Host Bundle installed;
- Port inbound rules active on Windows Defender Firewall.
### b) Linux
- Nginx;
- .NET 8.0+ SDK in order to publish the webapp;
- No hookup: for persistent serving without a need for an open bash use:
```bash
nohup dotnet server-cpu-stress.dll &
```
## Main files
### Controller
HomeController.cs holds two function:
1. IsPrime()
```C#
///
/// Check if a number is prime.
///
/// The number to check.
/// True if the number is prime, false otherwise.
///
/// This method is not efficient, but it is simple and easy to understand.
/// It checks if the number is divisible by any number from 2 to the square root of the number.
/// If it is divisible by any of these numbers, then it is not prime.
/// If it is not divisible by any of these numbers, then it is prime.
///
private bool IsPrime(int num)
{
if (num < 2) return false;
for (int i = 2; i <= Math.Sqrt(num); i++)
{
if (num % i == 0) return false;
}
return true;
}
```
2. GetPrimesUpTo() - which calls IsPrime()
```C#
///
/// Get all prime numbers up to a given number.
///
/// The maximum number to check.
/// A list of all prime numbers up to the input.
///
/// This method is not efficient, but it is simple and easy to understand.
/// It checks if each number is prime using the IsPrime method.
/// If the number is prime, it is added to the list of primes.
/// Finally, the list of primes is returned.
///
private List GetPrimesUpTo(int max)
{
var primes = new List();
for (int num = 2; num <= max; num++)
{
if (IsPrime(num))
{
primes.Add(num);
}
}
return primes;
}
```
### Views/Home
#### Index.cshtml
Presents a form so the user can input a upper limit (a maximum) for the List of primes as the result.
#### Results.cshtml
Displays each of the elements (primes) in a new line up to the input of the Index.cshtml page.
Also provides a back button.
### Program.cs
The only change made was:
```C#
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
```
## Publish to IIS on Windows Server
### Preparing the project
1. Build
I personally suggest `C:\projects\server-cpu-stress` as the webapp directory.
```bash
dotnet build
```
2. Publish
Choose either:
a) Publishing without a path.
```bash
dotnet publish -c Release
```
b) Publishing in a specific path.
That may be `C:\published\server-cpu-stress`
```bash
dotnet publish -c Release -o
```
### Running on IIS
1. Add a new Website
With IIS open, right click on Sites ...

2. Set it up
Set a name such as `server-cpu-stress`
- If it's a new site, IIS might create a new `Application pool` (field to the right of the name) which will follow with the same name as you choose for your `Site name`.
- Choose the path for the published webapp such as `C:\published\server-cpu-stress`.
- Bind a port such as 80. Make sure there are rules to allow traffic from that port on Windows Defender Firewall's Ibound Rules. Added them as needed.

3. Check its Application pool
(LEFT) Click on Application Pools;
(MIDDLE) Click on the name of your pool such as `server-cpu-stress`;
(RIGHT) Click on `Basic settings`.

And finally make sure your pool's `.NET CLR version` is set to `No Managed Code`

## Serving with Nginx and revers proxy
### Install .NET 8.0 SDK
```bash
sudo yum install -y dotnet-sdk-8.0
```
### Nginx
#### Install
```bash
sudo dnf install nginx -y
```
#### Publish the webapp code
Publishing without a path.
```bash
dotnet publish -c Release
```
#### Start Nginx
```bash
sudo systemctl start nginx
sudo systemctl enable nginx
```
#### Configure the Nginx reverse proxy
Open the .congf file
```bash
sudo nano /etc/nginx/nginx.conf
```
```bash
server {
listen 80; # Port
server_name 00.000.000.00; # Your public IP or domain
location / {
proxy_pass http://localhost:5000; # Change the port to match your Kestrel app
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
```
#### Restart
```bash
sudo systemctl restart nginx
```
#### Run locally
```bash
dotnet run --project server-cpu-stress.csproj
```