.Net Core on Linux

Host ASP.NET Core on Linux with Apache

Server Config
# install apache / bind

yum install bind httpd

# create link to Maria DB Repository

vi /etc/yum.repos.d/MariaDB.repo

#paste this in

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

#install it

yum install mariadb

#add .net core feed

rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm

#update all existing packages

yum update

#install .net core 2.2

yum install dotnet-sdk-2.2 -y

#add a user for asp projects

adduser asp-dev
Passwd asp-dev

#add user to wheel adds su priv

usermod -aG wheel asp-dev

#switch to asp-dev user

su asp-dev

#goto home folder

cd ~

#make aspTest project

dotnet new console -o aspTest
cd aspTest

#create Program.cs

vi program.cs

Then paste

using System;

namespace myApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Type :wq to save
Then test it with

dotnet run

Should look like

#switch back to root

su root

#start mariadb

service mariadb start

#force mariadb to start each time the system boots

systemctl start mariadb

#start named (DNS)

service named start
system start named

#create Zones folder for DNS

mkdir /etc/named/zones

#create zone for eugeneprogramming

vi /etc/named/zones/eugeneprogramming.com

Paste

; BIND db file for eugeneprogramming.com

$TTL 86400

@       IN      SOA     lee.ns.cloudflare.com.      hostmaster.eugeneprogramming.com. (
                        2019011901	; serial number YYMMDDNN
                        28800           ; Refresh
                        7200            ; Retry
                        864000          ; Expire
                        86400           ; Min TTL
			)

                NS      lee.ns.cloudflare.com. 
                NS      amanda.ns.cloudflare.com. 

                MX      10 mail.eugeneprogramming.com.
                MX      20 mail.eugeneprogramming.com.


$ORIGIN eugeneprogramming.com.

Then :wq to save

#edit named.conf to include zone

vi /etc/named.conf

#Add to end

zone "eugeneprogramming.com" IN {
        type master;
        notify no;
        file "/etc/named/zones/eugeneprogramming.com";
};

#Create public_html folder

mkdir -p /var/www/eugeneprogramming.com/public_html
chown -R eugenep:eugenep /var/www/eugeneprogramming.com/public_html
chmod -R 755 /var/www

#create apache sites-available

mkdir /etc/httpd/sites-available
mkdir /etc/httpd/sites-enabled

#edit apache config

vi /etc/httpd/conf/httpd.conf

#add to end

IncludeOptional sites-enabled/*.conf

#link it to enabled

ln -s /etc/httpd/sites-available/eugeneprogramming.com.conf /etc/httpd/sites-enabled/eugeneprogramming.com.conf

#make ssl folder

Mkdir /var/www/eugeneprogramming.com/ssl

#generated origin SSL cert in cloudflare PEM Format

#Create a .crt file and a .key file from cloudflare and save it in ssl folder

#Then in the vhosts

SSLEngine      on
SSLCertificateFile        /path/to/your_domain_name.crt
SSLCertificateKeyFile     /path/to/your_private.key 

#edit your vhost and add it all in

vi /etc/httpd/sites-available/eugeneprogramming.com.conf

#at the end of everything it will looklike

So part of this is a reverse proxy that passes the local application running on port 5001 to an apache subdomain.

#create Maria DB and user

Type MYSQL and a terminal prompt shows
create database eugenepr default character set utf8 default collate utf8_bin;

This is an admin user with access to all databases

CREATE USER 'bar'@'%' IDENTIFIED BY 'strongpassword';
GRANT ALL PRIVILEGES ON *.* TO 'bar'@'%' ;

You can limit to a database like eugenepr

GRANT ALL PRIVILEGES ON eugenepr.* TO 'bar'@'%' ;

#I like phpmyadmin so i added that

rpm -iUvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install phpmyadmin
vim /etc/httpd/conf.d/phpMyAdmin.conf

#Add your ip to allowed
#also add your connection settings

Application Specific Changes
In your ASP Core project install Pomelo.EntityFrameworkCore.MySql Nuget package. I did version 2.1.4.

Then in your startup.cs

 // Msql 
 // services.AddDbContext(options => options.UseSqlServer(Configuration["ConnectionStrings:Local"]));

Change To

 // Maria DB
      services.AddDbContext(options => options.UseMySql(Configuration["ConnectionStrings:Local"]));

Then your appsettings.json connectoin string is going to change to be

"Local": "Server=127.0.0.1;Database=YOURDATABASE;User Id=YOURUSER;Password=YOURPASSWORD"

Creating a Service of your App

In order for your app to run all the time you need to create a service

vi /etc/systemd/system/lab2.service

Put this in and save

[Unit]
Description=Lab 2 for CS296

[Service]
WorkingDirectory=/var/www/eugeneprogramming.com/cs296/lab2
ExecStart=/usr/bin/dotnet run /var/www/eugeneprogramming.com/cs296/lab2
ExecStop=/usr/bin/pgrep dotnet-cs296-lab2
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-cs296-lab2
User=eugenep
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

Now you should be able to

service lab2 start

And just so its the default for the system to start the service on boot

systemctl start lab2

If all is well you should be able to access your .net core app via apache reverse proxy 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *