Installing Debian 9 (Stretch) LAMP Server v1.0 (page 2)

botond published 2019/06/21, p - 14:13 time

2. page content

 

Continuation

Az on the first page we installed the MariaDB database engine and Apache web server, on this page we continue to build the LAMP server by installing and configuring PHP7 and phpMyAdmin.

 

 

Installing PHP 7

The default is Debian 9 (Stretch) PHP version is 7.0. The latest release of this is a 7.0.33.

Of course, we need to be aware that PHP 7.0 is not the latest PHP branch. However, this description only covers this now, because it is in the official Debian repository. Later articles will cover the installation of newer versions of PHP in parallel.

To install Apache as a module, enter the following apt-get command:

apt-get -y install php7.0 libapache2-mod-php7.0
When using PHP as an Apache module, there are already more advanced server API (SAPI) uses, such as PHP-FPM, which runs the PHP server much more efficiently and securely. However, the primary purpose of this tutorial is to build the simplest web server possible for any (PHP-based) dynamic web page, CMS system We can run.
We will expand this server with many more in the future, including installing and deploying PHP-FPM.

Then restart the apache web server with systemctl command:

systemctl restart apache2.service

You will now have a working PHP system, but we still need to add some things to include the appropriate plugins.

Install MySQL / MariaDB support

For PHP support for our recently installed database engine, install the php7.0 mysql package:

apt-get -y install php7.0-mysql

Installing additional PHP packages

Az apt-cache Use the command to list what packages are still available for PHP 7.0:

apt-cache search php7.0

There is a big list for this, and there are a lot of them to choose from.

The following installation command is recommended for a lot of usable PHP systems:

apt-get -y install \
    php-pear \
    php-imagick \
    php-apcu \
    php-memcache \
    php7.0-curl \
    php7.0-gd \
    php7.0-intl \
    php7.0-imap \
    php7.0-mcrypt \
    php7.0-pspell \
    php7.0-recode \
    php7.0-sqlite3 \
    php7.0-tidy \
    php7.0-xmlrpc \
    php7.0-xsl \
    php7.0-zip \
    php7.0-opcache

Of course, you can install additional packages later if needed at any time.

Once you've installed all the plugins we need, restart Apache:

systemctl restart apache2.service

Enable Embedded PHP Mode (Optional)

By default, Apache is configured so that only php runs PHP code in files with the extension. This is primarily for security reasons. However, we may need to run PHP code embedded in HTML files, for example. In today's modern PHP pages, CMS systems, this is no longer fashionable, but in the case of older websites, there were often such solutions, where PHP code parts were included in the HTML files.

So if you need such a need to run PHP code in HTML files, you should do the following. If we don't need it, skip this.

Create an html file in the web root:

nano /var/www/html/proba.html

And let's include the following:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<title>Próba oldal</title>
	</head>
	<body>
		Helló világ!<br>
		Itt jön egy beágyazott PHP kórdész:<br>
		<?PHP
			echo "PHP fut!";
		?>
	</body>
</html>

And load it into your browser:

http://192.168.1.120/proba.html

PHP Embedded Code Testing - Not yet working

As you can see, PHP code does not run in the first round. To remedy this, open the already mentioned on the first page of the description Virtual hosting our file:

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

And place the lines highlighted in green at the end of the VirtualHost block of the configuration file to look like this:

<VirtualHost *:80>

	[...]
	
	<FilesMatch ".+\.html$">
		SetHandler application/x-httpd-php
	</FilesMatch>

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Then restart Apache:

systemctl restart apache2.service

Then upgrade to our previous trial page in the browser:

PHP Embedded Code Testing - It Works!

And now the PHP code is running in the HTML file.

Of course, this can be handled more flexibly, for example, if we only need it in a specific directory, we can embed said lines in a Directory block. Or, if we have more virtual hosts, then just add what we need, etc.

 

 

PHP testing

Finally, we test our entire PHP system. Create a php file in the web root:

nano /var/www/html/phpinfo.php

Then add the following lines:

