The Linux, Apache, MySQL and PHP (LAMP) stack is a versatile application environment. Resource use and configuration is minimal, making it a prime candidate for commodity hosting environments. Deployment is easy, and ranges from making simple in-place edits to full virtualization and containerization. Developers are familiar with LAMP, and it is simple to gain and acquire talent to build and launch LAMP-based applications and sites. This guide will deploy a secure and capable LAMP stack on the CentOS 7 operating system.
Getting Started
Before you begin, make sure you have the following:
• 1 server (Cloud Server or Dedicated Server) running a fresh installation of CentOS 7
At this guide’s conclusion, this server will be capable of hosting one or more LAMP-based sites.
Tutorial
Let’s go. Start by updating all the installed packages on your server. This will apply all current bugfixes and security patches. When complete, reboot so the new server environment is active. Perform this step regularly to keep your system updated and secure.
yum update -y && shutdown -r now
We’ll begin by installing the Apache web server. Apache will both serve up your content and interpret PHP scripts. This is easier to reason about than other solutions, which split PHP execution into separate processes.
yum install httpd
Now we start the Apache server.
systemctl start httpd.service
We must also enable it to start on boot, otherwise it will not launch automatically when the system restarts.
systemctl enable httpd.service
Next we’ll install the database. Instead of MySQL, we’ll use MariaDB. MariaDB is a drop-in MySQL replacement that is backwards-compatible with MySQL and all of its tooling. It includes a number of additional storage engines and scalability features that make it a better fit for large-scale database use.
Start by installing the mariadb package.
yum install mariadb-server mariadb
Again, we’ll start the server itself.
systemctl start mariadb
We now need to set the MySQL root password, which is needed for administering databases and permissions. When asked for your current root password, simply press enter. Keep this password in a secure place, as you’ll need it whenever adding or modifying databases.
mysql_secure_installation
Set root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
You can now enable MariaDB to start on boot.
systemctl enable mariadb.service
Next we install the PHP runtime. We’ll begin by installing the base package needed by all PHP scripts.
yum install php php-mysql
With PHP installed, Apache must be restarted so it integrates with the newly-installed runtime.
systemctl restart httpd.service
PHP is an incredibly modular language. Chances are high that you’ll need a number of extensions to deploy or develop PHP apps. Many of these modules are packaged for CentOS. Here we show how to use Yum to find and install the php-gd module. Use this for any modules your PHP site requires.
yum search php-
php-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-dba.x86_64 : A database abstraction layer module for PHP applications
php-devel.x86_64 : Files needed for building PHP extensions
php-embedded.x86_64 : PHP library for embedding in applications
php-enchant.x86_64 : Enchant spelling extension for PHP applications
php-fpm.x86_64 : PHP FastCGI Process Manager
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php-intl.x86_64 : Internationalization extension for PHP applications
php-ldap.x86_64 : A module for PHP applications that use LDAP
php-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
php-mysql.x86_64 : A module for PHP applications that use MySQL databases
......
yum install php-gd -y
We’re almost done. Let’s test the installation to ensure everything is working.
cd /var/www/html
In CentOS, the document root directory is /var/www/html. All of your PHP scripts must be located here by default in order to be run by Apache.
To test, we’ll create a PHP info file at index.php. This will use a simple function call to display lots of information about your PHP environment. More fundamentally, it will ensure that Apache is executing PHP scripts. The file should be called index.php.
nano index.php
<?php
phpinfo();
?>
To test your environment, access your server at http://your_ip. If that doesn’t work, try http://your_ip/index.php. If all goes well, you’ll see lots of PHP diagnostic details about the environment and loaded modules.
Conclusion
You now have a flexible environment into which you can deploy any number of PHP sites. Simply upload a PHP application, follow its installation instructions, and easily run blogs, wikis or other powerful tools and services. If you found this article helpful, feel free to share it with your friends and let us know in the comments below!