系統(tǒng)環(huán)境:Centos Linux release 7.5
軟件版本:myslq 5.7.24
? ? ? ? ? ? ? ? ? ? nginx1.14.0
? ? ? ? ? ? ? ? ? ? php7.1.23擴(kuò)展(redis,igbinary,inotify,swoole)
? ? ? ? ? ? ? ? ? ? ssl
? ? ? ? ? ? ? ? ? ? denyhosts
一、安裝 mysql 5.7.24
? ? ? ? ? ? 安裝yum repo, 由于centos 的yum源中沒有mysql ,需要到mysql的官網(wǎng)下載 yum repo 配置文件:
????????????yum install wget vim unzip gcc-c++ -y
? ??????????wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
? ? ? ? 然后進(jìn)行repo的安裝:
? ? ? ? ? ? rpm -ivh mysql57-community-release-el7-9.noarch.rpm
? ? ? ? ? ? 執(zhí)行完成之后在/etc/yum.repos.d/目錄下生成兩個(gè)repo文件mysql-community.repo mysql-community-source.repo
? ? ? ? ? ? ll /etc/yum.repos.d/
? ? ? ? ? ? 開始安裝MySQL使用yum命令即可完成安裝
????????????yum install mysql-server –y
? ? ? ? ? ? 啟動MySQL:
? ? ? ? ? ? systemctl start mysqld
? ? ? ? ? ? 獲取安裝時(shí)的臨時(shí)密碼:
? ? ? ? ? ? grep 'temporary password' /var/log/mysqld.log
? ? ? ? ? ? 用查詢的密碼登陸:
? ? ? ? ? ? mysql -u root -p?
? ? ? ? ? ? 登陸成功后修改密碼:
? ? ? ? ? ? set global validate_password_policy=0;
? ? ? ? ? ? set password=password("12345678")
? ? ? ? ? ? 配置默認(rèn)編碼為utf8:
? ? ? ? ? ? vim /etc/my.cnf ?添加
? ? ? ? ? ? [mysqld]
? ? ? ? ? ? character_set_server=utf8
? ? ? ? ? ? init_connect='SET NAMES utf8'
附常用命令:
? ? ? ? mysql_secure_installation ? #設(shè)置安全選項(xiàng)
? ? ? ? ?systemctl stop mysqld ? ? ? ? ? #關(guān)閉mysql
? ? ? ? ?systemctl restart mysqld ? ? ? ? #重啟mysql
? ? ? ? ? systemctl status mysqld ? ? ? ?#查看mysql運(yùn)行狀態(tài)
? ? ? ? ? ? systemctl enable mysqld ? ? ? ?#設(shè)置開機(jī)啟動
? ? ? ? ? ? systemctl disable mysqld ? ? ? #關(guān)閉開機(jī)啟動
默認(rèn)配置路徑:
? ? ? ? 配置文件:/etc/my.cnf
? ? ? ? 日志文件:/var/log/mysqld.log
? ? ? ? 服務(wù)啟動腳本:/usr/lib/systemd/system/mysqld.service
? ? ? ? socket文件:/var/run/mysqld/mysqld.pid
二、安裝ngnix1.14.0
? ? ? ? 編譯安裝前所需要的準(zhǔn)備:安裝GCC編譯器 PCRE庫 zlib庫 OpenSSL庫
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel?
下載安裝包
? ? ? ? wget http://nginx.org/download/nginx-1.14.0.tar.gz
解壓安裝
? ? ? ? tar -zxvf nginx-1.14.0.tar.gz
?????cd nginx-1.14.0/
? ??????./configure --prefix=/usr/local/nginx --pid-path=/run/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre
編譯安裝
make
make install
防火墻增加80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
防火墻配置生效
firewall-cmd --reload
啟動nginx
/usr/local/nginx/sbin/nginx
訪問網(wǎng)頁
設(shè)置ngnix開機(jī)自啟,即在rc.local增加啟動代碼就可以了
vim /etc/rc.local 增加一行
/usr/local/nginx/sbin/nginx
設(shè)置執(zhí)行權(quán)限:
chmod 755 /etc/rc.local
關(guān)閉SELINUX?
vim /etc/selinux/config 將SELINUX=enforcing改為SELINUX=disabled
附(Nginx部分控制命令):默認(rèn)Nginx安裝在/usr/local/nginx/中,因此
/usr/local/nginx/sbin/nginx? ? ? ? ? #默認(rèn)啟動start
/usr/local/nginx/sbin/nginx -t? ? ? ? #測試配置信息
/usr/local/nginx/sbin/nginx -s stop? ? #快速停止服務(wù)
/usr/local/nginx/sbin/nginx -s quit? ? #正常停止服務(wù)
/usr/local/nginx/sbin/nginx -s reload? #重啟
安裝PHP7.1.23
安裝libmcrypt擴(kuò)展需要安裝第三方y(tǒng)um源
wget http://www.atomicorp.com/installers/atomic
sh ./atomic (默認(rèn)回車就行)
編譯安裝前所需要的準(zhǔn)備
yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libxslt libxslt-devel
安裝所依賴的包
yum install php-mcrypt -y
yum install libmcrypt -y
yum install libmcrypt-devel -y
yum install bzip2-devel -y
yum install gmp-devel -y
yum install readline-devel –y
yum install psmisc –y
下載安裝包
wget -O php-7.1.23.tar.gz http://am1.php.net/get/php-7.1.23.tar.gz/from/this/mirror?
解壓
tar -zxvf php-7.1.23.tar.gz
進(jìn)入目錄
cd php-7.1.23
開始配置
./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-libxml-dir --with-xmlrpc --with-openssl --with-mcrypt --with-mhash --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-opcache
開始編譯安裝
make
make install
配置php環(huán)境變量,? vim /etc/profile 在末尾加上
PATH=$PATH:/usr/local/php/bin
export PATH
保存后使立即生效
source /etc/profile
查看PHP版本 php -v
設(shè)置php開機(jī)自啟,即在rc.local增加啟動代碼就可以了
vim /etc/rc.local? 增加一行
/usr/local/php/sbin/php-fpm
設(shè)置執(zhí)行權(quán)限:
chmod 755 /etc/rc.local
附(PHP部分控制命令)
/usr/local/php/sbin/php-fpm? #啟動PHP
killall php-fpm? ? #關(guān)閉PHP
配置nginx支持php-fpm
PHP-FPM是一個(gè)PHP FastCGI管理器。PHP-FPM提供了更好的PHP進(jìn)程管理方式,可以有效控制內(nèi)存和進(jìn)程、可以平滑重載PHP配置。新版PHP已經(jīng)集成了PHP-FPM,可以直接使用。
php的默認(rèn)安裝位置在 /usr/local/php
對php-fpm運(yùn)行用戶進(jìn)行設(shè)置
首先創(chuàng)建web用戶
用戶 組都設(shè)置為www-data,可以自己定(實(shí)際上在配置時(shí)已經(jīng)設(shè)為www-data了)
groupadd www-data
useradd -g www-data www-data
復(fù)制一份php-fpm配置文件
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
打開配置文件
vim php-fpm.conf
發(fā)現(xiàn)最后一行引入php-fpm.d目錄下所有 .conf 類型的配置文件
打開 php-fpm.d目錄,復(fù)制默認(rèn)配置文件并打開修改
cd php-fpm.d
cp www.conf.default? www.conf
vim www.conf? 修改用戶和用戶組為www-data用戶
保存退出,啟動php-fpm服務(wù)
/usr/local/php/sbin/php-fpm
php-fpm默認(rèn)使用9000端口,使用如下命令查看啟動狀態(tài)
netstat -lnt | grep 9000
nginx相關(guān)配置打開nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
修改用戶組第一行注釋去掉 nobody改為www-data
添加index.php,使nginx默認(rèn)使用index.php為入口頁
配置php-fpm模塊,修改第69行的/scripts 為 $document_root
以上都修改后保存退出,先檢測相關(guān)配置是否正確
/usr/local/nginx/sbin/nginx -t
顯示正確,重啟nginx
/usr/local/nginx/sbin/nginx -s reload
切到nginx下的html目錄(默認(rèn)代碼目錄)新建index.php
cd ?/usr/local/nginx/html
vim index.php
測試訪問對應(yīng)地址是否可以運(yùn)行
看到這界面說明php成功運(yùn)行。
安裝redis-5.0.0
下載redis-5.0.0安裝包
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
解壓
tar -zxvf redis-5.0.0.tar.gz
cd redis-5.0.0
編譯安裝
make
切換至redis的src目錄
cd src
cp redis-server redis-cli /usr/local/bin
cp redis-sentinel redis-benchmark redis-check-aof redis-check-dump /usr/local/bin
創(chuàng)建配置目錄
mkdir /etc/redis
mkdir -p /var/lib/redis/6379
cp /root/redis-5.0.0/redis.conf? /etc/redis/6379.conf
修改配置文件 vim /etc/redis/6379.conf
daemonize yes
pidfile /var/run/redis_6379.pid
port 6379
oglevel notice
dir /var/lib/redis/6379
創(chuàng)建服務(wù)
sysctl -w vm.overcommit_memory=1
sysctl -w net.core.somaxconn=512.
echo never > /sys/kernel/mm/transparent_hugepage/enabled
cp /root/redis-5.0.0/utils/redis_init_script /etc/init.d/redis_6379
vim /etc/systemd/system/redis.service? 內(nèi)容如下
[Unit]
Description=Redis on port 6379
[Service]
Type=forking
ExecStart=/etc/init.d/redis_6379 start
ExecStop=/etc/init.d/redis_6379 stop
[Install]
WantedBy=multi-user.target
啟動服務(wù)
systemctl enable redis
systemctl daemon-reload
systemctl start redis
systemctl status redis
php7安裝redis擴(kuò)展
配置phpize
wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
tar -zvxf m4-1.4.9.tar.gz
cd m4-1.4.9/
編譯安裝
./configure && make && make install
配置autoconf
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
tar -zvxf autoconf-2.62.tar.gz
cd autoconf-2.62/
編譯安裝
./configure && make && make install
yum install m4 –y
yum install autoconf –y
下載包
wget https://codeload.github.com/phpredis/phpredis/zip/develop
mv develop phpredis-develop.zip
unzip phpredis-develop.zip
cd phpredis-develop
生成configure配置文件:
/usr/local/php/bin/phpize
編譯安裝:
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
配置/etc/php.ini 添加
vim /etc/php.ini
extension=redis.so
重啟php
killall php-fpm
/usr/local/php/sbin/php-fpm
訪問http://luntan.0728idc.com/index.php,查找Redis
php7安裝inotify擴(kuò)展
pecl install inotify
配置/etc/php.ini 添加
vim /etc/php.ini
extension=inotify.so
重啟php
killall php-fpm
/usr/local/php/sbin/php-fpm
訪問http://luntan.0728idc.com/index.php,查找inotify
php7安裝igbinary擴(kuò)展
pecl install igbinary
配置/etc/php.ini 添加
vim /etc/php.ini
extension=igbinary.so
重啟php
killall php-fpm
/usr/local/php/sbin/php-fpm
訪問http://luntan.0728idc.com/index.php,查找igbinary
php7安裝swoole擴(kuò)展
下載安裝包
wget https://codeload.github.com/swoole/swoole-src/tar.gz/v4.2.6
tar -zxvf v4.2.6
cd swoole-src-4.2.6/
生成configure配置文件:
/usr/local/php/bin/phpize
編譯安裝
./configure --enable-async-mysql
make
make install
配置/etc/php.ini 添加
vim /etc/php.ini
extension= swoole.so
重啟php
killall php-fpm
/usr/local/php/sbin/php-fpm
訪問luntan.0728idc.com/index.php,查找swoole
十、在Nginx上配置多個(gè)站點(diǎn)
在Nginx配置目錄下,創(chuàng)建一個(gè)”vhost”目錄
mkdir? /usr/local/nginx/conf/vhost
創(chuàng)建site luntan.0728idc.com的配置文件
vim /usr/local/nginx/conf/vhost/luntan.0728idc.com.conf
server {
? ? listen? ? ? 80;
? ? server_name luntan.0728idc.com;
? ? root /app/luntan.0728idc.com/;
? ? index index.html index.htm index.php;
? ? location / {
? ? ? ? if (!-e $request_filename) {
? ? ? ? ? ? rewrite (.*) /index.php;
? ? ? ? }
? ? }
? ? ? location ~ \.php$ {
? ? ? root? ? ? ? ? /app/luntan.0728idc.com;
? ? ? fastcgi_pass? 127.0.0.1:9000;
? ? ? fastcgi_index? index.php;
? ? ? fastcgi_param? SCRIPT_FILENAME? $document_root$fastcgi_script_name;
? ? ? include? ? ? ? fastcgi_params;
? ? ? }
}
打開nginx.conf文件
vim /usr/local/nginx/conf/nginx.conf
將虛擬目錄的配置文件加入到”http {}”部分的末尾 ,注意注釋server {}內(nèi)容
include /usr/local/nginx/conf/vhost/*.conf;
重啟Nginx服務(wù)
/usr/local/nginx/sbin/nginx -s reload?
在Nginx配置SSL 證書使用https訪問
防火墻增加443端口
firewall-cmd --zone=public --add-port=443/tcp --permanent
防火墻配置生效
firewall-cmd --reload
上傳證書文件并修改站點(diǎn)配置文件
vim /usr/local/nginx/conf/vhost/luntan.0728idc.com.conf
server {
? ? listen? ? ? 443;
? ? server_name luntan.0728idc.com;
? ? root /app/luntan.0728idc.com/;
? ? index index.html index.htm index.php;
? ? ssl on;
? ? ssl_certificate /usr/local/nginx/conf/ssl/luntan.0728idc.com.crt;
? ? ssl_certificate_key /usr/local/nginx/conf/ssl/luntan.0728idc.com.key;
? ? ssl_session_timeout 5m;
? ? ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
? ? ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
? ? ssl_prefer_server_ciphers on;
? ? location / {
? ? ? ? if (!-e $request_filename) {
? ? ? ? ? ? rewrite (.*) /index.php;
? ? ? ? }
? ? }
? ? ? location ~ \.php$ {
? ? ? root? ? ? ? ? /app/luntan.0728idc.com/;
? ? ? fastcgi_pass? 127.0.0.1:9000;
? ? ? fastcgi_index? index.php;
? ? ? fastcgi_param? SCRIPT_FILENAME? $document_root$fastcgi_script_name;
? ? ? include? ? ? ? fastcgi_params;
? ? ? }
}
重啟ngnix 就可以通過游覽器使用https訪問了
/usr/local/nginx/sbin/nginx -s reload
部署denyhosts防止ssh暴力破解
DenyHosts是Python語言寫的一個(gè)程序,它會分析sshd的日志文件(/var/log/secure),當(dāng)發(fā)現(xiàn)重 復(fù)的攻擊時(shí)就會記錄IP到/etc/hosts.deny文件,從而達(dá)到自動屏IP的功能。
下載安裝包
wget http://"downloads.sourceforge.net/project/denyhosts/denyhosts/2.6/DenyHosts-2.6.tar.gz"
tar -xzf DenyHosts-2.6.tar.gz
cd DenyHosts-2.6
開始安裝
python setup.py install
DenyHosts默認(rèn)安裝到/usr/share/denyhosts目錄
配置
cd /usr/share/denyhosts/
cp denyhosts.cfg-dist denyhosts.cfg
vim denyhosts.cfg
PURGE_DENY = 1h #過多久后清除已阻止IP
HOSTS_DENY = /etc/hosts.deny #將阻止IP寫入到hosts.deny
BLOCK_SERVICE = sshd #阻止服務(wù)名
DENY_THRESHOLD_INVALID = 1 #允許無效用戶登錄失敗的次數(shù)
DENY_THRESHOLD_VALID = 10 #允許普通用戶登錄失敗的次數(shù)
DENY_THRESHOLD_ROOT = 5 #允許root登錄失敗的次數(shù)
WORK_DIR = /usr/share/denyhosts/data #將deny的host或ip紀(jì)錄到Work_dir中
DENY_THRESHOLD_RESTRICTED = 1 #設(shè)定 deny host 寫入到該資料夾
LOCK_FILE = /var/lock/subsys/denyhosts #將DenyHOts啟動的pid紀(jì)錄到LOCK_FILE中,已確保服務(wù)正確啟動,防止同時(shí)啟動多個(gè)服務(wù)。
HOSTNAME_LOOKUP=NO #是否做域名反解
ADMIN_EMAIL = #設(shè)置管理員郵件地址
DAEMON_LOG = /var/log/denyhosts #自己的日志文件
DAEMON_PURGE = 1h #該項(xiàng)與PURGE_DENY 設(shè)置成一樣,也是清除hosts.deniedssh 用戶的時(shí)間
設(shè)置啟動腳本使DenyHosts每次系統(tǒng)重起后自動啟動:
cp daemon-control-dist daemon-control
ln -s /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts
添加服務(wù)并設(shè)置開機(jī)啟動
chkconfig --add denyhosts
chkconfig denyhosts on
啟動denyhosts服務(wù)并查看狀態(tài)
service denyhosts start
service denyhosts status
查看屏蔽IP
cat /etc/hosts.deny