Install Web Server (NGINX), Programming Language (PHP), Database (MariaDB) to provide web server services.
NGINX + PHP + MariaDB (MySQL) is a software group typically used to serve dynamic websites and web applications.
The install version introduces both CentOS’s default Repo and an external Repo where you can install the latest version. CentOS’s default repo is more secure because it is released after testing the safety of the version, but it takes some time to reflect the latest version. The external repo features the latest version quickly. Which version you use depends on your discretion, especially depending on the final application you want to serve your web. In addition, the program should use only one version, unless for special reasons.
In general, we recommend using the latest version with feature additions, bug patches, and security patches.
MariaDB
MariaDB is included in the CentOS default Repo, but due to its low version, we recommend the latest Repo-based installation from MariaDB developers.
1 MariaDB Installation
Register Yum Repo with MariaDB Developer’s latest Repo.
1 |
nano /etc/yum.repos.d/mariadb.repo |
and paste
1 2 3 4 5 |
[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.3/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 |
Now install MariaDB by issuing following command,
1 |
tum install mariadb-server |
2. Install PHP
PHP is included in the CentOS default Repo, but I recommend a Remi Repo based installation because of its low version.
2 Remi Repo based installation
Add a Remi Repo.
1 |
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm |
Install yum-utils to set the version.
1 |
yum install yum-utils |
Set the version to use as PHP 7.1.
1 |
yum-config-manager --enable remi-php71 |
PHP has a core and extension module structure. It installs by adding Core and necessary extension modules.
1 |
yum install php php-bcmath php-common php-dba php-fpm php-gd php-mbstring php-mysql php-pear php-xml |
If you need additional modules, you can install them after checking.
1 |
yum search php |
3. NGINX Installation
Since NGINX is not included in the CentOS default repo, you must register the latest repo of NGINX developer or use the repo provided by Extra Packages for Enterprise Linux (EPEL). NGINX Repo based installation is recommended.
Register Yum Repo with the latest Repo from NGINX Developer.
1 |
nano /etc/yum.repos.d/nginx.repo |
and add these lines and save the file
1 2 3 4 5 |
[nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 |
Once you have added the Repo, install NGINX by issuing following command,
1 |
yum install nginx |
4. Web Service User Settings
Do not use root because the webserver is a remote service. Although there are several settings depending on security, this document adds a USER that can use web services, and adds a webserver service, nginx, to a group of USERs.
Add a USER to use the web service.
1 |
useradd USER |
Set the password for the created USER.
1 |
passwd USER |
Set the part for other in the Web service user’s default file creation permissions to restrict access to unauthorized users.
1 |
echo "umask 0027" >> /home/USER/.bash_profile |
Add nginx to the group of USER to use the web service.
1 |
usermod -a -G USER nginx |
Set the permissions of the created USER directory.
1 |
chmod 750 /home/USER/ |
5. Package Settings
5.1 MariaDB setup
The MariaDB configuration file is located in /etc/my.cnf and includes /etc/my.cnf.d/, so the user configuration file is created in /etc/my.cnf.d/mysql.cnf.
The setting allows both MariaDB server / clients to work with UTF-8 by default.
1 |
nano /etc/my.cnf.d/mysql.cnf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[mysqld] init_connect = SET collation_connection = utf8_general_ci init_connect = SET NAMES utf8 character-set-server = utf8 collation-server = utf8_general_ci character-set-client-handshake = FALSE [mysqldump] default-character-set = utf8 [mysql] default-character-set = utf8 [client] default-character-set = utf8 |
To add a query cache, add the following:
1 2 3 4 |
nano /etc/my.cnf.d/query-cache.cnf [mysqld] query_cache_type = 1 query_cache_size = 16M |
Set 16M appropriately according to user memory. Too large a value would rather degrade the performance of the cache.
5.2 PHP Configuration
The PHP configuration file is located in /etc/php.ini and includes /etc/php.d/, so the user configuration file is written in /etc/php.d/php.ini.
1 |
nano /etc/php.d/php.ini |
Adjust timezone according to your needs
1 2 |
[PHP] date.timezone = Asia/Karachi |
In addition, for security, you can add:
1 2 3 4 5 |
cgi.fix_pathinfo=0 open_basedir = /home display_errors = Off allow_url_fopen = Off expose_php = Off |
Set PHP’s Runtime user: group.
Find the user and group parts of the www.conf file and make the following changes.
1 |
nano /etc/php-fpm.d/www.conf |
1 2 |
user = nginx group = nginx |
5.3 NGINX Settings
Set up NGINX’s Runtime user: group.
Find the user part of the nginx.conf file and make the following changes.
1 2 3 |
nano /etc/nginx/nginx.conf user nginx nginx; |
Write default settings to work with PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
nano /etc/nginx/conf.d/USER.conf server { listen 80; server_name SERVER-NAME; root /home/USER/web_site; index index.html index.htm index.php; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; try_files $uri = 404; } } |
6. Enable the Newly Installed Programs
Register the programs using systemctl so that they can be executed automatically when the server reboots.
1 2 3 |
systemctl enable mariadb systemctl enable php-fpm systemctl enable nginx |
Start the installed programs.
1 2 3 |
systemctl start mariadb systemctl start php-fpm systemctl start nginx |
7. MariaDB initial setup
The first time you install MariaDB, you can proceed with the basic setup as follows:
1 |
mysql_secure_installation |
And respond to queries by pressing Enter
8.Register MariaDB Users
Create database and user for your application by issuing following commands
1 |
mysql -u root -p |
1 2 3 4 5 |
CREATE DATABASE your_database_name; CREATE USER your_user_name@localhost IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON your_database_name.* TO your_user_name@localhost IDENTIFIED BY 'your_password'; FLUSH PRIVILEGES; quit; |
9. Testing
Log in as the web service user or change the user to USER.
Create a web service folder.
1 |
mkdir /home/USER/web_site/ |
Create a php info file to test the working of php,
1 |
nano /home/USER/web_site/phpinfo.php |
paste following lines
Connect to the web browser address with the name set in server_name in NGINX settings and confirm.
If you see the following screen, the installation is successful.