1
2
3
<?php
phpinfo();
?>

Save and then run the server on your browser IP Address with the correct URL:

http://192.168.1.120/phpinfo.php

Testing PHP 7

I have marked the Server API line with a green dot, where we can see that "Apache 2.0 Handler" indicates that PHP is currently running as an Apache module. Also, if you scroll down here, you can see all the loaded PHP modules and their settings.

If you need to set something in php.ini and find it here: /etc/php/7.0/apache2/php.ini

 

Installing phpMyAdmin

A phpMyAdmin allows us to conveniently manage our databases, run queries, perform maintenance, etc. on the web. To install it, run the following command:

apt-get -y install phpmyadmin

A few dialogs will appear during installation. First you ask which HTTP server we are using:

Install phpMyAdmin - Select a web server

Select apache2 here.

Then, after a further installation, in the following dialog, you will be asked if you need your own database to run phpMyAdmin and whether you want to configure the phpMyAdmin automatically dbconfig-common via:

Install phpMyAdmin - Automatically configure a database using dbconfig-common

Choose yes here.

You will then be prompted for another password that the phpMyAdmin control user will work with:

Install phpMyAdmin - Request a password for the control user

With this user, phpMyAdmin does the background maintenance, structure synchronizations, etc., so we won't have to deal with it. So press an enter to do this to generate your own password.

The installation is now complete.

Allow root access

Instead of Debian 8 (Jessie) LAMP server for phpMyAdmnin installation, by default - for additional security reasons - root access is not allowed in the phpMyadmin interface, so this has to be set manually for us (this is not the same setting as discussed above). To set it up, run the following command:

echo "UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';FLUSH PRIVILEGES;" | mysql -u root -p

Then prompt us for the MariaDB root password at the command prompt, enter it.

The point of this is that you can log in as root at the command prompt, so that's where you make the root user configure you to log in to phpMyAdmin.

Testing phpMyAdmin

You can now log in to phpMyAdmin as root at the following address:

http://192.168.1.120/phpmyadmin

phpMyAdmin - Login

phpMyAdmin - Home

 

 

Additional security settings (optional)

If we plan to use the command line database in the future, or if we also want to use our shell scripts to manage our databases, securing command-line database use.

In short, this is to set the required username-password pair in a defaults file once, and then set the appropriate permissions on the file. Then you only need to reference this file for all command line or script database operations, where your passwords will not be displayed.

I wrote about this in a more general form in the linked description, so let's see what it looks like in this case.

Open as root the defaults file already created by our database engine:

nano /etc/mysql/debian.cnf

And set the database root password as highlighted in green:

# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = root
password = '<root jelszó>'
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = root
password = '<root jelszó>'
socket = /var/run/mysqld/mysqld.sock
basedir = /usr

Then save it.

You can then avoid asking for the password at the command line, which allows you to automate working with databases in scripts as well. Of course, we can create such defaults files for any database user.

Accordingly, for example, if we were to run the phpMyAdmin root login configuration command now, we would do the following:

echo "UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';FLUSH PRIVILEGES;" | mysql --defaults-file=/etc/mysql/debian.cnf

Even if it can be run by trial, it does no harm if it is run again.

In defaults files, passwords are optional, but you should include plain apostrophes because if the password may contain spaces or special characters, they may not always work without apostrophes.

Finally, for security's sake, let's look at the permissions of our defaults file (though created by the system, so it should be fine):

ls -l /etc/mysql/debian.cnf

And this should look like this:

-rw------- 1 root root 305 jún   21 13:35 /etc/mysql/debian.cnf

This way we can be sure that only the root user can see the file.

 

Download virtual machine

Image of the finished Debian 9 (Stretch) LAMP server virtual machine Download from here.

 

Conclusion

So this would be a completely basic Debian 9 (Stretch) based LAMP server, from which we can now run any PHP and MySQL / MariaDB based dynamic web pages. Later, we will be adding some useful things to make them more widely available.

 

 

Navigation

This description consists of several pages: