This post I will show you how to install the LEMP (Nginx + PHP-FPM + MySQL) stack on CentOS 7 for start developing with PHP.
Nginx
Step one: update your OS
Run the command bellow to update CentOS:
1 | $ sudo yum update |
Step two: install EPEL repository
To install Nginx, it’s necessary the EPEL repository. The easiest way to add it is installed via yum.
1 | $ sudo install epel-release |
Step three: install Nginx
1 | $ sudo install nginx |
Step four: start Nginx and test it
After installed, let’s start nginx with the following command:
1 | $ sudo systemctl start nginx |
By default, CentOS 7 enable firewall for avoid web server traffic. Let’s allow HTTP and HTTPS:1
2
3$ sudo firewalld-cmd --permanent --zone=public --add-service=http
$ sudo firewalld-cmd --permanent --zone=public --add-service=https
$ sudo firewalld-cmd --reload
For local development environment, if you will prefer disable firewall service to avoid future headaches, run this command:1
$ sudo systemctl disable firewalld
Now it’ possible to test if the server is running. Open the web browser and access:1
2
3
4
5
6
7
8# If you are using the vagrant file of my last post
http://localhost:8888
# For local installation:
http://localhost
# For remote installation:
http://<IP address>
You will se the default CentOS 7 Nginx web page. To enable Nginx for start automatically on boot, enter the following command:1
$ sudo systemctl enable nginx
PHP-FPM
Step one: install REMI repository
Add REMI repositoy to install PHP 7.
1 | $ wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm |
Step two: enable PHP on REMI
You will need to edit a remi repo file to enable PHP. In this case, we will enable PHP 5.6. Feel free to choose another version (7.0 or 7.1 ) editing the respective file.
1 | # Open the file on Vi editor |
Change enable=0 to enable=1 on [remi-php56] block:1
2
3
4
5
6
7
8[remi-php56]
name=Remi's PHP 5.6 RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php56/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/php56/mirror
# NOTICE: common dependencies are in "remi-safe"
enabled=1 # HERE!
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
Save and close the file (:x ENTER)
Step three: install PHP
Now, the easiest step. You will install PHP command line, PHP-FPM (PHP FastCGI Process Manager) and PHP MySQL extension:
1 | $ sudo yum install php php-fpm php-mysql |
Step four: integrante Nginx with PHP-FPM
You will need to add a configuration block of php-fpm to ensure that nginx knows to send PHP requests to PHP-FPM. Let’s do it creating a conf file:1
sudo vi /etc/nginx/default.d/php-fpm.conf
1 | # Add the following content to it: |
Save and close the file (:x ENTER)
This content block tells to Nginx that any .php file should pass to 127.0.0.1 (localhost) on port 9000, wich is where PHP-FPM runs. That is the default configuration.
Step five: start PHP, restart Nginx and test it
Let’ start PHP-FPM, restart Nginx service (it’s necessary) and enable for boot:
1 | $ sudo systemctl start php-fpm |
Now, you will create a phpinfo file to check the PHP information and confirm that the installation was successfull.1
2
3
4# Go to default nginx server folder
$ sudo cd /usr/share/nginx/html
# create a phpinfo.php file with call phpinfo() function
$ sudo echo "<?php phpinfo();" > phpinfo.php
Open the web browser with following url:1
2
3
4
5
6
7
8# If you are using the vagrant file of my last post
http://localhost:8888/phpinfo.php
# For local installation:
http://localhost/phpinfo.php
# For remote installation:
http://<IP address>/phpinfo.php
MySQL
Step one: install MySQL yum repository
You will download the RPM package of MySQL website and install it:1
2$ wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
$ sudo rpm -Uvh mysql57-community-release-el7-9.noarch.rpm
Step two: install MySQL
Then, install MySQL:1
$ sudo yum install mysql-server
Step three: start MySQL
You will start the MySQL Server with the following command:1
$ sudo systemctl start mysqld
You don’t need to enable MySQL on boot, because it was done automatically on installation process. Also during the installation, a temporary password was generated for MySQL root user inside mysql log file. Check it with the following command:1
$ sudo grep 'temporary password' /var/log/mysqld.log
You will need it to configure the MySQL, so make note (copy it ;))
Step four: configure MySQL
You will use a MySQL script included to simplify the configuration. Use this command to run the script:1
$ sudo mysql_secure_installation
This will prompt you for the root password that you copied on last step. Then, it will require to set a new root password. All the subsequent questions in order to remove anonymous users, disallow remote root login, remove the test database and access to it, and reload the privilege tables.
Step five: login MySQL
For testing MySQL and make login on console, following the commands bellow:
1 | $ mysql -u root -p |
Enter with the root password that you define and press ENTER. Now you are logged in. Check the database’s list with the command:
1 | show databases; |
For logout, run the command:1
quit;
Now you have all you need to start developing PHP with MySQL database. On the next posts, I will show you other ways to configure Nginx (allow many sites) and useful PHP extensions. Also, start with a PHP framework that help us fast development with design patterns, clean code and great performance.