HOW TO INSTALL PHP 7, NGINX, MYSQL ON CENTOS/RHEL 7
Introduction
A LEMP software stack is a group of xuất hiện source software that is typically installed together lớn enable a vps to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating system, with the ENginx web hệ thống (which replaces the Apache component of a LAMP stack). The site data is stored in a MySQL-based database, & dynamic content is processed by PHP.Bạn đang xem: How to install php 7, nginx, mysql on centos/rhel 7
In this guide, we’ll get a LEMP stack with PHP 7.4 installed on a CentOS 7 server, using MariaDB as the database management system. MariaDB works as a drop-in replacement for the original MySQL server, which in practice means you can switch khổng lồ MariaDB without having to make any configuration or code changes in your application.
Prerequisites
Before you begin with this guide, you should have a separate, non-root user trương mục set up on your server. You can learn how to vì chưng this by completing steps 1-4 in the initial server cài đặt for CentOS 7.
Step 1 — Installing Nginx
In order to display website pages khổng lồ our site visitors, we are going to lớn employ Nginx, a high-performance website server. Khổng lồ get the latest Nginx version, we’ll first install the EPEL repository, which contains additional software for the CentOS 7 operating system.
To địa chỉ the CentOS 7 EPEL repository, run the following command:
sudo yum install epel-releaseSince we are using a sudo command, these operations get executed with root privileges. It will ask you for your regular user’s password lớn verify that you have permission to run commands with root privileges. You’ll also be prompted to confirm installation, so press Y to lớn proceed.
Now that the EPEL repository is installed on your server, install Nginx using the following yum command:
sudo yum install nginxOnce the installation is finished, start the Nginx service with:
sudo systemctl start nginxYou can do a spot kiểm tra right away to lớn verify that everything went as planned by visiting your server’s public IP address in your website browser (see the cảnh báo under the next heading lớn find out what your public IP address is if you vị not have this information already):
http://server_domain_name_or_IP/You will see the default CentOS 7 Nginx website page, which is there for informational & testing purposes. It should look something like this:

