Deploy ASP.NET Core Apps to AWS EC2
Before deploying ASP.NET Core apps to AWS EC2, the EC2 Linux has
to be setup completely for running .NET Core app (like installing
.NET Core 2.1 or .NET Core 2.0, installing Apache 2 (Httpd) and
setting the reverse mechanism for localhost:5000, etc.)
I have an ASP.NET Core project that is named VodManageSystem in
Visual Studio on Mac and I want to deploy it to AWS EC2 then the
following steps have to be done.
1. Publish VodManageSystem Project
- Go to the root directory of VodManageSystem
project,
/Users/chaolee/VisualStudio/Smile_SoftwareSolution/VodManageSystem
- Key in the following command line then the
the project will be published to a folder named
/Users/chaolee/VisualStudio/Smile_SoftwareSolution/VodManageSystem/bin/Release/netcoreapp2.1,
this folder name is depended on .NET Core version
dotnet publish --configuration Release
2. Login to AWS
- cd
/Users/chaolee/AmazonAWS/KeyPairName --->
The folder that the KeyPair from AWS is stored on my Mac
- chmod 400 smile.pem
---> Change privileges of the file (smile.pem) of the
KeyPair downloaded from AWS
- ssh -i smile.pem
ec2-user@ec2-13-59-195-3.us-east-2.compute.amazonaws.com
---> Login to AWS cloud
The three command lines are included inside
loginAmazonEC2.sh under my EC2 home directory so I can just go to
home directory and execute the following tow commands
3. Copy the published VodManageSystem
package to AWS
Execute the following command:
- scp -r -i
/Users/chaolee/AmazonAWS/KeyPairName/Smile.pem
/Users/chaolee/VisualStudio/Smile_SoftwareSolution/VodManageSystem/bin/Release/netcoreapp2.1/publish/.
ec2-user@ec2-13-59-195-3.us-east-2.compute.amazonaws.com:/home/ec2-user/aspdotnetcore_web/
or execute the following command because I
put this long command line inside the bash file named
scp_aspdotnetcore_web.sh.
- sh scp_aspdotnetcore_web.sh
The published VodManageSystem
package will be copied to asp_dotnetcore_web directory under my
EC2 home directory, which is
4. Kill the running processes of VodManageSystem's .NET Core
app
First of all, Display all running process
of my EC2 instance on AWS by executing the following command to
find out the its processes ids
Second, kill the running processes
depending on ids that are found
- sudo kill 3130 (for
example: 3130 was the id for bash command that is executed by
sh VodManageSystem.sh)
- sudo kill 3131 (for
example: 3131 was the running process id of
VodManageSystem.dll)
5. Delete the old files and directories of the
VodManageSystem package that was deployed before
The published VodManageSystem package was put
under directory, /opt/VodManageSystem on AWS EC2 instance so go to
this directory by executing the following command
Then remove the whole package under this
directory (/opt/VodManageSystem) using command:
6. Move all files and directories of VodManageSystem
package that was just copied to aspdotnetcore_web directory
under EC2 home directory to /opt/VodManageSystem
- sudo mv * /opt/VodManageSystem
7. Check if VodManageSystem.dll can be run
- /opt/dotnet/dotnet VodManageSystem.dll
(because the .NET Core was installed in /opt/dotnet directory)
8. If it did not work then check what went wrong and fix it
by going through the above steps
9. If it works then reboot the AWS EC2 instance (the EC2 linux
system will start VodManageSystem.dll when it starts because the
auto execution was setup before)
- Go to Amazon Web Services (AWS) Console and login
- Action->Instance State->Reboot
- After few minutes then check if the .NET Core app
(VodManageSystem) is running by going to its web address
10. The settings for auto executing when AWS EC2 instance starts
- Create a bash file named VodManageSystem.sh under /opt
directory and there are three command lines inside
cd /opt/VodManageSystem
/opt/dotnet/dotnet
VodManageSystem.dll
exit 0
- Put the following command line inside /etc/rc.d/rc.local file
sh /opt/VodManageSystem.sh