Centos 7.5 搭建mysql+nginx+php+redis+php+ssl+denyhosts
系统环境:CentOS Linux release 7.5.1804 (Core)
软件版本:Mysql 5.7.24
Nginx 1.14.0
PHP7.1.23扩展(redis,igbinary,inotify,swoole,fileinfo)
redis-5.0.0
SSL
denyhosts
一、安装mysql 5.7.24
安装YUM Repo,由于CentOS的yum源中没有mysql,需要到mysql的官网下载yum repo配置文件:
yum install wgetvim unzip zipgcc-c++–y
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
然后进行repo的安装:
rpm -ivh mysql57-community-release-el7-9.noarch.rpm
执行完成后会在/etc/yum.repos.d/目录下生成两个repo文件mysql-community.repomysql-community-source.repo
[图片上传失败...(image-f52fe9-1558444333806)]
开始安装MySQL使用yum命令即可完成安装
yum install mysql-server –y
启动msyql:
systemctl start mysqld
获取安装时的临时密码:
grep 'temporary password' /var/log/mysqld.log
用查到的密码登录:
mysql -u root -p
登录成功后修改密码:
set global validate_password_policy=0;
set password=password("52Wangdun");
配置默认编码为utf8:
vim /etc/my.cnf#添加
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
[图片上传失败...(image-f5de3f-1558444333806)]
附常用命令:
mysql_secure_installation #设置安全选项
systemctl stop mysqld #关闭MySQL
systemctl restart mysqld #重启MySQL
systemctl status mysqld#查看MySQL运行状态
systemctl enable mysqld#设置开机启动
systemctl disable mysqld#关闭开机启动
默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
二、安装nginx 1.14.0
编译安装前所需要的准备:安装GCC编译器 PCRE库zlib库OpenSSL库
yum install -y gccpcrepcre-develzlibzlib-developensslopenssl-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
访问网页
[图片上传失败...(image-6f2922-1558444333806)]
设置ngnix开机自启,即在rc.local增加启动代码就可以了
vim /etc/rc.local增加一行
/usr/local/nginx/sbin/nginx
[图片上传失败...(image-345775-1558444333806)]
设置执行权限:
chmod 755 /etc/rc.local
关闭SELINUX
vim /etc/selinux/config将SELINUX=enforcing改为SELINUX=disabled
[图片上传失败...(image-883791-1558444333804)]
附(Nginx部分控制命令):默认Nginx安装在/usr/local/nginx/中,因此
/usr/local/nginx/sbin/nginx #默认启动start
/usr/local/nginx/sbin/nginx -t #测试配置信息
/usr/local/nginx/sbin/nginx -s stop #快速停止服务
/usr/local/nginx/sbin/nginx -s quit #正常停止服务
/usr/local/nginx/sbin/nginx -s reload #重启
三、安装PHP7.1.23
编译安装前所需要的准备
yum -y install libxml2 libxml2-devel opensslopenssl-devel curl-devellibjpeg-devellibpng-develfreetype-devellibmcrypt-devellibxsltlibxslt-devel
安装过程中发现无法安装libmcrypt扩展需要安装第三方yum源
wget http://www.atomicorp.com/installers/atomic
sh ./atomic(默认回车就行)
安装所依赖的包
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
解压
tarzxvf php-7.1.23.tar.gz
进入目录
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环境变量, vim /etc/profile 在末尾加上
PATH=$PATH:/usr/local/php/bin
export PATH
[图片上传失败...(image-352c51-1558444333806)]
保存后使立即生效
source /etc/profile
查看PHP版本php -v
[图片上传失败...(image-d7d514-1558444333806)]
设置php开机自启,即在rc.local增加启动代码就可以了
vim /etc/rc.local增加一行
/usr/local/php/sbin/php-fpm[图片上传失败...(image-e1fc5a-1558444333804)]
设置执行权限:
chmod 755 /etc/rc.local
附(PHP部分控制命令)
/usr/local/php/sbin/php-fpm #启动PHP
killallphp-fpm #关闭PHP
四、配置nginx支持php-fpm
创建web用户,组都设置为www-data
groupadd www-data
useradd -g www-data www-data
复制一份php-fpm配置文件
cd /usr/local/php/etc
cpphp-fpm.conf.defaultphp-fpm.conf
打开配置文件
vimphp-fpm.conf
发现最后一行引入php-fpm.d目录下所有 .conf类型的配置文件
[图片上传失败...(image-25a677-1558444333806)]
打开php-fpm.d目录,复制默认配置文件并打开修改
cdphp-fpm.d
vim www.conf修改用户和用户组为www-data用户
[图片上传失败...(image-f87314-1558444333806)]
保存退出,启动php-fpm服务
/usr/local/php/sbin/php-fpm
php-fpm默认使用9000端口,使用如下命令查看启动状态
netstat -lnt | grep 9000
[图片上传失败...(image-1cdddd-1558444333806)]
nginx相关配置打开nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
修改用户组第一行注释去掉 nobody改为www-data
[图片上传失败...(image-a1b910-1558444333806)]
添加index.php,使nginx默认使用index.php为入口页
[图片上传失败...(image-cedff7-1558444333806)]
配置php-fpm模块,去掉注释并修改第69行的/scripts 为 $document_root
[图片上传失败...(image-57d07b-1558444333806)]
以上都修改后保存退出,先检测相关配置是否正确
/usr/local/nginx/sbin/nginx–t
[图片上传失败...(image-111c3-1558444333806)]
显示正确,重启nginx
/usr/local/nginx/sbin/nginx -s reload
切到nginx下的html目录(默认代码目录)新建index.php
[图片上传失败...(image-c58d99-1558444333806)]
<?php
phpinfo();
?>
测试访问对应地址是否可以运行
http://58.53.128.101/index.php
[图片上传失败...(image-348dfd-1558444333806)]
看到这界面说明php成功运行。
五、安装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
编译安装
yum install gcc
make MALLOC=libc
切换至redis的src目录,将目录下的文件复制到/usr/local/redis/bin/目录
mkdir -p /usr/local/redis/bin/
cd src
cp redis-sentinel redis-server redis-benchmark redis-check-aof redis-check-rdb redis-cli /usr/local/redis/bin/
make install
创建配置目录
mkdir -p /usr/local/redis/etc/
cp /root/redis-5.0.0/redis.conf /usr/local/redis/etc/redis.conf
修改配置文件vim /usr/local/redis/etc/redis.conf
daemonize yes
pidfile /var/run/redis_6379.pid
port 6379
loglevel notice
dir/usr/local/redis/
将redis的启动脚本复制一份放到/etc/init.d目录下
cp /root/redis-5.0.0/utils/redis_init_script /etc/init.d/redisd
修改启动脚本
vim /etc/init.d/redisd
[图片上传失败...(image-35ddf6-1558444333805)]
CONF="/usr/local/redis/etc/redis.conf"
设置开机启动
chkconfig redisd on
启动redisd
service redisd start
[图片上传失败...(image-8519b3-1558444333805)]
关闭redisd
[图片上传失败...(image-debd0a-1558444333805)]
查看redis目录结构
[图片上传失败...(image-2c8875-1558444333805)]
六、php7安装redis扩展
配置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
配置/usr/local/php/etc/php.ini 添加
extension=redis.so
重启php
Killall php-fpm
/usr/local/php/sbin/php-fpm
访问http://58.53.128.101/index.php,查找Redis
[图片上传失败...(image-f1c4bc-1558444333805)]
七、php7安装inotify扩展
pecl install inotify
配置/etc/php.ini 添加
extension=inotify.so
重启php
Killall php-fpm
/usr/local/php/sbin/php-fpm
访问http://58.53.128.101/index.php,查找inotify
[图片上传失败...(image-e24eed-1558444333805)]
八、php7安装igbinary扩展
pecl install igbinary
配置/etc/php.ini 添加
extension=igbinary.so
重启php
killallphp-fpm
/usr/local/php/sbin/php-fpm
访问http://58.53.128.101/index.php,查找igbinary
[图片上传失败...(image-dff7a1-1558444333805)]
九、php7安装swoole扩展
下载安装包
wget https://codeload.github.com/swoole/swoole-src/tar.gz/v4.0.2
tar -zxvf v4.0.2
cd swoole-src-4.0.2/
生成configure配置文件:
/usr/local/php/bin/phpize
编译安装
./configure --with-php-config=/usr/local/php/bin/php-config
make clean && make &&make install
配置/usr/local/php/etc/php.ini添加
extension= swoole.so
重启php
killallphp-fpm
/usr/local/php/sbin/php-fpm
访问http://58.53.128.101/index.php,查找swoole.so
[图片上传失败...(image-bd69f7-1558444333805)]
php7安装fileinfo扩展
cd /root/lnmp1.5/src/
tar -xjvf php-7.1.18.tar.bz2
cd php-7.1.18/ext/fileinfo/
/usr/local/php/bin/phpize
./configure -with-php-config=/usr/local/php/bin/php-config
make&& make install
vi /usr/local/php/etc/php.ini添加
extension= fileinfo.so
重启php
killallphp-fpm
/usr/local/php/sbin/php-fpm
访问http://58.53.128.101/index.php,查找fileinfo.so
[图片上传失败...(image-cafca8-1558444333805)]
十、在Nginx上配置多个站点
在Nginx配置目录下,创建一个”vhost”目录
mkdir /usr/local/nginx/conf/vhost
创建site b的配置文件
vim /usr/local/nginx/conf/vhost/rk.cakai.cn.conf
server {
listen 80;
server_name gm.cakai.cn;
root /app/gm.cakai.cn/;
index index.html index.htm index.php;
location / {
if (!-e $request_filename) {
rewrite (.*) /index.php;
}
}
location ~ .php$ {
root /app/gm.cakai.cn;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_param SCRIPT_FILENAME fastcgi_script_name;
includefastcgi_params;
}
}
创建site b的配置文件
vim /usr/local/nginx/conf/vhost/b.conf
server {
listen 80;
server_name gm.cakai.cn;
root /app/gm.cakai.cn/;
index index.html index.htm index.php;
location / {
if (!-e $request_filename) {
rewrite (.*) /index.php;
}
}
location ~ .php$ {
root /app/gm.cakai.cn;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_param SCRIPT_FILENAME fastcgi_script_name;
includefastcgi_params;
}
}打开nginx.conf文件
vim /usr/local/nginx/conf/nginx.conf
将虚拟目录的配置文件加入到”http {}”部分的末尾 ,注释server {}****内容
include /usr/local/nginx/conf/vhost/*.conf;
[图片上传失败...(image-e931e-1558444333804)]
重启Nginx服务
/usr/local/nginx/sbin/nginx -s reload
十、在Nginx配置SSL 证书使用https访问
防火墙增加443端口
firewall-cmd --zone=public --add-port=443/tcp --permanent
防火墙配置生效
firewall-cmd --reload
上传证书文件并修改站点配置文件
vim /usr/local/nginx/conf/vhost/gm.cakai.cn.conf
server {
listen 443;
server_name gm.cakai.cn;
root /app/gm.cakai.cn/;
index index.html index.htm index.php;
ssl on;
ssl_certificate /usr/local/nginx/conf/ssl/gm.cakai.cn.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/gm.cakai.cn.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/gm.cakai.cn/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_param SCRIPT_FILENAME fastcgi_script_name;
includefastcgi_params;
}
}
[图片上传失败...(image-4c195f-1558444333804)]
重启ngnix就可以通过游览器使用https访问了
/usr/local/nginx/sbin/nginx -s reload
十一、 部署denyhosts防止ssh暴力破解
DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP的功能。
下载安装包
wgethttp://"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默认安装到/usr/share/denyhosts目录
配置
cd /usr/share/denyhosts/
cpdenyhosts.cfg-distdenyhosts.cfg
vimdenyhosts.cfg
PURGE_DENY = 1h #过多久后清除已阻止IP
HOSTS_DENY = /etc/hosts.deny #将阻止IP写入到hosts.deny
BLOCK_SERVICE = sshd #阻止服务名
DENY_THRESHOLD_INVALID = 1 #允许无效用户登录失败的次数
DENY_THRESHOLD_VALID = 10 #允许普通用户登录失败的次数
DENY_THRESHOLD_ROOT = 5 #允许root登录失败的次数
WORK_DIR = /usr/share/denyhosts/data #将deny的host或ip纪录到Work_dir中
DENY_THRESHOLD_RESTRICTED = 1 #设定 deny host 写入到该资料夹
LOCK_FILE = /var/lock/subsys/denyhosts #将DenyHOts启动的pid纪录到LOCK_FILE中,已确保服务正确启动,防止同时启动多个服务。
HOSTNAME_LOOKUP=NO #是否做域名反解
ADMIN_EMAIL = #设置管理员邮件地址
DAEMON_LOG = /var/log/denyhosts #自己的日志文件
DAEMON_PURGE = 1h #该项与PURGE_DENY 设置成一样,也是清除hosts.deniedssh用户的时间
设置启动脚本使DenyHosts每次系统重起后自动启动:
cp daemon-control-dist daemon-control
ln -s /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts
添加服务并设置开机启动
chkconfig --add denyhosts
chkconfigdenyhosts on
启动denyhosts服务并查看状态
servicedenyhosts start
servicedenyhosts status
查看屏蔽IP
cat /etc/hosts.deny
[图片上传失败...(image-a328c6-1558444333804)]
��u�[������