Citation

J’ai toujours rêvé d'un ordinateur qui soit aussi facile à utiliser qu'un téléphone. Mon rêve s'est réalisé : je ne sais plus comment utiliser mon téléphone.

Bjarne Stroustrup

jeudi 30 octobre 2014

Serveur LAMP - suphp - phpMyAdmin sous CentOS 6.5

J'ai eu à faire un serveur LAMP sous Linux CentOS 6.5. L'idée est d'avoir un serveur multi-sites webs, Chaque site est la propriété d'un utilisateur et s'exécute avec le compte du propriétaire grâce à suphp. On a donc un cloisonnement des sites webs.
Je n'ai rien inventé (je ne suis pas assez bon sous Linux pour ça) et je me suis inspiré de cet article : http://www.opensource-tn.org/viewtopic.php?f=9&t=118 et de quelques autres.
Après pas mal de recherche, voila la procédure que j'ai utilisé:

Installations de bases

on commence par mettre l'OS à jour:
# yum update -y
 Installation de Apache, MySQL:
# yum install httpd mysql-server -y
# chkconfig mysqld on
# chkconfig httpd on
 Configuration du firewall Linux pour laisser passer les port 80 et 443:
# iptables -I INPUT 3 -p tcp -i eth0 --dport 80 -j ACCEPT
# iptables -I INPUT 3 -p tcp -i eth0 --dport 443 -j ACCEPT
# iptables-save>/etc/sysconfig/iptables
# service iptables restart
Installation des repositoris  EPEL et REMI (pour php 5.4):
# cd /tmp
# wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
# wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
# cd
On édite /etc/yum.repos.d/remi.repo pour mettre la ligne enabled=1 dans la section [remi]

Installation de PHP:
# yum --enablerepo=remi install php php-mysql php-curl php-mcrypt php-gd php-cli php-intl php-posix php-mbstring php-domxml -y
# yum --enablerepo=remi install php-bcmath php-channel-phpseclib php-pear php-php-gettext php-phpseclib-crypt-aes php-phpseclib-crypt-base php-phpseclib-crypt-rijndael php-recode php-tcpdf php-tcpdf-dejavu-sans-fonts php-tidy -y
On neutralise SELINUX:
# echo 0 >/selinux/enforce
# replace "SELINUX=enforcing" "SELINUX=disabled" -- /etc/selinux/config

Configurations de base

