How do I configure a Python script to run as a service (re-launch on system restart, restart on failure) in Amazon AWS EC2 instance?
You can create a systemd service on the ec2 instance to achieve this. Steps are:
Create a service definition file:
sudo vi /lib/systemd/system/mypythonservice.service
Add the systemd unit file definition. You can check this or the systemd reference guide for more details:
[Unit]
Description=My Python Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/myuser/mypythonproject.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
Set the necessary permissions on the file:
sudo chmod 644 /lib/systemd/system/mypythonservice.service
Reload the systemd daemon:
sudo systemctl daemon-reload
Enable the service to start on reboot:
sudo systemctl enable mypythonservice.service
And of course you can add all of this as part of a EC2 Instance User Data script to automatically configure on instance launch.
Configure a Python as a service in AWS EC2
After much unsuccessful research to set up a Python API written on custom port 8080 to run on Amazon's Linux AMI operating system (AWS), I decided to solve this dilemma and share the solution with all of you.
See the solution in this link.
Related
I need to turn off sshd service in ElasticBeanstalk ec2. (Docker MultiContainer)
e.g) service sshd stop.
So i try to add .ebextensions commands.(refer to link https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html)
0_command.config
container_commands:
00-stop-sshd:
command: 'sudo service sshd stop'
but it is not worked. I wonder why it doesn't work.
How can I execute the above command?
I wanted to launch a little static website on an EC2 instance and I followed the following steps:
launched a t2.micro instance using console in us-east-1 region
attached an existing security group which allows http request, https and ssh.
logged in my ec2 through SSH and changed it into an apache web server using following commands:
commands:
#bin/bash
sudo su
yum update -y
yum install httpd -y
cd /var/www/html
Then I wrote a hello world html code in index.html file and started my web service
service httpd start
chkconfig on
Even after following all the above steps, when I open try to access the webpage by going to the public ipv4 address of my ec2 instance, I am getting a timout error.
Any idea what I might be missing here?
Thank you.
You can try creating new EC2 instance like below
Lunch the same type of instance
Add below script in user data to create HTML page
#!/bin/bash
Use this for your user data (script from top to bottom)
install httpd
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "Hello World - $(hostname -f)" > /var/www/html/index.html
After launching EC2, open that security group and add below HTTP rule
At the end, manually type HTTP://your public address. sometimes by default its shows with HTTPS so just ensure you are typing HTTP only.
I have a number of containers on Docker in the AWS EC2 instance. All the containers are set to restart=always using this command
sudo docker update --restart=always 0576df221c0b
But, After the AWS Linux host start, docker containers on AWS now do not start until I run the “docker ps” command in the terminal. Here is a screenshot of docker ps. the screenshot below was taken after 1 hour of the AWS Linux reboot.
Any ideas about what might be causing the problem? Thanks a lot
Check the status of docker service & docker systemd socket. Probably docker service is down. And systemd socket is enabled for docker.
systemctl status docker.service
systemctl status docker.socket
When socket is enabled, Systemd daemon open listening sockets on behalf of the docker application and only start the docker daemon when a connection comes in. In your case when you execute the docker ps, a connection reaches to the listening socket and it in turn starting the docker.
To change the behaviour, enable the docker service. Then all your containers should start on system boot up. Run the following command
systemctl enable docker.service
I'm new to devops. I want to install Jenkins in AWS EC2 with docker.
I have installed the Jenkins by this command:
docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts
On AWS security group, I have enabled port 8080 and 50000. I also enabled port 22 for SSH, 27017 for Mongo and 3000 for Node.
I can see the Jenkins container when I run docker ps. However, when I run https://xxxx.us-east-2.compute.amazonaws.com:8080, there is not a Jenkins window popup for Jenkins setting and display error, ERR_SSL_PROTOCOL_ERROR.
Does someone know what's wrong here? Should I install Nginx as well? I didn't install it yet.
The error is due to the fact that you are using https:
https://xxxx.us-east-2.compute.amazonaws.com:8080
From your description it does not seem that you've setup any type of ssl connection to your instance. So you should connect using http only:
http://xxxx.us-east-2.compute.amazonaws.com:8080
But this is not good practice as you communicate using plain text. A common solution is to access your jenkins web-ui through ssh tunnel. This way the connection is encrypted and you don't have to exposed any jenkins port in your security groups.
I have a jenkins instance running inside a docker container that's listening on port 8181.
Example URL of the jenkins instance:
http://ec2-34-155-164-97.us-west-2.compute.amazonaws.com/
I have a tomcat docker instance that's listening on port 8383 running inside the jenkins docker container.
I can access jenkins instance from my local browser. Is there any possible way that I can access my docker tomcat instance from my local browser?
Here is my docker run command:
docker run -d -v /var/run/docker.sock:/var/run/docker.sock \ -v $(which docker):/usr/bin/docker -p 8181:8080 jenkins-dsl
Please provide your suggestions.
It sounds like your docker run command simply needs to expose the port that your nested tomcat server is running on.
To do this, you need to pass in -p argument into your command. The -p argument is for binding a host port to the docker container's port:
-p <host_port>:<container_port>
You can pass in as many -p arguments as you want to bind multiple ports.
So if the docker tomcat server is running on port 8383 within the Jenkins docker container, then you can do something like this:
-p 8383:8080
Full command example:
docker run -d -it -p 8383:8080 --name tomcatServer docker-tomcat
I would assume that this would allow you to access tomcat server using the example URL provided like so:
http://ec2-34-155-164-97.us-west-2.compute.amazonaws.com:8383
However, you'd have to ensure your AWS Security Group will allow traffic to port 8383.
EDIT: Updated answer to reflect the resolution we discussed in the comments.
Edited
I could able to launch tomcat by specifying the port in the URL and opening the port in EC2 instance.
http://ec2-34-155-164-97.us-west-2.compute.amazonaws.com:8383
Latest Docker installation guide for Tomcat clearly says you will get this error when you launch it for the first time
You can then go to http://localhost:8888 or http://host-ip:8888 in a browser (noting that it will return a 404 since there are no webapps loaded by default).
its because you do not have any apps in the default webapps folder of Tomcat. your latest Tomcat docker image has the default apps in the "webapps.dist" folder, you have to copy it to "webapps" folder. Do the Following commands
# docker exec -it tomcat-container /bin/bash
# cd webapps.dist
# cp -R * ../webapps
"tomcat-container" is your container name.
now refresh your browser you will get it. if not let me know