1.安裝 NET 7 Runtime
查看已安裝的版本dotnet --list-runtimes
2.安裝 Apache server
Apache server will act as a reverse proxy and pass the request to the Kestrel Server from the internet.
2_1. Let’s install the Apache server in the Linux machine using the following command:
sudo apt install apache2
2_2.After installing the Apache Server, install the following required modules:
sudo a2enmod rewrite
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod headers
sudo a2enmod ssl2_3.Now, restart the Apache server using the following command:
sudo systemctl restart
apache2
3.上傳網站內容到服務器.
4. 為網站建立一個服務檔案
#sudo nano /etc/systemd/system/xxx.ice.com.tw.service
[Unit]
/xxx.
Description=Running ASP.NET Core on debian 11 Webserver APACHE
[Service]
WorkingDirectory=/sftserver/web/xxx.ice.com.tw/
ExecStart=/usr/bin/dotnet /sftpserver/web/xxx.ice.com.twdll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
In the above configuration,
WorkingDirectory defines the folder from which the service will run.
ExecStart defines the application to be started in this service.
Then, move the newly created file to the Linux server in the location etc/systemd/system/ using FileZilla. Enable and start the service using the following command in PuTTY:sudo systemctl enable
xxx.ice.com.tw
.service
sudo systemctl startxxx.ice.com.tw
.service
5. 建立Apach組態檔案
#sudo nano /etc/apache2/sites-available/.conf
xxx.ice.com.tw
figuration file
Create a new host configuration file named sample.conf and add the following configuration to it:<VirtualHost *:80>
ServerName
xxx.ice.com.tw
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
ErrorLog ${APACHE_LOG_DIR}/.-error.log
xxx.ice.com.tw
</VirtualHost>
In the above configuration:
VirtualHost *:80 means that the site is hosted under port 80.
Domain URL is given in the ServerName.
For proxypass and proxypassreverse, we have provided the Kestrel server that is running locally and its default port is 5000.
All the application’s config files are located in the file path etc/apache2/sites-available in the Linux server. So, move the created config file to the Linux server under this folder.
Now, enable the new site configuration using the following command:
#sudo a2ensite xxx.ice.com.tw.conf
Check the syntax of the configuration using the following command:sudo apachectl configtest
If there is no error, then your site is ready to go!
5. 建立第2個以後ASP.NET Core 網站
We have seen how to create a service and config file to host an ASP.NET Core application in a Linux server. Now, let’s see how you can host another application on the same Linux server. To do so, follow these steps:
Move the new application sample to the Linux server under the file path /var/www/sample1.
Then, create a new service file named sample1.service and add the following configuration to it. Here, we will add the port from which the new sample application will be hosted.[Unit]
Description=Running ASP.NET Core on Ubuntu 18.04 Webserver APACHE
[Service]
WorkingDirectory=/var/www/sample/
ExecStart=/usr/bin/dotnet /var/www/sample/sample.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
Environment=ASPNETCORE_URLS=http://127.0.0.1:5001
[Install]
WantedBy=multi-user.target
Move the service and enable it by following the steps given in the Create a service to Run the application section.
Now, create a new host configuration file named sample1.conf and add the following command to it. Here, for proxy, use the new port 5001.<ltVirtualHost *:80>
ServerName www.sample1.com
ProxyPreserveHost On
ProxyPass / http:// 127.0.0.1:5001/
ProxyPassReverse / http:// 127.0.0.1:5001/
ErrorLog ${APACHE_LOG_DIR}/sample-error.log
</VirtualHost>
Finally, move the .conf file to the Linux server and then enable the site. Now, you can access the new hosted application.
Note: Similarly, you can host any number of applications using the app’s port in the Linux server.
Conclusion
Thanks for reading! In this blog post, we have seen how to host multiple ASP.NET Core applications in a Linux Ubuntu server using Apache as a reverse proxy. With this approach, hosting an ASP.NET Core app in Linux isn’t complicated. So, try out the steps provided in this blog post and leave your feedback in the comments section.
The Syncfusion ASP.NET Core UI controls library powered by Essential JS 2 is the only suite that you will ever need to build an application since it contains over 70 high-performance, lightweight, modular, and responsive UI controls in a single package. Use them to increase your productivity in app development!
If you’re already a Syncfusion user, you can download the product setup, or, you can download a free 30-day trial to evaluate our products.
You can also contact us through our support forum, Direct-Trac, or feedback portal. As always, we are happy to assist you!