Configuration d'Apache, dans /etc/httpd/conf/httpd.conf on met :
ServerName <nom du serveur>
ServerSignature Off
ServerTokens Prod
Configuration de base des VHosts
Créer un fichier /etc/httpd/conf.d/03-EnableVirtualHost.conf avec dedans :
NameVirtualHost *:80
Include conf.d/hosts/*.conf
Créer le répertoire :
# mkdir /etc/httpd/conf.d/hosts
On démarre les services:
# service httpd start
# service mysqld start
Sécurisation de MySQL :
# /usr/bin/mysql_secure_installation
Tapez <entrée>, y , <mot de passe root mysql 2 fois>, y, y, y, y

Compilation et installation de suphp

Je n'ai pas réussi à compiler la version 0.7.2 de suphp ...donc j'utilise la version 0.7.1.
Installation du nécessaire à la compilation de suPhp
# yum install gcc gcc-c++ automake autoconf make httpd-devel apr apr-devel -y
Recupération et compilation de suPhp
# cd /usr/local/src
# wget http://www.suphp.org/download/suphp-0.7.1.tar.gz
# tar zxvf suphp-*.tar.gz && rm -f suphp-*.tar.gz && cd suphp-*
# ./configure --prefix=/usr/local/suphp-0.7.1 --sysconfdir=/etc --with-apxs=/usr/sbin/apxs --with-apr=/usr/bin/apr-1-config --with-logfile=/var/log/suphp.log --with-setid-mode=paranoid --with-min-uid=500 --with-min-gid=500 --with-apache-user=apache
# make
# make install
Lien symbolique vers suPhp pour pouvoir changer de version sans changer la config web
# cd /usr/local
# ln -s suphp-0.7.1 suphp
# ls -l

Configuration de suphp

Modifier/Créer le fichier /etc/suphp.conf:

[global]
;Path to logfile
logfile=/var/log/suphp.log

;Loglevel
loglevel=info

;User Apache is running as
webserver_user=apache

;Path all scripts have to be in
docroot=/var/www:${HOME}/www:/usr/share/phpMyAdmin

;Path to chroot() to before executing script
;chroot=/mychroot

; Security options
allow_file_group_writeable=true
allow_file_others_writeable=false
allow_directory_group_writeable=false
allow_directory_others_writeable=false

;Check wheter script is within DOCUMENT_ROOT
check_vhost_docroot=false

;Send minor error messages to browser
errors_to_browser=true

;PATH environment variable
env_path=/bin:/usr/bin

;Umask to set, specify in octal notation
umask=0027

; Minimum UID
min_uid=500

; Minimum GID
min_gid=500

[handlers]
;Handler for php-scripts
;x-httpd-php="php:/usr/bin/php"
x-httpd-php="php:/usr/bin/php-cgi"

;Handler for CGI-scripts
x-suphp-cgi="execute:!self"

Modification de la configuration Apache et php
# mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.disabled
# echo "LoadModule suphp_module modules/mod_suphp.so">/etc/httpd/conf.d/00-suphp.conf
# replace ";cgi.fix_pathinfo=1" "cgi.fix_pathinfo=1"  -- /etc/php.ini
# service httpd restart

Installation de phpMyAdmin

# yum install phpmyadmin -y

Création du user pmauser qui exécute phpMyAdmin
# adduser -U pmauser
# chown -R pmauser:pmauser /usr/share/phpMyAdmin/.
# chmod -R u+s /usr/share/phpMyAdmin/.
Configuration de phpMyAdmin
# mv /etc/httpd/conf.d/phpMyAdmin.conf /etc/httpd/conf.d/phpMyAdmin.conf.original
On remplace le fichier de configuration d'origine par un autre. Le fichier /etc/httpd/conf.d/10-phpMyAdmin.conf contient :

# phpMyAdmin - Web based MySQL browser written in php
#
# Allows only localhost by default
#
# But allowing phpMyAdmin to anyone other than localhost should be considered
# dangerous unless properly secured by SSL

Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8

   <IfModule mod_authz_core.c>
     # Apache 2.4
     Require local
   </IfModule>
   <IfModule !mod_authz_core.c>
     # Apache 2.2
     Order Deny,Allow
     Deny from All
     Allow from 127.0.0.1
     Allow from ::1
     # MODIFIER l'IP DES MACHINES AYANT DROIT D'UTILSER PHPMYADMIN
     # peut être de la forme 100.10.3.124/255.255.255.0
     Allow from 181.166.244.101
   </IfModule>
   # Configuration de suPHP
   suPHP_Engine on
   suPHP_UserGroup pmauser pmauser
   suPHP_ConfigPath /etc/
   suPHP_AddHandler x-httpd-php
   AddHandler x-httpd-php .php .php3 .php4 .php5
   # Configuration de PHP
   # php_admin_flag engine on
</Directory>
<Directory /usr/share/phpMyAdmin/setup/>
   <IfModule mod_authz_core.c>
     # Apache 2.4
     Require local
   </IfModule>
   <IfModule !mod_authz_core.c>
     # Apache 2.2
     Order Deny,Allow
     Deny from All
     Allow from 127.0.0.1
     Allow from ::1
   </IfModule>
</Directory>
# These directories do not require access over HTTP - taken from the original
# phpMyAdmin upstream tarball
#
<Directory /usr/share/phpMyAdmin/libraries/>
    Order Deny,Allow
    Deny from All
    Allow from None
</Directory>
<Directory /usr/share/phpMyAdmin/setup/lib/>
    Order Deny,Allow
    Deny from All
    Allow from None
</Directory>
<Directory /usr/share/phpMyAdmin/setup/frames/>
    Order Deny,Allow
    Deny from All
    Allow from None
</Directory>
# This configuration prevents mod_security at phpMyAdmin directories from
# filtering SQL etc.  This may break your mod_security implementation.
#
#<IfModule mod_security.c>
#    <Directory /usr/share/phpMyAdmin/>
#        SecRuleInheritance Off
#    </Directory>
#</IfModule>

On relance le serveur Apache:
# service httpd restart

 Création d'un site web

On va créer le site "test.home" qui s’exécutera avec le compte "test".
Création du comte :
# useradd -d /home/test -m -U -s /bin/bash test
# mkdir /home/test/www
# chown -R test:test /home/test/
# chmod -R 750 /home/test
# chmod -R g+s /home/test/
# passwd test
On fait un fichier /etc/httpd/conf.d/hosts/test.home.conf avec le contenu :
<VirtualHost *:80>
   ServerAdmin webmaster@test.home
   ServerName test.home
   ServerAlias *.test.home
   DocumentRoot /home/test/www
   <Directory />
      Options FollowSymLinks
      AllowOverride None
   </Directory>
   <Directory /home/test/www>
      Options ExecCGI Indexes FollowSymLinks MultiViews
      AllowOverride all
      Order allow,deny
      Allow from all
      # Configuration de suPHP
      suPHP_Engine on
      suPHP_UserGroup test test
      suPHP_ConfigPath /etc/
      suPHP_AddHandler x-httpd-php
      AddHandler x-httpd-php .php .php3 .php4 .php5
   </Directory>
   AddType application/x-httpd-php .php
   DirectoryIndex index.html index.php
</VirtualHost>

Création de la base MySQL

On se connecte sur http://test.home/phpmyadmin avec le compte root et on peut créer la base de données.

Conclusion

J'ai créé un serveur LAMP avec cette méthode. J'espère que le serveur est à peu prés sécurisé bien que je pense qu'il manque quelques paramètres de sécurisation comme l'interdiction de certaines fonction PHP par exemple.

Bonus

Un script qui fait l'installation complète LAMP avec suPHP et qui crée un script de création de compte utilisateur web : Le script setup_lamp.sh
Il faut modifier le script avant de l'utiliser pour définir le mot de passe root MySql ainsi que les IP ayant le droit d'accéder à phpMyAdmin.
De toutes façon IL FAUT RELIRE un script avant de l'exécuter... c'est le minimum.

Aucun commentaire:

Enregistrer un commentaire