How to auto start web services when starting an Amazon EC2 instance? - apache

How do I set the httpd and mysqld services to start automatically upon booting an amazon-ec2 instance?
Currently I have to start them manually by connecting to the instance via ssh and running sudo service httpd start and sudo service mysqld start.

Rather than starting over with a new AMI, you could just issue the following commands on an Amazon Linux EC2 instance...
sudo chkconfig mysqld on
sudo chkconfig httpd on
You can check the settings before & after enabling these services to start on boot using the following commands...
sudo chkconfig --list mysqld
sudo chkconfig --list httpd
See all services using just...
sudo chkconfig --list
NOTE: If you are having any trouble with chkconfig being in root's path, you can try specifying the full path like this...
sudo /sbin/chkconfig mysqld on
sudo /sbin/chkconfig httpd on

It is different between Amazon Linux 1 and Amazon Linux 2.
Amazon Linux 1
In AmazonLinux1, use chkconfig command.
$ sudo chkconfig mysqld on
$ sudo chkconfig httpd on
Amazon Linux2
In AmazonLinux2, systemd was introduced. So, chkconfig is legacy command. You should use systemctl. It is a control command for systemd.
$ sudo systemctl enable mysqld
$ sudo systemctl enable httpd
You can confirm it is enabled or not using by is-enabled command.
$ sudo systemctl is-enabled mysqld
enabled
chkconfig command request will be forwarded to systemctl.
$ chkconfig mysqld on
Note: Forwarding request to 'systemctl enable mysqld.service'.

If you using Amazon Linux 2 AMI you need to follow these steps:
In AMI2 they are using systemctl for managing services check if it is installed on your machine
2.systemctl list-units --type=service by this command check if tomcat.service is listed
sudo systemctl enable tomcat.service To eanable tomcat start on boot up
systemctl is-enabled tomcat.service To check if tomcat enabled to start on boot up linux system
After that you can reboot your linux system and tomcat will be started.
For more about systemctl Click Here

One of my client wants to do this task and I have successfully done by using following way.
Following commands starts the services automatic when instance started.
Auto start apache/httpd
1) systemctl enable httpd
Auto start redis service
2) systemctl enable redis
I have set SELINUX set to disabled in
3) /etc/sysconfig/selinux
For mysql services
sudo chkconfig mysqld on
sudo chkconfig httpd on

I faced the similar problem, here is the solution i am suggesting,
you need to create a file under /etc/init.d directory, e.g with name tomcat, and change the JAVA_HOME and CATALINA_HOME parameters as per your system installation.
Once you do setup this file then run the below command:
sudo chkconfig <file-name> on
where is the file you have created in /etc/init.d it is tomcat in my case.
[ec2-user#ip-<myip> init.d]$ cat tomcat
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/opt/apache-tomcat-7.0.96
export $JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/opt/apache-tomcat-7.0.96
case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
sh $CATALINA_HOME/bin/shutdown.sh
;;
restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
esac
exit 0
chmod 755 tomcat
chkconfig --add tomcat
chkconfig --level 234 tomcat on
chkconfig --list tomcat
service tomcat start

ReactJS on Amazon Linux2 process:
Installing ReactJS on EC2 and running the app at boot:
Once you connect to EC2 instance install NodeJS. Follow this tutorial:
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html
Install httpd server using this tutorial: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateWebServer.html
I used Git Clone to clone the ReactJS app on to /home/ec2-user.
Install Yarn using the command “npm install yarn -g”
Execute the following commands in the cloned project: “Yarn” and then “Yarn build”
Now Copy the build folder using : cp -a /build/. /var/www/html/
Now go to the /var/www/html/ here create a .htaccess file using vi and include the following content: “Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]”
Save the file with :wq
Now in /etc/httpd/conf/httpd.conf search for Directory with “/var/www/html” attribute and change “AllowOverride None” to “AllowOverride All”. Now open the browser and enter http://ec2-ip or http://ec2-url you will see the default page
Enter the command “systemctl enable httpd” and then “systemctl start httpd” on AmazonLinux2. Now you can access the app on boot rather than running the app again and again.
You are complete.

