Install LEMP (nginx) from Centos 7

A. Install NGINX of centos 7


1. Default centos nginx repo is not available, we need to install EPEL repository


# yum install epel-release -y

2. Install Nginx


# yum install nginx -y

3. Enable Nginx


# systemctl start nginx
# systemctl enable nginx

4. Check whether Nginx is running or not by accessing the server's Public IP. If it cannot be accessed, try setting a firewall:


# sudo firewall-cmd --permanent --zone=public --add-service=http
# sudo firewall-cmd --permanent --zone=public --add-service=https
# sudo firewall-cmd --reload


B. Install MySQL (mariadb)


1. mariadb is included in the centos default repository. just install mariadb


# yum install mariadb-server mariadb -y

2. Enable mariadb


# systemctl start mariadb
# systemctl enable mariadb

3. To install mariadb with secure settings.


# mysql_secure_installation

Change the root password? [Y/n] n
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

Additional, If you want to create a new user, you can use step :


# mysql
> CREATE USER 'usernamebaru'@'localhost' IDENTIFIED BY 'passwordbaru';
GRANT ALL PRIVILEGES ON * . * TO 'usernamebaru'@'localhost';


C. Install PHP v7.4


1. We need to download and install the additional CentOS repositories that contain the necessary packages for PHP v74


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

2. Enable php74 repository, which defaults to disable


# yum install yum-utils -y
# yum-config-manager --enable remi-php74

3. Install package php


# yum --enablerepo=remi,remi-php74 install php-fpm php-common

4. Install php module, to make sure the service runs well


# yum --enablerepo=remi,remi-php74 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

D. Configure Nginx to work with php


1. Create the nginx configuration file


# nano /etc/nginx/conf.d/default.conf

add the following script, make sure the your_server_ip section is replaced with the server IP:
server {
listen 80;
server_name your_server_ip;

# note that these lines are originally from the "location /" block
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;
}
}

2. Save, then restart the nginx service


# systemctl restart nginx

3. Edit PHP-FPM file


# nano /etc/php-fpm.d/www.conf

find and replace the following parts;


user = apache to user = nginx
group = nginx becomes a group = nginx
;listen.owner = nobody to listen.owner = nginx
;listen.group = nginx tolisten.group = nginx
listen = 127.0.0.1:9000 to ;listen = 127.0.0.1:9000, then underneath add
listen = /var/run/php-fpm/php-fpm.sock

4. Save, then activate php-fpm


# systemctl start php-fpm.service
# systemctl enable php-fpm.service


E. Install phpmyadmin


1. The last step is to install phpmyadmin which functions to configure the database, both export and import on the server.


# yum -y install phpMyAdmin

2. Link to access http://xxx.xxx.xxx.xxx/phpMyAdmin/, login using root mariadb


# ln -s /usr/share/phpMyAdmin /usr/share/nginx/html
# chown -R nginx:nginx /var/lib/php/session
# systemctl restart nginx

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.