Today we are going to see how to install apache, mysql, php, phpmyadmin and other necessary things required to run a web server perfectly.
Apache
Apache is the most popular web server in the world. Its a great default choice for hosting a website. Lets see how to install apache.
First you should update ubuntu repositories using below command.
sudo apt update
Now install apache and other packages
sudo apt install apache2
sudo is used to execute these commands with root privileges. It will ask you for your regular user’s password to verify your intentions. And if you already have root privileges then you don’t need to use sudo .
Before command execution, apt show how much extra disk space need to install this package and ask confirmation to proceed or deny. Press Y and hit Enter to continue, and the installation will proceed.
After apache installed, you need to edit apache config file /etc/apache2/apache2.conf to set server ServerName , root directory path and other several settings.
Now restart apache to reflect changes on config:
sudo service apache2 restart
Test Apache
Access server address in browser or localhost in case of local system.
http://your_server_IP_address OR http://localhost
you will see apache default page.
MySQL
sudo apt install mysql-server
This will again ask for extra disk space and you need to press Y and hit Enter to continue.
During the installation, your server will ask you to select and confirm a password for the MySQL “root” user. This is an administrative account in MySQL that has increased privileges.
PHP
sudo apt install php libapache2-mod-php php-mcrypt php-mysql
You can modify /etc/apache2/mods-enabled/dir.conf to tell apache how to treat files. By default index.html is called when we access folder. We can change it to index.php .
sudo nano /etc/apache2/mods-enabled/dir.conf
this file looks like this:
<IfModule mod_dir.c> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm </IfModule>
here you can change files preferences. and restart apache sudo service apache2 restart
Test PHP
To test php, go to apache web root folder /var/www/html and create a file info.php and put this code on it.
<?php phpinfo(); ?>
now access this file from browser
http://your_server_IP_address/info.php OR http://localhost/info.php
The page you will see looks like this:
phpMyAdmin
phpMyAdmin provide GUI interface to manage mysql database.
sudo apt install phpmyadmin php-mbstring php-gettext
This will ask you a few questions in order to configure your installation correctly.
- For the server selection, choose apache2
- Select Yes when asked whether to use dbconfig-common to set up the database
- You will then be asked to choose and confirm a MySQL application password for phpMyAdmin
The installation process adds the phpMyAdmin Apache configuration file into the /etc/apache2/conf-enabled/ directory, where it is read automatically. The only thing you need to do is explicitly enable the mbstring PHP extension, which you can do by typing:
sudo phpenmod mbstring
Now restart apache server:
sudo systemctl restart apache2
phpMyAdmin is now installed and configured. However, before you can log in and begin interacting with your MySQL databases, you will need to ensure that your MySQL users have the privileges required for interacting with the program.
Adjusting User Authentication and Privileges
When you installed phpMyAdmin onto your server, it automatically created a database user called phpmyadmin which performs certain underlying processes for the program. Rather than logging in as this user with the administrative password you set during installation, it’s recommended that you log in as either your root MySQL user or as a user dedicated to managing databases through the phpMyAdmin interface.
Configuring Password Access for the MySQL Root Account
In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program — like phpMyAdmin — to access the user.
In order to log in to phpMyAdmin as your root MySQL user, you will need to switch its authentication method from auth_socket to mysql_native_password if you haven’t already done so. To do this, open up the MySQL prompt from your terminal:
sudo mysql
Next, check which authentication method each of your MySQL user accounts use with the following command:
SELECT user,authentication_string,plugin,host FROM mysql.user;
Output +------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | root | | auth_socket | localhost | | mysql.session | *9364d39DE5F65ADC4A4547Dg4591d63B9874D23H | mysql_native_password | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint | *6356475SE6F65AWS64B965CA428363B64746AS33 | mysql_native_password | localhost | | phpmyadmin | *4364d39DW5F65ABC4A4547Qg4591X63A9874F23N | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 5 rows in set (0.00 sec)
In this example, you can see that the root user does in fact authenticate using the auth_socket plugin. To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:
FLUSH PRIVILEGES;