Reputation: 3524
Deploy my webapi dotnet core project to Ubuntu server Configuration with kestrel and proxy via nginx
my code in file Program.cs
public class Program {
public static void Main(string[] args) {
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: true)
.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseConfiguration(config)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseUrls("http://localhost:5050")
.UseStartup<Startup>()
.Build();
host.Run();
}
}
Created file in /etc/nginx/sites-available/default
and maded symlink to /etc/nginx/sites-enable/default
server {
listen 80;
location / {
proxy_pass http://localhost:5050;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
}
}
my file in /etc/systemd/system/kestrel-project.service
[Unit]
Description=WebApi .NET Core running on Ubuntu
[Service]
WorkingDirectory=/var/aspnetcore/project
ExecStart=/usr/bin/dotnet /var/aspnetcore/project/project.dll
Restart=always
RestartSec=10
SyslogIdentifier=dotnet-project
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
For deploy use this script and it's work until this day
dotnet restore
dotnet build
dotnet publish
cp -r /root/src/project/bin/Debug/netcoreapp2.0/publish/* /var/aspnetcore/project/
cp /root/src/project/bin/Debug/netcoreapp2.0/project.dll /var/aspnetcore/project/
service nginx restart
systemctl daemon-reload
systemctl stop kestrel-project.service
systemctl enable kestrel-project.service
systemctl start kestrel-project.service
systemctl status kestrel-project.service
Now when I launch my webapi on my localhost on pc - webapi work well and answer to my requests, but from server I've got HTTP error 500 and in /var/log/nginx/error.log
I see this
2018/02/11 19:58:04 [alert] 10584#10584: aborting
2018/02/11 20:01:15 [error] 11717#11717: *1 connect() failed (111:Connection refused) while connecting to upstream, client: *.*.*.*, server: , request: "GET /list HTTP/1.1", upstream: "http://127.0.1.1:5050/list", host: "*.*.*.*.*"
Please help me to understand this problem and fix it
Without changes webapi work well a long time but a couple of days ago I began to appear these errors
Upvotes: 5
Views: 3738
Reputation: 3524
It's not the answer on my question, but I found another way to eradicate the problem
How to make project great again
Create new .net Core project Web-Api with Docker support
Copy/Paste code of the project to new project with Docker
And now my code in Program.cs
public static void Main(string[] args) {
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseStartup<Startup>()
.Build();
}
My file in /etc/nginx/sites-available/default
server {
listen 80;
server_name api.mydomain.com;
location / {
proxy_pass http://localhost:5050;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
drop file /etc/systemd/system/kestrel-project.service
config Docker in project
My Docker file - use for automatic build on Docker Hub
FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app
RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
EXPOSE 5050/tcp
ENV ASPNETCORE_URLS http://*5050
ENV ASPNETCORE_ENVIRONMENT docker
ENTRYPOINT ["dotnet", "run", "--server.urls", "http://0.0.0.0:5050"]
My file Docker.Compose
FROM microsoft/dotnet:latest
WORKDIR /app
EXPOSE 5050:80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY project/project.csproj project/
RUN dotnet restore
COPY . .
WORKDIR /src/project
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "project.dll"]
sign up on Docker Hub it's free
Connect to my github account and create automatic build for My project
And then on server side I connect to docker hub
make docker pull dockerprofile/project
and then docker run -d -p 5050:80 dockerprofile/project
After that everything works fine already 4 months
ps If you need more details just ask for me
Upvotes: 1