#!/bin/bash
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
sudo yum update -y
sudo yum install dotnet-sdk-3.1 -y

sudo yum install -y libicu fontconfig harfbuzz freetype libgdiplus glibc glibc-devel

wget https://keysystems.ru/files/web/INSTALL/WEB6-TEST/Stable/Linux/core.tar.gz
chmod +x core.tar.gz
mkdir /etc/web_core
tar -C /etc/web_core/ -xvf core.tar.gz

cd /etc/yum.repos.d
echo -e  "[nginx]">nginx.repo
echo -e  "name=nginx repo">>nginx.repo
echo -e  "baseurl=http://nginx.org/packages/mainline/rhel/7/\$basearch/">>nginx.repo
echo -e  "gpgcheck=0">>nginx.repo
echo -e  "enabled=1">>nginx.repo

sudo yum install nginx -y
cd /etc/systemd/system
echo -e  "[Unit]">kestrel-web_next.service
echo -e  "Description=Example .NET Web API App running on RedOS 7.2">>kestrel-web_next.service
echo -e  "[Service]">>kestrel-web_next.service
echo -e  "WorkingDirectory=/etc/web_core/Publish/">>kestrel-web_next.service
echo -e  "ExecStart=/bin/dotnet /etc/web_core/Publish/web_core.dll">>kestrel-web_next.service
echo -e  "Restart=always">>kestrel-web_next.service
echo -e  "RestartSec=10">>kestrel-web_next.service
echo -e  "KillSignal=SIGINT">>kestrel-web_next.service
echo -e  "SyslogIdentifier=dotnet-example">>kestrel-web_next.service
echo -e  "User=root">>kestrel-web_next.service
echo -e  "Environment=ASPNETCORE_ENVIRONMENT=Production">>kestrel-web_next.service
echo -e  "[Install]">>kestrel-web_next.service
echo -e  "WantedBy=multi-user.target">>kestrel-web_next.service

sudo systemctl enable kestrel-web_next
sudo systemctl start kestrel-web_next.service 
sudo systemctl status kestrel-web_next.service

cd /etc/nginx/conf.d
rm default.conf
echo -e "server {">default.conf
echo -e  "listen 80;">>default.conf
echo -e "server_name   example.com *.example.com;">>default.conf
echo -e  "location / {">>default.conf
echo -e  "    proxy_pass http://localhost:5000;">>default.conf
echo -e  "    proxy_http_version 1.1;">>default.conf
echo -e  "    proxy_set_header Upgrade \$http_upgrade;">>default.conf
echo -e  "    proxy_set_header Connection keep-alive;">>default.conf
echo -e  "    proxy_set_header Host \$host;">>default.conf
echo -e  "    proxy_cache_bypass \$http_upgrade;">>default.conf
echo -e  "    proxy_set_header X-Forwarded-For  \$proxy_add_x_forwarded_for;">>default.conf
echo -e  "    proxy_set_header X-Forwarded-Proto \$scheme;">>default.conf
echo -e  "   }">>default.conf
echo -e  "}">>default.conf

firewall-cmd --add-port=5000/tcp –permanent
firewall-cmd –reload
firewall-cmd --add-port=80/tcp –permanent
firewall-cmd –reload

sudo systemctl enable nginx
/usr/sbin/setsebool -P httpd_can_network_connect 1
sudo systemctl start nginx
sudo systemctl status nginx