The best way on Amazon Linux 2 is to use the following bash script on creation. This will install the updates, start Apache2, make it listed as a service so that it automatically restarts upon reboot, and the creation of an index.html and health.html sample files. Configuring a health page is important for application loadbalancers and for autoscaling groups.
#!/bin/bash
yum update -y
yum install httpd -y httpd-tools mod_ssl
service httpd start
chkconfig httpd on
systemctl start httpd
systemctl enable httpd
echo "Hello, World, from your Webserver on Amazon Linux" > /var/www/html/index.html
echo "Healthy" > /var/www/html/health.html
Cheers!

Either use any of the preexisting LAMP AMI, it will have both of them running as service already.
One example is BitNami, you will find several other when you fire an ec2 instance.

Related

creating docker container to host website

I want to run static website inside a docker container.
For this i have create ubuntu EC2 machine,installed docker and pulled centos image.
docker pull centos
docker run -td 9f38484d220f bash
docker exec -it aa779e39eb0f bash
===>now inside the container i am using below command
yum update
yum install apache
service httpd start
but i am getting command not recognized error.
Please help me figure out what i am doing wrong.
Also i as i want to run static website i will be putting below code once apache is installed successfully
$touch /var/www/html/index.html
$chkconfig httpd on
$echo "<b>Hii this is my first conatiner running/b>"
>> /var/www/html/index.html
Is this correct way of doing it ?
You installed apache and you are trying to run httpd. Refer this to read the difference between apache2 and httpd. You can run following commands to install apache and run a static hello world page on local host.
$ sudo yum update -y
$ sudo yum install -y httpd
$ sudo service httpd start
$ echo "<html><h1>Hello World!</h1></html>" > test
$ cat test > /var/www/html/index.html
You don't need a container for hosting a static website. S3 is a better choice for this.
If you want to do it as an exercice, considere this simple nginx solution, see: https://hub.docker.com/_/nginx
You have an example in the section : Hosting some simple static content
FROM nginx:alpine
COPY . /usr/share/nginx/html
Remember that you usually don't start a container then start a service inside (for testing and debugging). Entrypoint and command are what start your service, aka what you would manually do.

How to switch Apache MPM from Prefork to Event?

How to switch MPM Prefork to Event on Apache 2.4, Debian 8? I have already installed php-7 and php-fpm but can not find a complete tutorial on switching MPM Prefork to Event. I tried this but MPM Prefork is still running instead of Event.
I did the following steps one by one:
su
export PATH=$PATH:/sbin
a2dismod php7.1
a2dismod mpm_prefork
a2enmod mpm_event
a2enmod proxy_fcgi
a2enconf php7.1-fpm
systemctl restart apache2
If php7.1-fpm is not installed, you can install it before the mentioned steps using apt-get install php7.1-fpm or you can check if it is installed using dpkg -l {package_name}
finally, you can make sure of what you did using:
apachectl -V
Do, a2query -M to check current mpm_ worker(either prefork, event or worker).
Suppose its currently prefork.
So, if you want to switch to mpm_event .
Go to /etc/apache2/mods-enabled and do ls -la there will be symbolic links of mpm_prefork.conf and mpm_prefork.load .
THEN rename the mpm_prefork links to anything else (could be mpm_prefork2.conf and mpm_prefork2.load).
To Rename (run these commands) : cd /etc/apache2/mods-enabled and
mv mpm_prefork.conf mpm_prefork2.conf &
mv mpm_prefork.load mpm_prefork2.load
And then create new symbolic links mpm_event.conf and mpm_event.load from /mods-available to /mods-enabled like this :
sudo ln -s /etc/apache2/mods-available/mpm_event.load /etc/apache2/mods-enabled/mpm_event.load
sudo ln -s /etc/apache2/mods-available/mpm_event.conf /etc/apache2/mods-enabled/mpm_event.conf
And restart apache. sudo service apache2 restart.
Now, do a2query -M it will display event now.
You have successfully switched mpm_prefork to mpm_event

error apache2.service" and "journalctl -xe"

