Installazione ambiente LEMP (Linux Nginx MariaDB PHP) su CentOS 7
In questo tutorial andremo a vedere come installare passo dopo passo l’ ambiente LEMP (Linux Nginx MariaDB PHP) su server CentOS 7.
Prendiamo in considerazione un server CentOS 7 preinstallato ( per l’installazione vedi questo articolo) ed iniziamo installando il repository epel:
sudo yum install epel-release
sudo yum update
sudo yum install vim
adesso installiamo il web server Nginx
sudo yum install nginx
avviamo il demone
sudo systemctl start nginx
per verificare lo stato
sudo systemctl status nginx
e abilitiamo l’avvio automatico
sudo systemctl enable nginx
abilitiamo la porta 80 (HTTP) sul firewall
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --reload
per semplicità disabilitiamo il selinux (consideriamo l’installazione per ambienti di laboratorio)
vim /etc/selinux/config
cambiare la variabile SELINUX in disabled
setenforce 0
Verifichiamo che il web server sia attivo visitando il seguente url http://ip_server/, dovremmo ottenere questa schermata:
Adesso installiamo il database MARIADB
sudo yum install mariadb-server mariadb
avviamo e controlliamo che il servizio sia attivo
sudo systemctl start mariadb
sudo systemctl status mariadb
ed infine abilitiamolo all’ avvio automatico
sudo systemctl enable mariadb
Rendiamo Mariadb sicuro, avviando il seguente comando
sudo mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we’ll need the current
password for the root user. If you’ve just installed MariaDB, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.Enter current password for root (enter for none):Premere Invio
il primo passo è fornire una password a root (inizialmente era vuota)
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.Set root password? [Y/n]Y
New password:
Re-enter new password:
rimuoviamo utenti anonimi
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.Remove anonymous users? [Y/n]Y
disabilitiamo l’utenza root al collegamento remoto
Normally, root should only be allowed to connect from ‘localhost’. This
ensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n]Y
rimuoviamo il database di test
By default, MariaDB comes with a database named ‘test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.Remove test database and access to it? [Y/n]Y
ricarichiamo i privilegi sulle tabelle
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.Reload privilege tables now? [Y/n]Y
Abbiamo terminato con l’installazione del database.
All done! If you’ve completed all of the above steps, your MariaDB
installation should now be secure.Thanks for using MariaDB!
Adesso installiamo PHP
yum install php php-mysql php-fpm php-cli
sul file di configurazione di php.ini, impostiamo cgi.fix_pathinfo a 0.
Configuriamo php-fpm (FastCGI Process Manager)
sudo vi /etc/php-fpm.d/www.conf
ed impostiamo i seguenti parametri per lavorare con il web server Nginx, attiviamo le seguenti linee eliminando il ;
listen.owner = nobody
listen.group = nobody
ed inoltre
; 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 Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
salviamo e avviamo php-fpm ed abilitiamo all’avvio
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
A questo punto testiamo lo stack, creando una pagina statica in php
vi /usr/share/nginx/html/info.php
con il seguente codice
<?php phpinfo(); ?>
Impostiamo il web server per considerare pagine php, creiamo un Server Block ( è simile ai Virtual Host in Apache) di default per l’HTTP
vim /etc/nginx/conf.d/default.conf
ed aggiungiamo il seguente Server Block
server {
listen 80;
server_name ip_server_o_server_name;# 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;
}
}
systemctl restart php-fpm
systemctl restart nginx
e facciamo il test sul browser all’ inidirizzo http://ip_server/info.php, dovremo ottenere questo risultato
installazione dello stack LEMP su Centos 7 finita.