Install NGINX, PHP v7, MYSQL on CENTOS 7 (LEMP).

Here is how to install NGINX on centos 7, does not apply to other OSs and OS versions.

Nginx does not have a repository on centos. So we have to install EPEL with the command:

yum install epel-release -y

Then proceed to install Nginx:

yum install nginx -y

Run the Nginx service:

systemctl start nginx

systemctl enable nginx

For Apache, each webserver will use the .htaccess file for configuration. Particularly Nginx will configure inside the server. By default, Nginx will take the configuration in the conf.d directory. When changing this file, we must restart the Nginx service to apply.

In the nginx.conf file we see the server block, this block is Nginx’s default server configuration, we delete it. Create demo.conf file in conf.d directory and configure as follows:

<CODE>

server {
 listen 80 default_server;
 listen [::]:80 default_server;
 server_name 192.168.1.129;
 root /var/www/html/demo;
  location / {
 }

 error_page 404 /404.html;
  location = /40x.html {
 }

 error_page 500 502 503 504 /50x.html;
  location = /50x.html {
 }
}

</CODE>

This configuration will create a server with ip 192.168.1.129 port 80. The project directory is /var/www/html/demo, this configuration can only read html files. Use the command nginx -t to check if the configuration is ok.

Install php7 using rpm

wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm

rpm -Uvh remi-release-7.rpm

yum install yum-utils -y

yum-config-manager –enable remi-php71

yum –enablerepo=remi,remi-php71 install php-fpm php-common

Install modules for php:

yum –enablerepo=remi,remi-php71 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

Edit the configuration of php-fpm /etc/php-fpm.d/www.conf

user = apache convert user = nginx

group = apache convert group = nginx

listen.owner = nobody convert listen.owner = nginx

listen.group = nobody convert listen.group = nginx

listen = 127.0.0.1:9000 convert listen = /var/run/php-fpm/php-fpm.sock

Run php-fpm service

systemctl start php-fpm.service

systemctl enable php-fpm.service

Go back to editing the file /etc/nginx/conf.d/demo.conf

<CODE>

server {
 listen 80 default_server;
 listen [::]:80 default_server;
 server_name 192.168.1.129;

# note that these lines are originally from the “location /” block
 root /var/www/html/demo;
 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;
 }
}

</CODE>

Use the nginx -t command to check if the configuration is correct. Restart nginx service systemctl restart nginx

Bài viết liên quan