Step 1: Install Apache and Allow in Firewall
sudo apt-get update sudo apt-get install apache2
Set Global ServerName to Suppress Syntax Warnings
sudo apache2ctl configtest
Output AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message Syntax OK
Open up the main configuration file with your text edit:
sudo nano /etc/apache2/apache2.conf
Inside, at the bottom of the file, add a ServerName directive, pointing to your primary domain name. If you do not have a domain name associated with your server, you can use your server’s public IP address:
ServerName server_domain_or_IP
Save and close the file when you are finished. by clicking ctrl+x
Next, check for syntax errors by typing:
sudo apache2ctl configtest
Output Syntax OK
Restart Apache to implement your changes:
sudo systemctl restart apache2
You can now begin adjusting the firewall.
Adjust the Firewall to Allow Web Traffic
Next, assuming that you have followed the initial server setup instructions to enable the UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic. You can make sure that UFW has an application profile for Apache like so:
sudo ufw app list
Output Available applications: Apache Apache Full Apache Secure OpenSSH
If you look at the Apache Full profile, it should show that it enables traffic to ports 80 and 443:
sudo ufw app info "Apache Full"
Output Profile: Apache Full Title: Web Server (HTTP,HTTPS) Description: Apache v2 is the next generation of the omnipresent Apache web server. Ports: 80,443/tcp
Allow incoming traffic for this profile:
sudo ufw allow in "Apache Full"
You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser (see the note under the next heading to find out what your public IP address is if you do not have this information already):
http://your_server_IP_address
Step 2: Install MySQL
sudo apt-get install mysql-server
When the installation is complete, we want to run a simple security script that will remove some dangerous defaults and lock down access to our database system a little bit. Start the interactive script by running:
mysql_secure_installation
You will be asked to enter the password you set for the MySQL root account. Next, you will be asked if you want to configure theVALIDATE PASSWORD PLUGIN
.Warning: Enabling this feature is something of a judgment call. If enabled, passwords which don't match the specified criteria will be rejected by MySQL with an error. This will cause issues if you use a weak password in conjunction with software which automatically configures MySQL user credentials, such as the Ubuntu packages for phpMyAdmin. It is safe to leave validation disabled, but you should always use strong, unique passwords for database credentials.
Answer y for yes, or anything else to continue without enabling.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?Press y|Y for Yes, any other key for No:
You'll be asked to select a level of password validation. Keep in mind that if you enter 2, for the strongest level, you will receive errors when attempting to set any password which does not contain numbers, upper and lowercase letters, and special characters, or which is based on common dictionary words.There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary filePlease enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
If you enabled password validation, you'll be shown a password strength for the existing root password, and asked you if you want to change that password. If you are happy with your current password, enter n for "no" at the prompt:Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
For the rest of the questions, you should press Y and hit the Enter key at each prompt. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.At this point, your database system is now set up and we can move on.
Step 3: Install PHP
PHP is the component of our setup that will process code to display dynamic content. It can run scripts, connect to our MySQL databases to get information, and hand the processed content over to our web server to display.
We can once again leverage the
apt
system to install our components. We're going to include some helper packages as well, so that PHP code can run under the Apache server and talk to our MySQL database:
sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql
This should install PHP without any problems. We'll test this in a moment.In most cases, we'll want to modify the way that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache will first look for a file called
index.html
. We want to tell our web server to prefer PHP files, so we'll make Apache look for anindex.php
file first.To do this, type this command to open the
dir.conf
file in a text editor with root privileges:
sudo nano /etc/apache2/mods-enabled/dir.conf
It will look like this:/etc/apache2/mods-enabled/dir.conf
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
We want to move the PHP index file highlighted above to the first position after theDirectoryIndex
specification, like this:/etc/apache2/mods-enabled/dir.conf
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
When you are finished, save and close the file by pressing Ctrl-X. You'll have to confirm the save by typing Y and then hit Enter to confirm the file save location.After this, we need to restart the Apache web server in order for our changes to be recognized. You can do this by typing this:
sudo systemctl restart apache2
We can also check on the status of theapache2
service usingsystemctl
:
sudo systemctl status apache2
Sample Output● apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Wed 2016-04-13 14:28:43 EDT; 45s ago
Docs: man:systemd-sysv-generator(8)
Process: 13581 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
Process: 13605 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
Tasks: 6 (limit: 512)
CGroup: /system.slice/apache2.service
├─13623 /usr/sbin/apache2 -k start
├─13626 /usr/sbin/apache2 -k start
├─13627 /usr/sbin/apache2 -k start
├─13628 /usr/sbin/apache2 -k start
├─13629 /usr/sbin/apache2 -k start
└─13630 /usr/sbin/apache2 -k startApr 13 14:28:42 ubuntu-16-lamp systemd[1]: Stopped LSB: Apache2 web server.
Apr 13 14:28:42 ubuntu-16-lamp systemd[1]: Starting LSB: Apache2 web server...
Apr 13 14:28:42 ubuntu-16-lamp apache2[13605]: * Starting Apache httpd web server apache2
Apr 13 14:28:42 ubuntu-16-lamp apache2[13605]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerNam
Apr 13 14:28:43 ubuntu-16-lamp apache2[13605]: *
Apr 13 14:28:43 ubuntu-16-lamp systemd[1]: Started LSB: Apache2 web server.
Install PHP Modules
To enhance the functionality of PHP, we can optionally install some additional modules.
To see the available options for PHP modules and libraries, you can pipe the results of
apt-cache search
intoless
, a pager which lets you scroll through the output of other commands:
apt-cache search php- | less
Use the arrow keys to scroll up and down, and q to quit.The results are all optional components that you can install. It will give you a short description for each:
libnet-libidn-perl - Perl bindings for GNU Libidn
php-all-dev - package depending on all supported PHP development packages
php-cgi - server-side, HTML-embedded scripting language (CGI binary) (default)
php-cli - command-line interpreter for the PHP scripting language (default)
php-common - Common files for PHP packages
php-curl - CURL module for PHP [default]
php-dev - Files for PHP module development (default)
php-gd - GD module for PHP [default]
php-gmp - GMP module for PHP [default]
…
:
To get more information about what each module does, you can either search the internet, or you can look at the long description of the package by typing:
apt-cache show package_name
There will be a lot of output, with one field calledDescription-en
which will have a longer explanation of the functionality that the module provides.For example, to find out what the
php-cli
module does, we could type this:
apt-cache show php-cli
Along with a large amount of other information, you'll find something that looks like this:
Output…
Description-en: command-line interpreter for the PHP scripting language (default)
This package provides the /usr/bin/php command interpreter, useful for
testing PHP scripts from a shell or performing general shell scripting tasks.
.
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
open source general-purpose scripting language that is especially suited
for web development and can be embedded into HTML.
.
This package is a dependency package, which depends on Debian's default
PHP version (currently 7.0).
…
If, after researching, you decide you would like to install a package, you can do so by using theapt-get install
command like we have been doing for our other software.If we decided that
php-cli
is something that we need, we could type:
sudo apt-get install php-cli
If you want to install more than one module, you can do that by listing each one, separated by a space, following theapt-get install
command, like this:
sudo apt-get install package1 package2 ...
At this point, your LAMP stack is installed and configured. We should still test out our PHP though.Step 4: Test PHP Processing on your Web Server
In order to test that our system is configured properly for PHP, we can create a very basic PHP script.
We will call this script
info.php
. In order for Apache to find the file and serve it correctly, it must be saved to a very specific directory, which is called the "web root".In Ubuntu 16.04, this directory is located at
/var/www/html/
. We can create the file at that location by typing:
sudo nano /var/www/html/info.php
This will open a blank file. We want to put the following text, which is valid PHP code, inside the file:info.php
When you are finished, save and close the file.Now we can test whether our web server can correctly display content generated by a PHP script. To try this out, we just have to visit this page in our web browser. You'll need your server's public IP address again.
The address you want to visit will be:
http://your_server_IP_address/info.php
The page that you come to should look something like this:
Step One — Install phpMyAdmin
To get started, we can simply install phpMyAdmin from the default Ubuntu repositories.
We can do this by updating our local package index and then using the
apt
packaging system to pull down the files and install them on our system:
sudo apt-get update
sudo apt-get install phpmyadmin php-mbstring php-gettext
This will ask you a few questions in order to configure your installation correctly.Warning: When the first prompt appears, apache2 is highlighted, but not selected. If you do not hit Space to select Apache, the installer will not move the necessary files during installation. Hit Space, Tab, and then Enter to select Apache.
- For the server selection, choose apache2.
- Select yes when asked whether to use
dbconfig-common
to set up the database - You will be prompted for your database administrator's password
- You will then be asked to choose and confirm a password for the
phpMyAdmin
application itself
The installation process actually adds the phpMyAdmin Apache configuration file into the /etc/apache2/conf-enabled/
directory, where it is automatically read.
The only thing we need to do is explicitly enable the PHP mcrypt
and mbstring
extensions, which we can do by typing:
sudo phpenmod mcrypt
sudo phpenmod mbstring
Afterwards, you'll need to restart Apache for your changes to be recognized:
sudo systemctl restart apache2
You can now access the web interface by visiting your server's domain name or public IP address followed by/phpmyadmin
:https://domain_name_or_IP/phpmyadmin
You can now log into the interface using the
root
username and the administrative password you set up during the MySQL installation.When you log in, you'll see the user interface, which will look something like this:
Step Two — Secure your phpMyAdmin Instance
We were able to get our phpMyAdmin interface up and running fairly easily. However, we are not done yet. Because of its ubiquity, phpMyAdmin is a popular target for attackers. We should take extra steps to prevent unauthorized access.
One of the easiest way of doing this is to place a gateway in front of the entire application. We can do this using Apache's built-in
.htaccess
authentication and authorization functionalities.Configure Apache to Allow .htaccess Overrides
First, we need to enable the use of
.htaccess
file overrides by editing our Apache configuration file.We will edit the linked file that has been placed in our Apache configuration directory:
sudo nano /etc/apache2/conf-available/phpmyadmin.conf
We need to add anAllowOverride All
directive within thesection of the configuration file, like this:
/etc/apache2/conf-available/phpmyadmin.conf
Options FollowSymLinks
DirectoryIndex index.php
AllowOverride All
. . .
When you have added this line, save and close the file.To implement the changes you made, restart Apache:
sudo systemctl restart apache2
Create an .htaccess File
Now that we have enabled
.htaccess
use for our application, we need to create one to actually implement some security.In order for this to be successful, the file must be created within the application directory. We can create the necessary file and open it in our text editor with root privileges by typing:
sudo nano /usr/share/phpmyadmin/.htaccess
Within this file, we need to enter the following information:/usr/share/phpmyadmin/.htaccessAuthType Basic
AuthName "Restricted Files"
AuthUserFile /etc/phpmyadmin/.htpasswd
Require valid-user
Let's go over what each of these lines mean:
AuthType Basic
: This line specifies the authentication type that we are implementing. This type will implement password authentication using a password file.AuthName
: This sets the message for the authentication dialog box. You should keep this generic so that unauthorized users won't gain any information about what is being protected.AuthUserFile
: This sets the location of the password file that will be used for authentication. This should be outside of the directories that are being served. We will create this file shortly.Require valid-user
: This specifies that only authenticated users should be given access to this resource. This is what actually stops unauthorized users from entering.
When you are finished, save and close the file.
Create the .htpasswd file for Authentication
Now that we have specified a location for our password file through the use of the AuthUserFile
directive within our .htaccess
file, we need to create this file.
We actually need an additional package to complete this process. We can install it from our default repositories:
sudo apt-get install apache2-utils
Afterward, we will have thehtpasswd
utility available.The location that we selected for the password file was "
/etc/phpmyadmin/.htpasswd
". Let's create this file and pass it an initial user by typing:sudo htpasswd -c /etc/phpmyadmin/.htpasswd username
You will be prompted to select and confirm a password for the user you are creating. Afterwards, the file is created with the hashed password that you entered.If you want to enter an additional user, you need to do so without the
-c
flag, like this:sudo htpasswd /etc/phpmyadmin/.htpasswd additionaluser
Now, when you access your phpMyAdmin subdirectory, you will be prompted for the additional account name and password that you just configured:https://domain_name_or_IP/phpmyadmin
![]()
After entering the Apache authentication, you'll be taken to the regular phpMyAdmin authentication page to enter your other credentials. This will add an additional layer of security since phpMyAdmin has suffered from vulnerabilities in the past.
Finally, Do the following steps for rewrite mod and admin 404 issue
sudo a2enmod rewrite
and restart the apache
sudo service apache2 restart
To use mod_rewrite from within .htaccess files (which is a very common use case), edit the default VirtualHost with
sudo nano /etc/apache2/sites-available/000-default.conf
Below “DocumentRoot /var/www/html” add the following lines:
AllowOverride All
Restart the server again:
sudo service apache2 restart
How to downgrade PHP version:
sudo apt-get install php5.6
sudo apt-get install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install mcrypt php5.6-mcrypt
sudo a2dismod php7.2
sudo en2mod php5.6
https://www.tecmint.com/install-lamp-with-phpmyadmin-in-ubuntu-18-04/
0 Comments