Monday, June 1, 2020

How to install apache with php on Ubuntu

The cross platform supported apache web Server is a free and open source server.  From the linux repository we can install it easily. Generally, it works with PHP language and MySql Database. Other’s platform are also supported.

In this lesson, we will install apache web server on Ubuntu 16.04 machine. The command also work for other releases. But there is some exception. Some of the command may not applicable for other releases.

All the command must be run with root privileges. To install apache web server from the repository, please run the following command:-

apt-get  –y  install  apache2

After the installation complete, we have to configure the apache web server. To configure web server, we have to modify some couple of files. In this tutorial, we will use “vi” text editor for file modification.

First of all we will configure the server token setting for security reason. This setting is responsible for what you want to return as the server HTTP response header. The default setting is “full”, where conveys the most information. That’s why, most of the server information are publicly available. Now, we are going to change that to “Prod”, where conveys the least information. The file name we have to modify is security.conf which is available at “/etc/apache2/conf-enabled/” path. To opening this file the complete command will be:-

vi /etc/apache2/conf-enabled/security.conf

Now find out the “ServerTokens” setting and change it’s value to “prod” and also find out the “ServerSignatue” setting and change it’s value to “off”. This "ServerSignature" option is responsible for displaying the apache version and operating system information, which must be considered as security threat.  As like below:-

ServerTokens prod

ServerSignature off

Now we are going to change the “directory index” value. By using this value, we are telling our server that, what will be the default page if a user browse our web server. We can set the default page to index.html or index.php whatever we like. For that, we have to configure “dir.conf” file, which located at “/etc/apache2/mods-available/”. To opening this file, the complete command will be:-

vi /etc/apache2/mods-available/dir.conf

Now change the “DirectoryIndex” value to “index.php” or “index.html”, whatever you want. 

DirectoryIndex index.php
Or
DirectoryIndex index.html

You can keep multiple file type if you want. But for the security reason, it is recommended to keep only one file type. This is look like as follows:-

DirectoryIndex index.html index.php index.cgi

Now we are going to modify our server name. we can change it by editing the “apache2.conf” file which located at “/etc/apache2/”. The command will be the as follows:-

vi /etc/apache2/apache2.conf

Now find out the “ServerName” setting  and change it’s value as your requirement.

ServerName server.example.com

Now we are going to set the server admin email address, so that if anything goes wrong, apache can send an email to it’s admin. To set that open the “000-default.conf” file located at “/etc/apache2/sites-enabled/”. Now open this file with “vi” text editor and change it’s “ServerAdmin” address.

vi /etc/apache2/sites-enabled/000-default.conf

Now go to the “ServerAdmin” line and change it’s value as yours. That will be as follows:-

ServerAdmin webmaster@example.com

We are done. Now we have to restart our apache service. To restart that, please use the following command:-

systemctl restart apache2

Now open your browser and go to your website by using your hostname or ip address. Such as:-

http:// {type your hostname or your ip address}/

Now the apache webserver default page should be appeared. This time we are going to start installation process for PHP to work along with apache web server 

PHP is stand for “PHP Hypertext Preprocessor”. It is an open source server side scripting language and It was specially developed for web environment. In this lesson, we will discuss about how to configure PHP to work with apache web server.

First of all we have to install PHP along with all required modules. All command must be executed with root privileges. To install PHP, please run the following command:-

apt-get  –y install php

apt-get  –y install php7.0   [for php 7.0 version]

After the installation complete, we will forward to install its module. The module which is required to work with apache web server are as follows:-

php-cgi, libapache2-mod-php, php-common, php-pear, php-mbstring

If we want, it is possible to install all of those modules together or separately. To install together all of those please run the following command:-

apt-get  –y  install php-cgi   libapache2-mod-php  php-common  php-pear  php-mbstring

apt-get  –y  install php7.0-cgi  libapache2-mod-php  php7.0-common  php-pear  php7.0-mbstring   [for php 7.0 version]

After the installation complete, we have to configure apache web server to work with PHP scripting language. To configure apache for PHP language, please execute the following command.

a2enconf php7.0-cgi      [you don’t need to type full command, just press {tab} button. Here "7.0" is the PHP version. It will be varied as your PHP version]

Now we have to restart apache service. To restart run the command:-

Service apache2 reload

Now we have to configure the “time zone” setting. This time zone setting is available in the file named php.ini. This file is located at three different path. We are using “vi” text editor to open this file.  The file path are as follows:-

vi  /etc/php/7.0/cli/php.ini

vi  /etc/php/7.0/fpm/php.ini

vi  /etc/php/7.0/apache2/php.ini


After opening this file, please find out the “date.timezone” section and  replace its value by your time zone. This will be as follows:-

date.timezone  =  Asia/Tokyo 

All is done. Now restart your apache service to take effect with apache server.

Now keep your php file at the web server root directory. Generally, the directory path is “/var/www/html/”. You can check your current web server root directory path from the following file:-

vi   /etc/apache2/sites-available/000-default.conf

Now go to the “DocumentRoot” line. Here you will get the current root directory path. This will be look like as follows:-

DocumentRoot    /var/www/html

If you want you can change your root directory path from here. If everything is going with right way, then your web server is ready now with PHP support. If anything goes wrong, then please check the apache server log file. The file is located here:-

vi /var/log/apache2/error.log

If we view the log file with text editor, then we don't get any concurrent update until we close the file and reopen it. To view log file with concurrent update, we have to use "tail" command. In that case, the complete command will be :-

tail  -f  /var/log/apache2/error.log

Now review your log file to troubleshoot any error.


No comments:

Post a Comment