Question: What does the `apt-get install ...` command do?

When I enter `apt-get install ...` command, there are some texts appearing on the screen, but that does not have enough information for me. I want to know if any file is created / edited, any service is started and other activities...

Is there any `.sh` file executed when the `apt-get install ...` run? If so, how can I see the content of that `sh` file?

The reason for this question is recently I tried to install tomcat7 with `apt-get install tomcat7`. Everything works fine until I install `tomcat7-admin` (manager web application), the server became unresponsive to any request. I tried this many times, and this always happen.

Answer: 
Mostly, `apt-get` does the following things:

* checks for dependencies (and asks to install them),
* downloads the package, verifies it and then tells `dpkg` to install it.

`dpkg` will:

* extract the package and copy the content to the right location, and check for pre-existing files and modifications on them,
* run [package maintainer scripts](https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html): `preinst`, `postinst`, (and `prerm`, `postrm` before these, if a package is being upgraded)
* execute some actions based on [triggers](https://wiki.debian.org/DpkgTriggers)

You might be interested in the maintainer scripts, which are usually located at `/var/lib/dpkg/info/<package-name>.{pre,post}{rm,inst}`. These are usually shell scripts, but there's no hard rule. For example:

```
$ ls /var/lib/dpkg/info/xml-core.{pre,post}{rm,inst}
/var/lib/dpkg/info/xml-core.postinst
/var/lib/dpkg/info/xml-core.postrm
/var/lib/dpkg/info/xml-core.preinst
/var/lib/dpkg/info/xml-core.prerm

```
There are some fantastic answers here which are better than this short one, but something you might consider to help you get a better understanding of the changes made by a package manager is [Docker](https://www.docker.com). You can diff the changes made in a container using `docker diff <container>` and it will show you all of the changes. This is especially useful for taking a look under the hood to see what `apt-get install` does to a system. A quick search will get you [several](http://www.scriptrock.com/blog/comparing-containers-generating-dockerfiles-guardrail) [resources](https://stackoverflow.com/questions/21200304/docker-how-to-show-the-diffs-between-2-images) to help implement this.