Embed .NET Core Application with WordPress on Ubuntu Using Apache
This section explains how to install and embed .NET Core Application with WordPress on Ubuntu using the Apache Server.
-
Refer to this help link to setup and configure WordPress-one-click-install in digital ocean.
-
Refer to this link to install .NET SDK and .NET runtime.
-
Move the given .NET Core application to the following location on your Ubuntu machine and unzip it.
Location -/var/www/html -
Now, create a service for the .NET core application in the following location.
Service file location -/etc/systemd/system -
Create a file name with the
.serviceextension, copy-paste the following code sample, and modify the application path.
Example –newapp.service[Unit] Description=newapp [Service] WorkingDirectory=/var/www/html/<Embed-folder name> ExecStart=/usr/share/dotnet/dotnet /var/www/html/<Embed-folder name>/SampleCoreApp.dll --urls=http://localhost:5000 Restart=always #Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT SyslogIdentifier=newapp User=root Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target -
Save the file and run the following commands to enable and start the service.
sudo systemctl enable <service file name> sudo systemclt start <service file name> sudo systemctl status <service file name>Example-
sudo systemctl enable newapp.service -
Afterwards, create an Apache2 configuration for hosting our application. Therefore, navigate to the directory
/etc/apache2/sites-availableand create a file with a.confextension as shown follows.
For Example –sample-app.conf -
Create your virtual host as the following inside the created
.conffile: Copy-paste the code sample and fill in the required values.<VirtualHost *:*> RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME} </VirtualHost> <VirtualHost *:80> DocumentRoot "/var/www/html/" ServerName <your-domain-name> ErrorLog ${APACHE_LOG_DIR}helloapp-error.log CustomLog ${APACHE_LOG_DIR}helloapp-access.log common RewriteEngine on RewriteCond %{SERVER_NAME} =<your-domain-name> RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost> <VirtualHost *:80> ProxyPreserveHost On ProxyPass / http://127.0.0.1:5000/ ProxyPassReverse / http://127.0.0.1:5000/ ServerName <your-sub-domain-name> ErrorLog ${APACHE_LOG_DIR}helloapp-error.log CustomLog ${APACHE_LOG_DIR}helloapp-access.log common RewriteEngine on RewriteCond %{SERVER_NAME} =<your-sub-domain-name> RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost> -
After creating the file, enable the site using the following command.
a2ensite <conf file name> -
Then, restart the Apache2 server and access the server name in a web browser.
-
For configuring SSL for the site, create a
.conffile in the following location.
/etc/apache2/sites-available -
Then, place your SSL cert and key files in
/etc/ssl. -
Now, create an SSL conf file as follows.
Example -Sample-app-ssl.conf<VirtualHost *:443> ProxyPreserveHost On ProxyPass / http://127.0.0.1:5000/ ProxyPassReverse / http://127.0.0.1:5000/ ServerName <your-sub-domain-name> ErrorLog ${APACHE_LOG_DIR}helloapp-error.log CustomLog ${APACHE_LOG_DIR}helloapp-access.log common SSLCertificateFile /etc/ssl/<Crt-file-name> SSLCertificateKeyFile /etc/ssl/<Key-file-name> </VirtualHost> <VirtualHost *:443> ServerName <your-domain-name> DocumentRoot "/var/www/html" SSLEngine on SSLCertificateFile /etc/ssl/<Crt-file-name> SSLCertificateKeyFile /etc/ssl/<Key-file-name> </VirtualHost> -
After creating the file, enable the site through the following command.
a2ensite <conf file name> -
Now, restart the Apache2 server using the following command and refresh the site. The site will now be redirected to HTTPS.
sudo systemctl restart apache2.services