If you see this page, then your web server is now correctly installed.
To enable Nginx to lớn start on boot, run the following command:
sudo systemctl enable nginx
How to Find Your Server’s Public IP Address
If you vày not know what your server’s public IP address is, there are a number of ways you can find it. Usually, this is the address you use to connect to lớn your server through SSH.From the command line, you can find this a few ways. First, you can use the iproute2 tools to lớn get your address by typing this:
ip addr show eth0 | grep inet | awk " print $2; " | sed "s//.*$//"This will give you one or two lines back. They are both correct addresses, but your computer may only be able lớn use one of them, so feel không tính tiền to try each one.
An alternative method is to use an outside các buổi party to tell you how it sees your server. You can vì this by asking a specific server what your IP address is:
curl http://icanhazip.comRegardless of the method you use khổng lồ get your IP address, you can type it into your website browser’s address bar to lớn get to lớn your server.
Step 2 — Installing MariaDB
Now that we have our web hệ thống up và running, it is time lớn install MariaDB, a MySQL drop-in replacement. MariaDB is a community-developed fork of the MySQL relational database management system.
Again, we can use yum khổng lồ acquire & install our software. This time, we’ll also install some other helper packages that will assist us in getting our components to lớn communicate with each other:
sudo yum install mariadb-server mariadbWhen the installation is complete, we need to start MariaDB with the following command:
sudo systemctl start mariadbNow that our MariaDB database is running, we want to lớn run a security script that will remove some dangerous defaults & lock down access to lớn our database. Start the interactive script by running:
sudo mysql_secure_installationThe prompt will ask you for your current root MariaDB password. Since you just installed MariaDB, you most likely won’t have one, so leave it blank by pressing enter. Then the prompt will ask you if you want lớn set a root password. Go ahead and enter Y, và follow the instructions:
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 MariaDBroot user without the proper authorisation.Set root password?
The last thing you will want to bởi is enable MariaDB to lớn start on boot. Use the following command to vì so:
sudo systemctl enable mariadbAt this point, your database system is now mix up & we can move on.
Step 3 — Installing PHP
PHP is the component of our thiết lập that will process code to lớn display dynamic content. It can run scripts, connect lớn our MySQL databases to get information, và hand the processed nội dung over lớn our web hệ thống to display.
The PHP version available by mặc định within CentOS 7 servers is outdated, & for that reason, we’ll need to lớn install a third-party package repository in order khổng lồ obtain PHP 7+ và get it installed on your CentOS 7 server. Remi is a popular package repository providing the most up-to-date PHP releases for CentOS servers.
To install the Remi repository for CentOS 7, run:
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpmAfter the installation is done, you’ll need khổng lồ run a command to lớn enable the repository containing your preferred version of PHP. To kiểm tra which PHP 7+ releases are available in the Remi repository, run:
yum --disablerepo="*" --enablerepo="remi-safe" menu php<7-9><0-9>.x86_64You’ll see output lượt thích this:
Loaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * remi-safe: mirrors.ukfast.co.ukAvailable Packagesphp70.x86_64 2.0-1.el7.remi remi-safephp71.x86_64 2.0-1.el7.remi remi-safephp72.x86_64 2.0-1.el7.remi remi-safephp73.x86_64 2.0-1.el7.remi remi-safephp74.x86_64 1.0-3.el7.remi remi-safephp80.x86_64 1.0-3.el7.remi remi-safeIn this guide, we’ll install PHP 7.4, which is currently the most updated stable version of PHP. Lớn enable the correct Remi package khổng lồ get PHP 7.4 installed, run:
sudo yum-config-manager --enable remi-php74Now we can proceed lớn use yum for installing PHP as usual. The following command will install all the required packages to lớn get PHP 7.4 set up within Nginx & allow it to connect to MySQL-based databases:
sudo yum install php php-mysqlnd php-fpmTo confirm that PHP is available as your chosen version, run:
php --versionYou’ll see output lượt thích this:
sudo yum install nanoOpen the /etc/php-fpm.d/www.conf configuration file using nano or your editor of choice:
sudo nano /etc/php-fpm.d/www.confNow look for the user và group directives. If you are using nano, you can hit CTRL+W to tìm kiếm for these terms inside the mở cửa file.
Xem thêm: Misa Không Kết Nối Được Với Máy Chủ, Tại Sao Phần Mềm Misa Sme Chỉ Dùng Được Trong
…; Unix user/group of processes; Note: The user is mandatory. If the group is not set, the default user"s group; will be used.; RPM: apache user chosen to provide access to the same directories as httpduser = apache; RPM: Keep a group allowed khổng lồ write in log dir.group = apache…You’ll notice that both the user and group variables are set to apache. We need lớn change these to lớn nginx:
…; RPM: apache user chosen lớn provide access khổng lồ the same directories as httpduser = nginx; RPM: Keep a group allowed khổng lồ write in log dir.group = nginx…Next, locate the listen directive. By default, php-fpm will listen on a specific host and port over TCP. We want to change this setting so it listens on a local socket file, since this improves the overall performance of the server.Change the line containing the listen directive khổng lồ the following:
listen = /var/run/php-fpm/php-fpm.sock;Finally, we’ll need khổng lồ change the owner & group settings for the socket file we just defined within the listen directive. Locate the listen.owner, listen.group và listen.mode directives. These lines are commented out by default. Uncomment them by removing the preceding ; sign at the beginning of the line. Then, change the owner & group lớn nginx:
listen.owner = nginxlisten.group = nginxlisten.mode = 0660Save & close the file when you’re done editing. If you are using nano, vì chưng so by pressing CTRL + X, then Y & ENTER.
To enable và start the php-fpm service, run:
sudo systemctl start php-fpmYour PHP environment is now ready. Next, we’ll configure Nginx so that it sends all requests for PHP scripts to be processed by php-fpm.
Step 4 — Configuring Nginx to Process PHP Pages
Now, we have all of the required components installed. The only configuration change we still need to vị is tell Nginx to lớn use our PHP processor for dynamic content.
Nginx has a dedicated directory where we can define each hosted website as a separate configuration file, using a vps block. This is similar to Apache’s virtual hosts.
With the default installation, however, this directory is empty. We’ll create a new file to serve as the mặc định PHP website on this server, which will override the default server block defined in the /etc/nginx/nginx.conf file.
First, open a new tệp tin in the /etc/nginx/conf.d directory:
sudo nano /etc/nginx/conf.d/default.confCopy the following PHP hệ thống definition block khổng lồ your configuration file, & don’t forget to lớn replace the server_name directive so that it points to lớn your server’s domain name name or IP address:
server listen 80; server_name server_domain_or_IP; root /usr/share/nginx/html; index index.php index.html index.htm; location / try_files $uri $uri/ =404; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html root /usr/share/nginx/html; location ~ .php$ try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; Save và close the file when you’re done.
Next, restart Nginx to lớn apply the changes:
sudo systemctl restart nginxYour web server is now fully set up. In the next step, we’ll thử nghiệm the PHP integration lớn Nginx.
Step 5 — Testing PHP Processing on your web Server
Now that your web hệ thống is set up, we can create a thử nghiệm PHP script khổng lồ make sure Nginx is correctly handling .php scripts with the help of php-fpm.
Before creating our script, we’ll make a change to the mặc định ownership settings on Nginx’s document root, so that our regular sudo user is able lớn create files in that location.
The following command will change the ownership of the default Nginx document root lớn a user & group called sammy, so be sure to replace the highlighted username & group in this command khổng lồ reflect your system’s username and group.
sudo chown -R sammy.sammy /usr/share/nginx/html/We’ll now create a kiểm tra PHP page khổng lồ make sure the web vps works as expected.
Create a new PHP file called info.php at the /usr/share/nginx/html directory:
nano /usr/share/nginx/html/info.phpThe following PHP code will display information about the current PHP environment running on the server:
phpinfo();When you are finished, save và close the file.
Now we can kiểm tra whether our web vps can correctly display content generated by a PHP script. Go lớn your browser và access your vps hostname or IP address, followed by /info.php:
http://server_host_or_IP/info.phpYou’ll see a page similar to this:

After checking the relevant information about your PHP hệ thống through that page, it’s best lớn remove the file you created as it contains sensitive information about your PHP environment và your CentOS server. You can use rm to remove that file:
rm /usr/share/nginx/html/info.phpYou can always regenerate this file if you need it later.
Conclusion
In this guide, you’ve built a flexible foundation for serving PHP websites and applications khổng lồ your visitors, using Nginx as web server và the latest PHP release version. You’ve phối up Nginx lớn handle PHP requests through php-fpm, và you also phối up a MariaDB database lớn store your website’s data.
Xem thêm: Cách Tăng Doanh Số Bán Hàng Hiệu Quả Nhất Năm 2021, 8 Cách Tăng Doanh Số Bán Hàng Hiệu Quả Nhất 2020
If you’ve enjoyed this tutorial and our broader community, consider checking out our hostingvietnam.vn products which can also help you achieve your development goals.