Contexte :

Nous partirons d'un système de base sous CentOS 6 installé.
Nous allons installer une stack Linux (E)nginx (nginx est prononcé engine X) Mysql Php.

Installation :

MySQL :

  1 $ sudo yum -y install mysql-server
  2 
  3 mysql_install_db
  4 WARNING: The host 'templatex64.alasta.com' could not be looked up with resolveip.
  5 This probably means that your libc libraries are not 100 % compatible
  6 with this binary MySQL version. The MySQL daemon, mysqld, should work
  7 normally with the exception that host name resolving will not work.
  8 This means that you should use IP addresses instead of hostnames
  9 when specifying MySQL privileges !
 10 Installing MySQL system tables...
 11 OK
 12 Filling help tables...
 13 OK
 14 
 15 To start mysqld at boot time you have to copy
 16 support-files/mysql.server to the right place for your system
 17 
 18 PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
 19 To do so, start the server, then issue the following commands:
 20 
 21 /usr/bin/mysqladmin -u root password 'new-password'
 22 /usr/bin/mysqladmin -u root -h templatex64.alasta.com password 'new-password'
 23 
 24 Alternatively you can run:
 25 /usr/bin/mysql_secure_installation
 26 
 27 which will also give you the option of removing the test
 28 databases and anonymous user created by default.  This is
 29 strongly recommended for production servers.
 30 
 31 See the manual for more instructions.
 32 
 33 You can start the MySQL daemon with:
 34 cd /usr ; /usr/bin/mysqld_safe &
 35 
 36 You can test the MySQL daemon with mysql-test-run.pl
 37 cd /usr/mysql-test ; perl mysql-test-run.pl
 38 
 39 Please report any problems with the /usr/bin/mysqlbug script!
 40 
 41 20:55 root@templatex64 ~ # service mysqld start
 42 Démarrage de mysqld :                                      [  OK  ]
 43 
 44 20:55 root@templatex64 ~ # /usr/bin/mysql_secure_installation
 45 
 46 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
 47       SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 48 
 49 
 50 In order to log into MySQL to secure it, we'll need the current
 51 password for the root user.  If you've just installed MySQL, and
 52 you haven't set the root password yet, the password will be blank,
 53 so you should just press enter here.
 54 
 55 Enter current password for root (enter for none): 
 56 OK, successfully used password, moving on...
 57 
 58 Setting the root password ensures that nobody can log into the MySQL
 59 root user without the proper authorisation.
 60 
 61 Set root password? [Y/n] y
 62 New password: 
 63 Re-enter new password: 
 64 Password updated successfully!
 65 Reloading privilege tables..
 66  ... Success!
 67 
 68 
 69 By default, a MySQL installation has an anonymous user, allowing anyone
 70 to log into MySQL without having to have a user account created for
 71 them.  This is intended only for testing, and to make the installation
 72 go a bit smoother.  You should remove them before moving into a
 73 production environment.
 74 
 75 Remove anonymous users? [Y/n] y
 76  ... Success!
 77 
 78 Normally, root should only be allowed to connect from 'localhost'.  This
 79 ensures that someone cannot guess at the root password from the network.
 80 
 81 Disallow root login remotely? [Y/n] y
 82  ... Success!
 83 
 84 By default, MySQL comes with a database named 'test' that anyone can
 85 access.  This is also intended only for testing, and should be removed
 86 before moving into a production environment.
 87 
 88 Remove test database and access to it? [Y/n] y
 89  - Dropping test database...
 90  ... Success!
 91  - Removing privileges on test database...
 92  ... Success!
 93 
 94 Reloading the privilege tables will ensure that all changes made so far
 95 will take effect immediately.
 96 
 97 Reload privilege tables now? [Y/n] y
 98  ... Success!
 99 
100 Cleaning up...
101 
102 
103 
104 All done!  If you've completed all of the above steps, your MySQL
105 installation should now be secure.
106 
107 Thanks for using MySQL!
108 
109 $ sudo chkconfig --level 2345 mysqld on

nginx

Ajouter le fichier de repository nginx /etc/yum.repos.d/nginx.repo

1 [nginx]
2  name=nginx repo
3  baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
4  gpgcheck=0
5  enabled=1

1 $ sudo yum -y install nginx
2 $ sudo service nginx start
3 $ sudo chkconfig --level 2345 nginx on

php

1 $ sudo yum -y install php-common php-fpm php-mysql
2 $ sudo service php-fpm start

Configuration :

nginx

Edition du fichier de d'origine /etc/nginx/conf.d/default.conf

 1 server {
 2     listen       80;
 3     server_name  localhost;
 4 
 5     location / {
 6         root   /usr/share/nginx/html;
 7         index  index.php index.html index.htm;
 8     }
 9 
10     error_page   500 502 503 504  /50x.html;
11     location = /50x.html {
12         root   /usr/share/nginx/html;
13     }
14 
15     location ~ \.php$ {
16         fastcgi_pass   127.0.0.1:9000;
17         fastcgi_index  index.php;
18         #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
19         fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
20         include        fastcgi_params;
21     }
22 }

Note : la directive fastcgi_param par défaut ne fonctionne pas (no $document_root qui indique /etc/nginx/html/ ...), j'ai du forcer le path de root !

Redémarrer nginx

1 $ sudo service nginx restart

page de test

1 $ sudo echo '<?php phpinfo(); ?>'

Test :

1 $ curl http://www.alasta.lab
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
3 <html><head>
4 <style type="text/css">
5 body {background-color: #ffffff; color: #000000;}
6 body, td, th, h1, h2 {font-family: sans-serif;}
7 pre {margin: 0px; font-family: monospace;}
8 a:link {color: #000099; text-decoration: none; back
9 --SNiP--

Bonus :

Masquer la version nginx

Avant

1 $ $ curl -I www.alasta.lab
2 HTTP/1.1 200 OK
3 Server: nginx/1.8.0
4 Date: Sun, 19 Jul 2015 20:02:28 GMT
5 Content-Type: text/html
6 Connection: keep-alive
7 X-Powered-By: PHP/5.3.3

Modification

Ajouter dans /etc/nginx/nginx.conf la ligne suivante dans la section http :

1 server_tokens off;

Redémarrage de nginx

1 $ sudo service nginx restart

Après

1 $ curl -I www.alasta.lab
2 HTTP/1.1 200 OK
3 Server: nginx
4 Date: Sun, 19 Jul 2015 20:02:28 GMT
5 Content-Type: text/html
6 Connection: keep-alive
7 X-Powered-By: PHP/5.3.3

Masquer la version de php

Modification

Dans le curl précédent on voit bien que la version de php est affichée, pour pallier à cela il suit de passer la directive expose_php à off dans /etc/php.ini

1 ; Decides whether PHP may expose the fact that it is installed on the server
2 ; (e.g. by adding its signature to the Web server header).  It is no security
3 ; threat in any way, but it makes it possible to determine whether you use PHP
4 ; on your server or not.
5 ; http://www.php.net/manual/en/ini.core.php#ini.expose-php
6 ;expose_php = On
7 expose_php = Off

1 $ sudo service php-fpm restart

Après

1 $ curl -I www.alasta.lab
2 HTTP/1.1 200 OK
3 Server: nginx
4 Date: Sun, 19 Jul 2015 20:07:39 GMT
5 Content-Type: text/html
6 Connection: keep-alive