Skip to content

Instantly share code, notes, and snippets.

@mightydes
Last active December 19, 2015 01:29
Show Gist options
  • Save mightydes/5876652 to your computer and use it in GitHub Desktop.
Save mightydes/5876652 to your computer and use it in GitHub Desktop.
Centos 6.2 for Web-Dev: * php * nginx * mysql * php-fpm
Tools
$ yum install git tree httpd-tools
$ yum install ImageMagick
Add repos
$ wget http://dl.fedoraproject.org/pub/epel/6/x86_64/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
Enable the remi repository
$ nano /etc/yum.repos.d/remi.repo
...
enabled=1
^o^x
Add users
$ adduser -G wheel -d /var/www/ www-data
$ passwd www-data
$ nano /etc/group
$ visudo
The command will open up the sudoers file with the vi editor.
The vi editor has two modes, command mode and insert mode.
Command mode allows vi to accept input commands while insert mode actually modifies the file.
The important vi commands that we will use are ":q" (quit) and ":w" (save).
To move into insert mode press the 'i' key a single time. To move back into command mode press the Esc key.
Use the 'j', 'k', or arrow keys to scroll up or down in the file.
Find the following:
## Allows people in group wheel to run all commands
# %wheel ALL=(ALL) ALL
and uncomment second line. Then press "Esc", ":w", ":q".
Add ssh key
$ mkdir /var/www/.ssh
$ nano /var/www/.ssh/authorized_keys
ssh-rsa {public key} {key comment}
^o^x
Install PHP Modules
$ yum install php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-apc php-cli php-pear php-pdo php-process php-intl
$ nano /etc/php.ini
cgi.fix_pathinfo=0
date.timezone = "Europe/Moscow"
error_reporting = E_ALL & ~E_NOTICE
display_errors = On
session.gc_maxlifetime = 28800
^o^x
Install php-fpm
$ yum install php-fpm
$ mkdir /var/www/tmp
$ nano /etc/php-fpm.d/www.conf
...
listen = 127.0.0.1:9000
replace with
listen = /var/www/tmp/php5-fpm.sock
...
; 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 = www-data
; RPM: Keep a group allowed to write in log dir.
group = wheel
...
php_value[session.save_path] = /var/lib/php/session
replace with
php_value[session.save_path] = /var/www/tmp
^o^x
Install Nginx
$ yum install nginx
$ mkdir /etc/nginx/conf.d/std
$ mkdir /etc/nginx/conf.d/sites-available
$ mkdir /etc/nginx/conf.d/sites-enabled
$ mkdir /var/www/sites
$ nano /etc/nginx/conf.d/std/listen
listen [::]:80;
^o^x
$ nano /etc/nginx/conf.d/std/www_redirect
server {
include conf.d/std/listen;
server_name ~^(www\.)(?<domain>.+)$;
return 301 $scheme://$domain$request_uri;
}
^o^x
$ nano /etc/nginx/conf.d/std/rewrite
location / {
try_files $uri $uri/ /index.php?$args;
}
^o^x
$ nano /etc/nginx/conf.d/std/fastcgi
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/www/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
^o^x
$ nano /etc/nginx/conf.d/std/symfony
rewrite ^/app\.php/?(.*)$ /$1 permanent;
try_files $uri @rewriteapp;
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# Deny all . files
location ~ /\. {
deny all;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
try_files $uri =404;
fastcgi_pass unix:/var/www/tmp/php5-fpm.sock;
fastcgi_index app.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_read_timeout 1800;
send_timeout 1800;
include fastcgi_params;
}
^o^x
$ nano /etc/nginx/conf.d/std/ht_ignore
location ~ /\.ht {
deny all;
}
^o^x
$ nano /etc/nginx/conf.d/std/subdomains
server {
include conf.d/std/listen;
server_name ~^(?<domain>([^w]|w[^w]|ww[^w]|www[^.]|www\.([^.]+)\.([^.]+)\.).+)$;
root /var/www/sites/$domain/web;
include conf.d/std/rewrite;
include conf.d/std/fastcgi;
include conf.d/std/ht_ignore;
}
^o^x
$ nano /etc/nginx/nginx.conf
user www-data;
worker_processes 4;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_comp_level 8;
gzip_disable msie6;
gzip_proxied any;
gzip_types text/plain application/xml application/x-javascript text/css;
index index.php index.html index.htm;
# std
include conf.d/std/www_redirect;
# sites-enabled
include conf.d/sites-enabled/*;
# subdomains
include conf.d/std/subdomains;
}
^o^x
$ nano /etc/nginx/conf.d/sites-available/example.com
server {
include conf.d/std/listen;
server_name example.com test.example.com;
root /var/www/sites/example.com/web;
include conf.d/std/rewrite;
include conf.d/std/fastcgi;
include conf.d/std/ht_ignore;
}
^o^x
Basic HTTP Authentication With Nginx
$ htpasswd -c /var/www/sites/example.com/.htpasswd {user name}
Nginx conf
location /admin {
auth_basic "Restricted Area";
auth_basic_user_file /var/www/sites/example.com/.htpasswd;
}
Install MySQL
$ yum install mysql mysql-server
$ service mysqld start
$ mysql_secure_installation
Autostart
$ chkconfig --levels 235 mysqld on
$ chkconfig --levels 235 nginx on
$ chkconfig --levels 235 php-fpm on
Composer
$ cd /var/www
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
Before start
$ chown -R www-data.wheel /var/lib/nginx
$ chown -R www-data.wheel /var/www/
phpMyAdmin
$ mkdir /var/www/pma
$ cd /var/www/pma
$ composer --prefer-dist create-project phpmyadmin/phpmyadmin phpmyadmin dev-STABLE
$ ln -s phpmyadmin/ web
$ mysql -u root -p{root password}
mysql> CREATE USER 'pma'@'localhost' IDENTIFIED BY '{pma password}';
mysql> GRANT ALL PRIVILEGES ON * . * TO 'pma'@'localhost';
mysql> CREATE USER '{main user}'@'localhost' IDENTIFIED BY '{main user password}';
mysql> GRANT ALL PRIVILEGES ON * . * TO '{main user}'@'localhost';
mysql> SOURCE web/examples/create_tables.sql
mysql> \q
$ mv phpmyadmin/config.sample.inc.php config.inc.php
$ nano config.inc.php
...
$cfg['LoginCookieValidity'] = 28800;
...
$cfg['Servers'][$i]['AllowRoot'] = false;
...
~NB~ Uncomment all in 'phpMyAdmin configuration storage settings'
$cfg['Servers'][$i]['controluser'] = 'pma';
$cfg['Servers'][$i]['controlpass'] = '{pma password}';
^o^x
$ cd web
$ ln -s ../config.inc.php config.inc.php
DB hints
Tables dump
$ mysqldump -uroot -p{root password} {DB name} | gzip > {file}.sql.gz
Source dump
$ gzip -d {file}.sql.gz
$ mysql -u root -p{root password}
mysql> USE {DB name}
mysql> SOURCE {file}.sql
mysql> \q
Simple DB backups
$ mkdir /var/www/crontab
$ mkdir /var/www/backups
$ mkdir /var/www/backups/db
$ nano /var/www/crontab/mysql_dump.sh
$ chmod +x /var/www/crontab/mysql_dump.sh
#!/bin/sh
# Backup to...
BACKUPDIR=/var/www/backups/db
# MySQL databases to backup
MYSQLDBS="{db_one} {db_two} {db_jango}"
# Date
DATE="$(date +"%Y-%m-%d")"
# Backup MySQL databases
mkdir $BACKUPDIR/$DATE
for db in $MYSQLDBS; do
mysqldump -uroot -p{root password} $db|gzip > $BACKUPDIR/$DATE/$db.sql.gz
done
^o^x
nano /etc/crontab
~NB~ add line
5 3 * * * www-data bash /var/www/crontab/mysql_dump.sh
^o^x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment