Prerequisite:
For this tutorial we will be using NGINX as our webserver. If you haven’t installed NGINX yet, please follow our How to install NGINX on CentOS 7 tutorial.
In This tutorial we will look at, how to install PHP and MariaDB on CentOS 7.
Configure Remi repositories for CentOS 7
## Remi Dependency on CentOS 7 and Red Hat (RHEL) 7 ##
$rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
## CentOS 7 and Red Hat (RHEL) 7 ##
$rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
$yum --enablerepo=remi,remi-php72 install php-fpm php-common php
Install required PHP Modules:
Pick the modules you might need for the following and install them to use them with PHP, for more PHP Modules/extensions please visit http://php.net/manual/en/extensions.php
• Memcache (php-pecl-memcache) – Extension to work with the Memcached caching daemon
• Memcached (php-pecl-memcached) – Extension to work with the Memcached caching daemon
• GD (php-gd) – A module for PHP applications for using the gd graphics library
• XML (php-xml) – A module for PHP applications which use XML
• MBString (php-mbstring) – A module for PHP applications which need multi-byte string handling
• MCrypt (php-mcrypt) – Standard PHP module provides mcrypt library support
• OPcache (php-opcache) – The Zend OPcache provides faster PHP execution through opcode caching and optimization.
• APCu (php-pecl-apcu) – APCu userland caching
• CLI (php-cli) – Command-line interface for PHP
• PEAR (php-pear) – PHP Extension and Application Repository framework
• PDO (php-pdo) – A database access abstraction module for PHP applications
• MySQL (php-mysqlnd) – A module for PHP applications that use MySQL databases
$yum --enablerepo=remi,remi-php72 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml
Automatically start PHP upon boot:
$systemctl restart php-fpm.service
$systemctl enable php-fpm.service
Install MariaDB on CentOS 7
MariaDB Server is one of the most popular opensource relational databases. It’s made by the original developers of MySQL and guaranteed to stay open source.
$yum -y install mariadb mariadb-server
Automatically start NGINX upon boot:
$systemctl restart mariadb.service
$systemctl enable mariadb.service
Run the msql/mariadb secure installation script to remove all the default settings and to remove test user and databases. Also set password for the root user.
[root@datahackr]# /usr/bin/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 authorization.
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 any one 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 farwill 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 MariaDBinstallation should now be secure.Thanks for using MariaDB!
Configure NGINX to work with PHP and MariaDB
Latest Fedora Nginx + PHP-FPM builds use custom config, first restore default config.
## Backup nginx default config ##
$cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
$cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
## Modify PHP-FPM to listen ip instead of socket ##
$vi /etc/php-fpm.d/www.conf
## Change following ##;
listen = /run/php-fpm/www.sock
listen = 127.0.0.1:9000
Note: You can of course use socket also, but then change this guide virtual host accordingly.
Create directory layout for your site. I use here testsite.local site, but this could of course be your real site, like https://datahackr.com
public_html directory and logs directory
$mkdir -p /srv/www/testsite.local/public_html
$mkdir /srv/www/testsite.local/logs
$chown -R apache:apache /srv/www/testsite.local$restorecon -r /srv/
Alternative setup to add logs under /var/log directory.
public_html directory and logs directory
mkdir -p /srv/www/testsite.local/public_html
mkdir -p /var/log/nginx/testsite.local
chown -R apache:apache /srv/www/testsite.local
chown -R nginx:nginx /var/log/nginx
Note: I use apache user and group here, because PHP-FPM runs as apache default (apache choosed to be able to access some dir as httpd). If you use some other user on your php-fpm conf then change this accordingly.
Create and configure virtual host directories under /etc/nginx
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled
Add following lines to /etc/nginx/nginx.conf file, after “include /etc/nginx/conf.d/*.conf” line (inside http block).
Load virtual host conf files.
include /etc/nginx/sites-enabled/*.conf;
Create testsite.local virtual host file
Add following content to /etc/nginx/sites-available/testsite.local file. This is very basic virtual host config.
server {
server_name testsite.local;
access_log /srv/www/testsite.local/logs/access.log;
error_log /srv/www/testsite.local/logs/error.log;
root /srv/www/testsite.local/public_html;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Link your virtual host to /etc/nginx/sites-enabled
$ln -sf /etc/nginx/sites-available/testsite.local.conf /etc/nginx/sites-enabled/testsite.local.conf
$systemctl restart nginx.service
## or reload## OR ##
$service nginx restart ## or reload
You have successfully configured PHP and MariaDB to work with NGINX. If you liked the post. please consider following us on LinkedIN, Youtube and subscribe.
Team,
DataHackr