LAMP Server on CentOS 7 with PHP-FPM

CentOS 7 is available on Linode. It emploies Apache 2.4 as the default web server, which provides a novel MPM event mode said to be as efficient as Nginx. This document describes a way to install a Linux, Apache, MariaDB and PHP with PHP-FPM server, also known as LAMP stack, on CentOS 7 step by step. It also includes PHP-FPM related configuration for phpMyAdmin.

Make sure that before starting this guide you have read through and completed our Getting Started guide.

Set the hostname

Firstly, let’s ensure that the hostname of the system is correctly set as described in Setting Your Hostname section of the Getting Started guide. Using following commands to verify.

To show your short hostname,

hostname

To show your fully qualified domain name (FQDN),

hostname -f

System preparation

Update your system first,

yum update

This makes your system and all the software running at the latest version.

Install Apache

Apache 2.4 is included in default CentOS repository. To install it,

yum install httpd

Then, backup all the default configuration files for future references,

mkdir ~/confbak
cp -R /etc/httpd ~/confbak

Config Apache

Enable Apache Service

Apache as a web server needs to be enabled as system service and started along with system. This can be achieved by using systemctl commands.

Enable httpd as a service,

systemctl enable httpd.service

Then, start httpd,

systemctl start httpd

To check the status of Apache to make sure it is running by issuing,

systemctl status httpd

Add service port in firewall

In the meantime, firewall should be configured to allow corresponding traffic. Following command will enable port 80 permanently but only come into effect after reboot,

firewall-cmd --add-service=http --permanent

If a reboot are not wanted at this moment, following command can be issued additionally,

firewall-cmd --add-service=http

Configure Apache Virtual Hosts

Once Apache is installed and enabled, Server directives need to be configured to specify server blocks for hosts. Each server block is enclosed in a <VirtualHost> directive. All directives need to be loaded by Apache while httpd service starts everytime. Apache has a default directory to store virtual host configuration files, which is /etc/httpd/conf.d/. By default, all files in this directory ended in .conf will be loaded by Apache. So, if there are more than one server blocks, they can be stated in one file /etc/httpd/conf.d/vhosts.conf, or in separated files, such as /etc/httpd/conf.d/{domain}.conf. In this post, the later way will be used as example.

/etc/httpd/conf.d/example.com.conf
<VirtualHost *:80>
ServerName example.com
DocumentRoot /srv/www/example.com/public_html

<IfModule mpm_event_module>
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/srv/www/example.com/public_html/$1 </IfModule> 

<Directory "/srv/www/example.com/public_html/"> 
    DirectoryIndex index.php index.html index.htm 
</Directory> 

ErrorLog /srv/www/example.com/logs/example.com-error_log 
TransferLog /srv/www/example.com/logs/example.com-access_log </VirtualHost>

Please note that the code snippet,

<IfModule mpm_event_module>
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/srv/www/example.com/public_html/$1 
</IfModule>

is for PHP-FPM to work when MPM event mode is activated in Apache.

Any other websites (domains) could be added as new files in the /etc/httpd/conf.d/ directory. And please make sure the string behind DocumentRoot and other occurrences are changed correspondingly.

After all the configurations are set, directories should be created respectively by issuing following commands,

mkdir -p /srv/www/example.com/public_html
mkdir /srv/www/example.com/logs

and,

mkdir -p /srv/www/{domain}/public_html
mkdir /srv/www/{domain}/logs

for any other domains.

Then, apache service can be restart to make the directives effective,

systemctl restart httpd

Now, the server can serve static contents in each public_html directory.

Deploy PHP with PHP-FPM

Install PHP with PHP-FPM

PHP-FPM is integrated with PHP now. So, it is very easy to install by issuing,

yum install php php-fpm

The yum should resolve the dependency problem by installing several another packages.

After installation, backup all the default configuration files,

cp /etc/php.ini ~/confbak/php.ini.bak
cp /etc/httpd/conf.d/php.conf ~/confbak/httpd/conf.d/php.conf.bak
cp /etc/httpd/conf.modules.d/10-php.conf ~/confbak/httpd/conf.modules.d/10-php.conf.bak
cp /etc/php-fpm.conf ~/confbak/php-fpm.conf.bak
cp -R /etc/php-fpm.d ~/confbak/php-fpm.d