Last time I try add new domain on localhost and I leave it on few weeks so now I try run my apache this command /etc/init.d/apache2 start and I get error
[....] Starting apache2 (via systemctl): apache2.serviceJob for apache2.service failed. See "systemctl status apache2.service" and "journalctl -xe" for details.
failed!
If i tried to do reinstall apache2 but it is still not work.
I just did these two lines.It worked.
Two web servers cannot be active on the one port at the same time
this code for apache & nginx:
or
if error journalctl -xe used this code
sudo apt-get install psmisc
sudo lsof -t -i tcp:80 -s tcp:listen | sudo xargs kill
Virtual Host configuration might cause this error
I solved this same problem by configuring my virtual host .conf files properly.
I created a virtual host & then removed the exapmle.conf file form /etc/apache2/sites-avalable/ but I didn't deleted the examle.conf file from /etc/apache2/sites-enabled/ for this reason i was getting this error.
Then I removed the example.conf file from both the folders( ../sites-enabled & ../sites-available ) and solved this issue.
If you tried to setup any virtual host recently, then try this solution.
Best of Luck
Kill the running process on the port. Hope it will work!
sudo apt-get install psmisc
sudo fuser 80/tcp
sudo lsof -i tcp:80
sudo lsof -i tcp:80 -s tcp:listen
sudo lsof -t -i tcp:80 -s tcp:listen | sudo xargs kill
Go
sudo nano /etc/apache2/apache2.conf
remove this line:
Include /etc/phpmyadmin/apache.conf
Then
service apache2 start/restart
This problem may be a result of some configuration files in apache missing. One of the solutions would be to purge the apache2 file.
You can type:
sudo apt-get purge apache2
Then reinstall apache2 by typing:
sudo apt-get install apache2
As stated in the error message, we just have to execute :
systemctl status apache2.service
or
journalctl -xe
And you will have more detail about the error (line of the error, or command misspelled, or module not included in the configuration, ...) :
for example you can have following detail Invalid command 'SSLEngine', perhaps misspelled or defined by a module not included in the server configuration ==> you then need to execute a2enmod ssl, and then execute service apache2 restart
Also I notice a difference between service apache2 reload and service apache2 restart commands. In case of persisting errors you should execute service apache2 restart, and then execute journalctl -xe.
You type
sudo netstat -pant
You check are you using port 80. If used, `
sudo service 'service_name' stop
and
sudo service apache2 start
The problem is because some configuration files are deleted.
You can use the following command to replace configuration files that have been deleted, without purging the package:
sudo apt-get -o DPkg::Options::="--force-confmiss" --reinstall install apache2
execute sudo service apache2 status and check the result. it might be trying to bind to a port that is already in use

Apache server is running but httpd is not running

I'm facing a weird problem. When I access the web-accessible directory of my server via domain linked to it. It says:-
Not Found
The requested URL / was not found on this server.
When I run this command on my server (via putty), it displays few processes ids demonstrating that Apache web server is running (correct me).
root#...:~# pgrep apache
4733
13505
13506
13507
13508
13686
14199
17672
But when I run this command, it says that httpd: unrecognized service
root#...:~# service httpd status
httpd: unrecognized service
Some other commands output:-
root#...:~# ps aux|grep -i http
root 29401 0.0 0.0 6460 792 pts/0 S+ 02:21 0:00 grep --color=auto -i http
How can I fix this?
Update:-
root#...:~# chkconfig --list | grep httpd
-bash: chkconfig: command not found
root#...:~# find / -name httpd*
/var/www/vhosts/lvps5-35-241-230.dedicated.hosteurope.de/httpdocs
/var/www/vhosts/.skel/0/httpdocs
/opt/psa/var/httpd_restart
/usr/lib/apache2/modules/httpd.exp
/usr/share/doc/apache2-doc/manual/fr/programs/httpd.html
/usr/share/doc/apache2-doc/manual/pt-br/programs/httpd.html
/usr/share/doc/apache2-doc/manual/da/programs/httpd.html
/usr/share/doc/apache2-doc/manual/zh-cn/programs/httpd.html
/usr/share/doc/apache2-doc/manual/tr/programs/httpd.html
/usr/share/doc/apache2-doc/manual/en/programs/httpd.html
/usr/share/doc/apache2-doc/manual/ja/programs/httpd.html
/usr/share/doc/apache2-doc/manual/ko/programs/httpd.html
/usr/share/doc/apache2-doc/manual/de/programs/httpd.html
/usr/share/doc/apache2-doc/manual/es/programs/httpd.html
/usr/share/doc/apache2-doc/examples/apache2/original/httpd.conf.gz
/usr/share/doc/apache2-doc/examples/apache2/original/extra/httpd-autoindex.conf
/usr/share/doc/apache2-doc/examples/apache2/original/extra/httpd-manual.conf
/usr/share/doc/apache2-doc/examples/apache2/original/extra/httpd-userdir.conf
/usr/share/doc/apache2-doc/examples/apache2/original/extra/httpd-vhosts.conf
/usr/share/doc/apache2-doc/examples/apache2/original/extra/httpd-languages.conf.gz
/usr/share/doc/apache2-doc/examples/apache2/original/extra/httpd-ssl.conf.gz
/usr/share/doc/apache2-doc/examples/apache2/original/extra/httpd-mpm.conf.gz
/usr/share/doc/apache2-doc/examples/apache2/original/extra/httpd-multilang-errordoc.conf
/usr/share/doc/apache2-doc/examples/apache2/original/extra/httpd-default.conf
/usr/share/doc/apache2-doc/examples/apache2/original/extra/httpd-dav.conf
/usr/share/doc/apache2-doc/examples/apache2/original/extra/httpd-info.conf
/usr/share/doc/apache2-doc/examples/apache2/extra/httpd-autoindex.conf
/usr/share/doc/apache2-doc/examples/apache2/extra/httpd-manual.conf
/usr/share/doc/apache2-doc/examples/apache2/extra/httpd-userdir.conf
/usr/share/doc/apache2-doc/examples/apache2/extra/httpd-vhosts.conf
/usr/share/doc/apache2-doc/examples/apache2/extra/httpd-languages.conf.gz
/usr/share/doc/apache2-doc/examples/apache2/extra/httpd-ssl.conf.gz
/usr/share/doc/apache2-doc/examples/apache2/extra/httpd-mpm.conf.gz
/usr/share/doc/apache2-doc/examples/apache2/extra/httpd-multilang-errordoc.conf
/usr/share/doc/apache2-doc/examples/apache2/extra/httpd-default.conf
/usr/share/doc/apache2-doc/examples/apache2/extra/httpd-dav.conf
/usr/share/doc/apache2-doc/examples/apache2/extra/httpd-info.conf
/etc/apache2/httpd.conf
/etc/apache2/httpd.pem
root#...:~# whereis httpd
httpd:
root#...:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 12.04.5 LTS
Release: 12.04
Codename: precise
root#...:~# ls /etc/init.d
README hostname network-interface-container plymouth-upstart-bridge rc spamassassin udevtrigger
apache2 hwclock network-interface-security portmap rc.local ssh umountfs
bind9 hwclock-save networking portmap-wait rcS stop-bootlogd umountnfs.sh
bluetooth keymap.sh ondemand postfix reboot stop-bootlogd-single umountroot
bootlogd killprocs passwd procps rpcbind-boot sudo unattended-upgrades
console-screen.sh klogd pc-remote psa rsync sw-cp-server urandom
courier-imap module-init-tools plymouth psa-firewall saslauthd sysklogd wide-dhcpv6-client
cron modules_dep.sh plymouth-log psa-firewall-forward screen-cleanup udev x11-common
dbus mongodb plymouth-ready psa-spamassassin sendsigs udev-fallback-graphics xinetd
fetchmail mysql plymouth-splash quota single udev-finish
halt network-interface plymouth-stop quotarpc skeleton udevmonitor
root#...:~# ls /etc/xinetd.d
chargen daytime discard echo ftp_psa poppassd_psa time
Is httpd listed in your /etc/xinetd, /etc/xinet.d, /etc/xinitd.d, or /etc/init.d directory? Also use chkconfig to see if httpd is listed.
# chkconfig --list | grep httpd
If it's not:
# find / -name httpd*
or
# whereis httpd
to find where it might be installed at. Then I'd make a symbolic link to it in (depending on you linux / unix flavor) /etc/xinetd, /etc/xinet.d, /etc/xinitd.d, or /etc/init.d.

Can't add Apache to start at boot with chkconfig (opensuse 13.1)

I'm running opensuse 13.1, and I installed Apache2 as part of LAMP.
Apache2 works just fine, except I can't make it start automatically with boot.
I used "chkconfig -a apache2" command (as described here)
which gives an error "apache2: unknown service".
Appreciate any hints.
Apache2 has been migrated to systemd. chkconfig works only for SysV services.
The following command (apparently) worked for me:
systemctl enable apache2.service
It seems that the OpenSUSE doc is out of sync.
Find the name of the service:
ls -al /etc/init.d/
(Look for apache or httpd)
Then use that name in chkconfig.