IntroductionThe LAMP server is the cornerstone of Linux web hosting. In the early days of dynamic web content LAMP was what won Linux the crown in the web space, and it still is responsible for powering a very large portion of the Internet's sites.
If you're looking to set up a LAMP stack to host your website, it'd be hard to find a better option to build it on than Debian Stretch. Debian is, after all, well known for its stability, security, and massive package repositories, and Stretch is certainly no exception.
MariaDB(MySQL)To get started, install and setup the database portion of the stack, MariaDB. Traditionally, the "M" in LAMP stands for MySQL. However, MariaDB is a drop-in replacement that isn't controlled by Oracle, so it tends to be a better option.
To install MaridaDB on Stretch, just use
aptto install the packages.
# apt install mariadb-client mariadb-serverDuring the install process, you will be prompted to create a root password for MariaDB. Make sure to choose something as secure as possible, since it will determine, in part, the security of your databases.
Now that the MariaDB server is installed, you can log in as your root user and set up a regular user and a database.
mysql -u root -pMariaDB will then prompt you for the root password that you just set up.
Creating a database is fairly simple. Just run the following.
CREATE DATABASE newdb;You need to create a regular user now to use the database. It is an absolutely terrible idea to use the root user for anything other than managing MariaDB as a whole.
CREATE USER 'username'@'localhost' IDENTIFIED BY 'userpassword';That command creates a regular user that can sign in locally and set that user's password.
In order for that user to be able to use the database that you just created, you need to grant them privileges on it. Since this is a general purpose user for managing everything on this database, it will be given all privileges.
GRANT ALL PRIVILEGES ON newdb.* to 'username'@'localhost';Once that's done, flush all privileges from the console and exit.
FLUSH PRIVILEGES; quitThat's all for the database. Certainly, you can customize any portion of this as you need.
PHPThe next step in getting the LAMP server set up is installing PHP. In the LAMP stack, PHP powers the web content and interacts with the database. To install PHP on Debian Stretch, run the following line.
# apt install php7.0 php7.0-mysqlThat's really all that you need. PHP is now ready to use.
ApacheThe Apache web server is extremely powerful and can be extremely easy to set up or ridiculously difficult, depending how in-depth you wan to go. Because this is just a simple guide, it's going to follow the quickest path for getting a basic server set up.
So, install both the Apache server and the module for PHP support.
# apt install apache2 libapache2-mod-php7.0
Testing Your ServerBy default, Apache will server the contents of
/var/www/htmland will look first for a file called
index.html. Create that file, and place the following line of code in it.
<?php phpinfo(); ?>Open up your browser and type in
localhostin your address bar. If you aren't doing this locally, type your domain name or IP. You should see a long table containing information about your PHP install. At this point, your sever is officially working.
If you want an easy way to manage your database through a graphical web interface, you can install an application called, phpmyadmin. It allows you to manage your database using PHP through your LAMP server. To install it on Stretch, just pull it with
# apt install phpmyadminOnce the package installs, you can navigate in your browser to
localhost/phpmyadminYou will be greeted with a login screen that will accept your database credentials and finally, an interface to work with your database.