Installing / Re-Installing Apache, PHP, libapache2-mod-php

The following is done on an Ubuntu machine but hoping it should cover other Debian instances.

I accidentally deleted php7.0.conf and php7.0.load from the mods-available folder. These files come with libapache2-mod-php. I uninstalled the module and installed it again – but the files didn’t appear. A lot of blogs suggested to remove Apache, PHP and libapache2-mod-php altogether and installing it again and it didn’t work. I used remove, purge and everything suggested but didn’t help. Eventually, I had to search for the installed module by:

dpkg --get-selections | grep libapache2-mod-php

I installed the module above as is but the result showed me libapache2-mod-php7.0. So I went ahead and removed and purged the module listed after which I installed the module again (without the 7.0 suffix) and I was able to recover the files again.

apt-get remove --purge libapache2-mod-php7.0
apt-get install libapache2-mod-php

Since I am covering this topic, let’s talk about installing Apache on Debian and the supported PHP packages along with SSL support.

apt-get install apache2
apt-get install php
apt-get install php-curl
apt-get install libapache2-mod-php

Enable SSL on the site:

cd /etc/apache2/sites-enabled
ln -fs ../sites-available/default-ssl.conf 

Finally, add the necessary modules to support PHP/SSL on Apache

cd /etc/apache2/mods-enabled
ln -fs ../mods-available/ssl.conf
ln -fs ../mods-available/ssl.load 
ln -fs ../mods-available/socache_shmcb.load
ln -fs ../mods-available/php7.0.conf
ln -fs ../mods-available/php7.0.load

Do not forget to restart apache with

service apache2 restart

.htaccess – Force www redirect

I recently found that having and on your website would affect SEO (Search Engine Optimization). It’s recommended to use either. So I chose the latter. This can be easily done by having a .htaccess rule on your root directory.

# Force www redirect #
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301,NC]

301 is the HTTP status code for a Permanent Redirect which doesn’t affect the SEO ratings.

(98)Address already in use: make_sock: could not bind to address [::]:80

I tried to reproduce how the system got in this situation and wasn’t able to. I did find the system in the same state as you reported it, but I couldn’t figure out how it got like that.

Basically when you ran “apachectl start”, Apache was already running. When Apache is being started (ie: apachectl start), it first checks to see if it is already running by checking or a PID file (Process ID file). If it sees a file, it will report this:

staging-aops:/var/run# apachectl start
httpd (pid 23390) already running

In this case, Apache *WAS* running, but there was NO PID file to be found, so Apache assumed it was OK to start. So it began to start but noticed that it couldn’t bind to port 80 because that port was already in-use by another process (which was Apache), and that’s the error message you received:

(98)Address already in use: make_sock: could not bind to address [::]:80

So what I had to do was manually kill Apache (given there wasn’t a PID file so it didn’t think it was even running), then I started Apache, noticed it created a PID file, then I stopped Apache (and noticed the PID file was deleted, as it was supposed to be). I then started Apache again and it created the PID file just fine and started.

I was not able to reproduce the problem where Apache was running yet the PID file didn’t exist. I am not sure how this is happening. The PID file is located at: /var/run/ Did someone delete the PID file in an effort to fix a problem? Sometimes that is required. If Apache were to be killed or somehow die and the PID file didn’t get cleaned up (deleted), then the PID file will have to be manually deleted before Apache can be started. But in this case it was the reverse – Apache WAS running w/no PID file.

To fix this in the future, you can:

1)   ps -ef | grep apache2 | grep root | grep -v grep    (which should output something like this):

         root     23741     1  0 13:54 ?        00:00:00 /usr/sbin/apache2 -k start

NOTE: The reason I grepped for “root” was because Apache uses the ROOT user for the initial parent process. This gives Apache the authority to do its job, running as user ROOT, and each subsequent Apache process that is spawned (child process) is now running as the “www-data” user. In order to kill all Apache processes in one step, you want to kill the parent (which is the only one running as user “root”). Killing that PID should also kill all child processes

The number to the RIGHT of “root” (23741, in this example), is the PID#. You can then do:

2)  kill -9 23741  (PID will vary each and every time you perform these steps, so do not try and reuse the 23741 number but the actual number from step# 1
3)  rm /var/run/      (Make sure NO PID file exists - it shouldn't, but just in case)
4)  apachectl start   

If you have this issue again, going through those 4 steps should surely fix the problem.

Apache is running now and there is a PID file now – so all should be good. You can “apachectl stop”, “apachectl start” or “apachectl restart” and all are working now.

Terminate local bottle server

I had my local bottle server running and my Terminal shut down. After reboot, I wasn’t able to start the server again because it was in use. I had to kill the open port connection. My bottle was running on 8081 so I searched for protocols using that port:

netstat -nap | grep 8081
tcp   0    0*      LISTEN      6491/python

Next I killed the PID by executing:

kill -9 6491

Force https on apache

Force https on apache or redirect http traffic to https site. Here’s a simple configuration that can be done within the .htaccess file on the site directory (do this in the site’s root directory if https is to be enforced for the entire site).

<IfModule mod_rewrite.c>
  RewriteEngine on
  Redirect permanent / https://%{HTTP_HOST}%/
  RewriteRule %{HTTPS} !=on
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Alternatively, you can modify the site configuration under /etc/apache2/sites-available/non-https-site

Add php code to .html file types

I wanted to add some php code to an existing static html page. But I noticed that the PHP content didn’t render on the browser in the first place. It would only work if I renamed the file extension from .html to .PHP.

In order to have the php content work in an HTML file extension, the following piece of configuration is required within the .htaccess file on your server. This can be created on the site’s root directory and the properties are inherited by all the sub-directories.

AddType application/x-httpd-php .html