一、LNMP的下載
LNMP一鍵安裝包是一個用Linux Shell編寫的可以為CentOS/RadHat/Fedora、Debian/Ubuntu/Raspbian/Deepin VPS或獨立主機安裝LNMP(Nginx/MySQL/PHP)、LNMPA(Nginx/MySQL/PHP/Apache)、LAMP(Apache/MySQL/PHP)生產環境的Shell程序。同時提供一些實用的輔助工具如:虛擬主機管理、FTP用戶管理、Nginx、MySQL/MariaDB、PHP的升級、常用緩存組件Redis、Xcache等的安裝、重置MySQL root密碼、502自動重啟、日志切割、SSH防護DenyHosts/Fail2Ban、備份等許多實用腳本。
官方網站為 https://www.lnmp.org/
二、LNMP的安裝
安裝時,MySQL版本選擇5.5.48及以上(5.4以上支持utf8mb4),PHP版本選擇5.6或7
也可以根據實際情況選擇其他版本。
三、常用配置
1. 安裝memcache
命令如下
[root@www.fangbei.org lnmp1.3-full]# ./addons.sh install memcached
+-----------------------------------------------------------------------+
| Addons script for LNMP V1.2, Written by Licess |
+-----------------------------------------------------------------------+
| A tool to Install cache,optimizer,accelerator...addons for LNMP |
+-----------------------------------------------------------------------+
| For more information please visit http://www.lnmp.org |
+-----------------------------------------------------------------------+ Which memcached php extension do you choose: Install php-memcache,(Discuz x) please enter: 1 Install php-memcached, please enter: 2 Enter 1 or 2 (Default 1): You choose php-memcache ====== Installing memcached ====== Press any key to install...or Press Ctrl+c to cancel
2. 啟用PATH_INFO支持
該功能用於ThinkPHP開發時,URL_MODEL為2時的支持。
網站配置目錄 /usr/local/nginx/conf/vhost/www.fangbei.org.conf
打開並添加如下紅包部分
server { listen 80; #listen [::]:80; server_name www.fangbei.org; index index.html index.htm index.php default.html default.htm default.php; root /home/wwwroot/www.fangbei.org; include other.conf; #error_page 404 /404.html; include enable-php.conf; location / { if (!-e $request_filename){ rewrite ^/(.*)$ /index.php?s=/$1 last; } } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } access_log off; }
3. 禁用mysql日志
在MySQL數據庫中,mysql-bin.000001、mysql- bin.000002等文件是數據庫的操作日志,例如UPDATE一個表,或者DELETE一些數據,即使該語句沒有匹配的數據,這個命令也會存儲到日志文件中,還包括每個語句執行的時間,也會記錄進去的。
比如如下
[root@www.fangbei.org /]# cd /usr/local/mysql/var/ [root@www.fangbei.org var]# ll total 29964 drwx------ 2 mysql mysql 4096 Feb 21 17:42 100893722 -rw-r----- 1 mysql root 8477 Feb 21 18:41 www.fangbei.org.err -rw-rw---- 1 mysql mysql 5 Feb 21 18:41 www.fangbei.org.pid -rw-rw---- 1 mysql mysql 18874368 Feb 21 18:41 ibdata1 -rw-rw---- 1 mysql mysql 5242880 Feb 21 18:41 ib_logfile0 -rw-rw---- 1 mysql mysql 5242880 Feb 21 17:08 ib_logfile1 drwx------ 2 mysql mysql 4096 Feb 21 17:08 mysql -rw-rw---- 1 mysql mysql 27735 Feb 21 17:08 mysql-bin.000001 -rw-rw---- 1 mysql mysql 1113246 Feb 21 17:08 mysql-bin.000002 -rw-rw---- 1 mysql mysql 264 Feb 21 17:08 mysql-bin.000003 -rw-rw---- 1 mysql mysql 1119 Feb 21 17:08 mysql-bin.000004 -rw-rw---- 1 mysql mysql 126 Feb 21 17:08 mysql-bin.000005 -rw-rw---- 1 mysql mysql 127076 Feb 21 18:41 mysql-bin.000006 -rw-rw---- 1 mysql mysql 107 Feb 21 18:41 mysql-bin.000007 -rw-rw---- 1 mysql mysql 133 Feb 21 18:41 mysql-bin.index drwx------ 2 mysql mysql 4096 Feb 21 17:08 performance_schema [root@www.fangbei.org var]#
以及
[root@www.fangbei.org var]# cd /root [root@www.fangbei.org ~]# ll total 491064 drwxr-xr-x 4 mysql mysql 4096 Feb 21 16:44 databases_backup_20170221164407 drwxr-xr-x 7 root root 4096 May 27 2016 lnmp1.3-full -rw-r--r-- 1 root root 500932095 Dec 4 12:32 lnmp1.3-full.tar.gz -rw-r--r-- 1 root root 1898084 Feb 21 17:18 lnmp-install.log [root@www.fangbei.org ~]# cd databases_backup_20170221164407/ [root@www.fangbei.org databases_backup_20170221164407]# ll total 21284 drwx------ 2 mysql mysql 4096 Feb 21 16:38 100893722 -rw-r----- 1 mysql root 4654 Feb 21 16:44 www.fangbei.org.err -rw-rw---- 1 mysql mysql 10485760 Feb 21 16:44 ibdata1 -rw-rw---- 1 mysql mysql 5242880 Feb 21 16:44 ib_logfile0 -rw-rw---- 1 mysql mysql 5242880 Feb 21 16:02 ib_logfile1 drwx------ 2 mysql mysql 4096 Feb 21 16:02 mysql -rw-rw---- 1 mysql mysql 19758 Feb 21 16:02 mysql-bin.000001 -rw-rw---- 1 mysql mysql 765307 Feb 21 16:02 mysql-bin.000002 -rw-rw---- 1 mysql mysql 263 Feb 21 16:02 mysql-bin.000003 -rw-rw---- 1 mysql mysql 731 Feb 21 16:02 mysql-bin.000004 -rw-rw---- 1 mysql mysql 125 Feb 21 16:02 mysql-bin.000005 -rw-rw---- 1 mysql mysql 497 Feb 21 16:44 mysql-bin.000006 -rw-rw---- 1 mysql mysql 114 Feb 21 16:11 mysql-bin.index [root@www.fangbei.org databases_backup_20170221164407]#
這些文件的生長速度是很快的,特別占用服務器資源。當服務器資源不夠充足的情況下,我們可以選擇定期刪除這些日志文件,或者修改配置文件,不讓日志生成。
編輯my.cnf 文件,命令:vi /etc/my.cnf
注釋掉 log-bin=mysql-bin 這個屬性
#skip-networking max_connections = 500 max_connect_errors = 100 open_files_limit = 65535 # log-bin=mysql-bin binlog_format=mixed server-id = 1 expire_logs_days = 10
然后刪除上述日志文件
4. phpMyAdmin超時問題
文件 home/wwwroot/default/phpmyadmin/libraries/config.default.php
設置 Cookies 有效期為12小時
$cfg['LoginCookieValidity'] = 43200; //12個小時
設定PHP session有效期的參數值 session.gc_maxlifetime 為大於12小時的秒數。下述配置多了10秒。
文件在./usr/local/php/etc/php.ini
session.gc_maxlifetime = 43210
5. phpMyAdmin輸入服務器地址
修改phpMyAdmin目錄下的 /libraries/config.default.php中 AllowArbitraryServer 值為 true
$cfg[‘AllowArbitraryServer’] = true;
9. 重啟
最后重啟 lnmp restart,讓上述所有配置生效
10. 刪除.user.ini文件
LNMP 1.2開始PHP防跨目錄限制使用.user.ini,該文件在網站根目錄下,可以修改open_basedir的值來設置限制目錄的訪問。
.user.ini文件無法直接修改,而且是隱藏文件可能在winscp下可能無法看到。
當網站被刪除的時候,.user.ini可以用以下方法刪除
[root@www.fangbei.org]# chattr -i ".user.ini"
[root@www.fangbei.org]# chmod 777 ".user.ini" [root@www.fangbei.org]# rm -rf ".user.ini"
11. 允許mysql遠程連接
1. 修改密碼
[root@cctv3]# /usr/bin/mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 39655 Server version: 5.5.60 Source distribution mysql> mysql> set password for root@localhost = password('pass1234'); Query OK, 0 rows affected (0.00 sec)
2. 授權
mysql> use idcode; Database changed mysql> mysql> grant all privileges on *.* to root@'%' identified by 'pass1234' with grant option; Query OK, 0 rows affected (0.00 sec) mysql> mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> mysql> select host,user,password from user; +-----------+------+-------------------------------------------+ | host | user | password | +-----------+------+-------------------------------------------+ | localhost | root | *ASDFF1B8AD1B4CFD578E76ABC1B6ADFF70D0CCTV | | % | root | *ASDFF1B8AD1B4CFD578E76ABC1B6ADFF70D0CCTV | +-----------+------+-------------------------------------------+ 3 rows in set (0.00 sec) mysql>
3、重啟
在路由表中移除3306端口的禁止條列
[root@Server-WeiXin ~]# iptables -L -n --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 3 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 6 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 7 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 8 8 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:11211 9 DROP udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:11211 [root@Server-WeiXin ~]# iptables -D INPUT 6 [root@Server-WeiXin ~]# iptables -L -n --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 3 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 6 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 8 7 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:11211 8 DROP udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:11211 [root@Server-WeiXin ~]#
設置阿里雲內網入方向規則
11. 配置https及證書,啟用啟用TLS1.2
server { listen 80; listen 443 ssl; #listen [::]:80; server_name cet.fangbei.org; index index.html index.htm index.php default.html default.htm default.php; root /home/wwwroot/cet.fangbei.org; include none.conf; #error_page 404 /404.html; include enable-php.conf;
ssl_protocols TLSv1.2; ssl_certificate /usr/local/nginx/cert/214130435490250.pem; ssl_certificate_key /usr/local/nginx/cert/214130435490250.key; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } access_log off; }
12. 配置301跳轉
server { listen 80; server_name lnmp.org; return 301 http://www.lnmp.org$request_uri; }