Adjust configurations

To enable PHP-FPM, some default configurations should be adjusted to be compatible with it.

Firstly, edit /etc/httpd/conf.d/php.conf to make the last several lines looks like,

<IfModule mpm_prefork_module>
#
# Apache specific PHP configuration options
# those can be override in each configured vhost
#
php_value session.save_handler "files"
php_value session.save_path "/var/lib/php/session"
</IfModule>

Secondly, edit /etc/httpd/conf.modules.d/00-mpm.conf, and add a # at the beginning of  following line,

LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

and then, delete the # of following line,

#LoadModule mpm_event_module modules/mod_mpm_event.so

Thirdly, add php-fpm as a system service and start it,

systemctl enable php-fpm.service
systemctl start php-fpm

The server should work with PHP now. There are some further informations about PHP-FPM on Apache WiKi which are really good to know.

Install and configure MariaDB

CentOS 7 switched from MySQL to MariaDB for its default database software. MariaDB has the ability to be compatible with MySQL database and its configuration settings. To install MariaDB database use the following command,

yum install mariadb mariadb-server php-mysql

This will install PHP support as well.

Then, add MariaDB as system service and start it,

systemctl enable mariadb.service
systemctl start mariadb

Use mysql_secure_installation script to secure database, just like the operation with MySQL.

mysql_secure_installation

Sample output:

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
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!

After this, login to MariaDB using root account and password just set above,

mysql -u root -p

To create a database named “mydatabase”, and a database user “dbuser” with password “mypassword”, then grant full permissions to the “mydatabase” database for the user “dbuser”, just input the following commands,

MariaDB > CREATE DATABASE mydatabase;
MariaDB > CREATE USER 'dbuser' IDENTIFIED BY 'mypassword';
MariaDB > GRANT ALL PRIVILEGES ON mydatabase.* to 'dbuser';

Now quit MariaDB,

MariaDB > quit

Install phpMyAdmin

Official CentOS 7 repositories does not provide phpMyAdmin package. Fortunately, EPEL (Extra Packages for Enterprise Linux) repository provides it.

Install EPEL repository,

yum install http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
yum update
yum install phpmyadmin

Backup default configuration file,

cp /etc/httpd/conf.d/phpMyAdmin.conf ~/confbak/httpd/conf.d/phpMyAdmin.conf.bak

By defaulty, phpMyAdmin can only accessed by localhost. The configuration file /etc/httpd/conf.d/phpMyAdmin.conf should be editted to allow access remotely by adding following statement,

Require all granted

as a new line right next the lines of,

Require ip ::1

Because phpMyAdmin is virtually installed in the sub directory of main host, here are two specific ProxyPassMatch rules for it. The following rules can be added right before the ProxyPassMatch rule state in the example.com.conf file,

ProxyPassMatch ^/phpMyAdmin/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/srv/www/example.com/public_html/phpMyAdmin/$1
ProxyPassMatch ^/phpMyAdmin((.*)?/)$ fcgi://127.0.0.1:9000/srv/www/example.com/public_html/phpMyAdmin$1index.php

After restart httpd service, phpMyAdmin should be accessable from example.com/phpMyAdmin/ now.

Congratulations! You now have a fully functioning LAMP stack on CentOS 7!©

本文发表于水景一页。永久链接:<https://cnzhx.net/blog/lamp-server-on-centos-7-with-php-fpm/>。转载请保留此信息及相应链接。

2 条关于 “LAMP Server on CentOS 7 with PHP-FPM” 的评论

雁过留声,人过留名

您的电子邮箱地址不会被公开。 必填项已用 * 标注

特别提示:与当前文章主题无关的讨论相关但需要较多讨论求助信息请发布到水景一页讨论区的相应版块,谢谢您的理解与合作!请参考本站互助指南
您可以在评论中使用如下的 HTML 标记来辅助表达: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>