Install Nginx, MySQL, PHP-FPM, Joomla in Debian 8

  • Category: 電腦相關
  • Last Updated: Tuesday, 08 December 2015 11:45
  • Published: Tuesday, 13 October 2015 01:10
  • Written by sam

Move Windows LAMP to Debian 8 (Jessie) and use Nginx to replace Apache, also setup rewriting http to https.

for apt installing latest packages

root@boredom:~# echo 'APT::Install-Recommends "0"; APT::Install-Suggests "0";' > 99recommended

backup source

root@boredom:~# cp /etc/apt/sources.list{,.orig} && vi /etc/apt/sources.list

add testing sources
for Debian Jessie

 

deb http://http.us.debian.org/debian testing main contrib
deb http://security.debian.org testing/updates main contrib
deb http://http.us.debian.org/debian testing-updates main

up-to-date

root@boredom:~# apt-get update
root@boredom:~# apt-get upgrade
root@boredom:~# apt-get dist-upgrade

install MySQL

root@boredom:~# apt-get install mysql-server
root@boredom:~# cp /etc/mysql/my.cnf{,.orig} && vi /etc/mysql/my.cnf
key_buffer_size                 = 16M
query_cache_size                = 1M
thread_cache_size               = 4M
max_allowed_packet              = 1M
table_open_cache                = 4
sort_buffer_size                = 512K
net_buffer_length               = 2K
read_buffer_size                = 16K
read_rnd_buffer_size            = 16K
myisam_sort_buffer_size         = 32K
max_connections                 = 20
bind-address                    = 127.0.0.1
thread_stack                    = 128K
myisam-recover                  = BACKUP
query_cache_limit               = 1M
bulk_insert_buffer_size         = 32K
innodb_buffer_pool_size         = 5M
innodb_additional_mem_pool_size = 512K

set PW to account

root@boredom:~# vi ~/.my.cnf
[client]
user=root
password=PW
socket=/var/run/mysqld/mysqld.sock
root@boredom:~# systemctl restart mysql
root@boredom:~# update-rc.d -f mysql enable

for secure

root@boredom:~# mysql_secure_installation
Enter current password for root
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
root@boredom:~# apt-get install nginx
root@boredom:~# cp /etc/nginx/nginx.conf{,.orig} && vi /etc/nginx/nginx.conf

if you just want use http

root@boredom:/etc/nginx/sites-available# cat boredom.gotdns.com.orig
server {
    listen 80;
    root /www/boredom.gotdns.com;
    server_name boredom.gotdns.com;
    access_log /var/log/nginx/boredom-access;
    #access_log off;
    error_log /var/log/nginx/boredom-error error;

    location / {
        root /www/boredom.gotdns.com;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?q=$request_uri;
        }
    ## php block
    location ~ \.php?$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm.socket;
        #fastcgi_pass 127.0.0.1:9001;
                fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_hide_header X-Powered-By;
    }
}

you want https and auto rewriting http to https

root@boredom:/etc/nginx/sites-available# cat boredom.gotdns.com
server {
    listen 80;
    server_name boredom.gotdns.com;
#    rewrite ^(.*) boredom.gotdns.com$1 permanent;
    return 301 https://$server_name$request_uri;
}
server {
    listen 443 default;
    server_name boredom.gotdns.com;

    ssl    on;
    ssl_certificate        /etc/nginx/ssl/boredom.crt;
    ssl_certificate_key    /etc/nginx/ssl/boredom.key;
    
    ssl_session_timeout  5m;
    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    ssl_prefer_server_ciphers   on;

    access_log /var/log/nginx/boredom.ssl-access;
    #access_log off;
    error_log /var/log/nginx/boredom.ssl-error error;

    client_max_body_size 5m;
    client_body_timeout 60;

    root /www/boredom.gotdns.com;
    index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?q=$request_uri;

    ## php block
    location ~ \.php?$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm.socket;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_hide_header X-Powered-By;
    }
}

disable default nginx virtual host

root@boredom:~# rm -f /etc/nginx/sites-{enabled,available}/default
root@boredom:~# vi /etc/nginx/sites-available/boredom.gotdns.com
server {
    listen 80;
    root /www/boredom.gotdns.com;
    server_name boredom.gotdns.com;
    access_log /var/log/nginx/boredom-access;
    #access_log off;
    error_log /var/log/nginx/boredom-error error;

    location / {
        root /www/boredom.gotdns.com;
        index index.php index.html index.htm;
  }
    ## php block
    location ~ \.php?$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm.socket;
        #fastcgi_pass 127.0.0.1:9001;
    fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_hide_header X-Powered-By;
    }
}
cd /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/boredom.gotdns.com
root@boredom:~# apt-get install php5-fpm php5-cli php5-mysql
root@boredom:~# vi /etc/php5/fpm/php.ini
cgi.fix_pathinfo=0
date.timezone = Asia/Taipei
memory_limit = 64M
expose_php = Off
root@boredom:~# vi /etc/php5/fpm/php-fpm.conf
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 10
root@boredom:~# cp /etc/php5/fpm/pool.d/www.conf{,.orig} && vi /etc/php5/fpm/pool.d/www.conf
listen = /var/run/php-fpm.socket
user = www-data
group = www-data
request_slowlog_timeout = 5s
slowlog = /var/log/php5-fpm.log
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500
listen.backlog = -1
pm.status_path = /status
request_terminate_timeout = 120s
rlimit_files = 32768
rlimit_core = unlimited
catch_workers_output = yes
php_value[session.save_handler] = files
php_admin_flag[log_errors] = on
php_admin_value[error_log] = /var/log/php5-fpm-error.log
root@boredom:~# service php5-fpm restart
root@boredom:~# update-rc.d -f php5-fpm enable
root@boredom:~# apt-get install php-apc
root@boredom:~# service php5-fpm restart

then import backup sql and website data

mysql -u root
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| j25                |
| mysql              |
| performance_schema |
+--------------------+
mysql> drop database j25;
mysql> create database j25;
mysql> quit
Bye
mysql -u root -h localhost j25 < j25.sql

last, modify /www/boredom.gotdns.com/configuration.php

Finish.