• 欢迎访问VPS岛网站,国外VPS,国内VPS,国外服务器,国内服务器,服务器主机,测评及优惠码,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站 QQ群

Centos 7.5 搭建mysql+nginx+php+redis+php+ssl+denyhos

Nginx相关 毕威2010 200次浏览 已收录 0个评论

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 扩展(redisigbinaryinotifyswoole

redis-5.0.0

SSL

denyhosts

一、安装 mysql 5.7.24

安装YUM Repo,由于CentOS yum源中没有mysql,需要到mysql的官网下载yum repo配置文件:

yum install wget vim unzip zip gcc-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.repo mysql-community-source.repo

image.png

开始安装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.png

附常用命令:

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 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

                   访问网页

image.png

设置ngnix开机自启,即在rc.local增加启动代码就可以了

vim /etc/rc.local  增加一行

/usr/local/nginx/sbin/nginx

image.png

设置执行权限:

chmod 755 /etc/rc.local

                   关闭SELINUX 

vim /etc/selinux/config SELINUX=enforcing改为SELINUX=disabled

                            image.png

附(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 openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libxslt libxslt-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 

解压

tar zxvf 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.png

保存后使立即生效

source /etc/profile

查看PHP版本 php -v

image.png

设置php开机自启,即在rc.local增加启动代码就可以了

vim /etc/rc.local  增加一行

/usr/local/php/sbin/php-fpmimage.png

设置执行权限:

chmod 755 /etc/rc.local

附(PHP部分控制命令)

/usr/local/php/sbin/php-fpm  #启动PHP

killall php-fpm                             #关闭PHP

 

四、配置nginx支持php-fpm

创建web用户,组都设置为www-data

groupadd www-data

useradd -g www-data www-data

复制一份php-fpm配置文件

cd /usr/local/php/etc

cp php-fpm.conf.default php-fpm.conf

打开配置文件

vim php-fpm.conf

发现最后一行引入php-fpm.d目录下所有 .conf 类型的配置文件

image.png

打开 php-fpm.d目录,复制默认配置文件并打开修改

cd php-fpm.d

cp www.conf.default  www.conf

vim www.conf  修改用户和用户组为www-data用户

image.png

保存退出,启动php-fpm服务

/usr/local/php/sbin/php-fpm

php-fpm默认使用9000端口,使用如下命令查看启动状态

netstat -lnt | grep 9000

image.png

nginx相关配置打开nginx配置文件

vim /usr/local/nginx/conf/nginx.conf

修改用户组第一行注释去掉 nobody改为www-data

image.png

添加index.php,使nginx默认使用index.php为入口页

image.png

配置php-fpm模块,修改第69行的/scripts $document_root

image.png

以上都修改后保存退出,先检测相关配置是否正确

/usr/local/nginx/sbin/nginx –t

image.png

显示正确,重启nginx

/usr/local/nginx/sbin/nginx -s reload

切到nginx下的html目录(默认代码目录)新建index.php

image.png

<?php

phpinfo();

?>

测试访问对应地址是否可以运行

http://58.53.128.101/index.php

image.png

看到这界面说明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

编译安装

make

切换至redissrc目录

cd src

cp redis-server redis-cli /usr/local/bin

cp redis-sentinel redis-benchmark redis-check-aof redis-check-dump /usr/local/bin

创建配置目录

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

创建服务

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   内容如下

[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

image.png

启动服务

systemctl enable redis

systemctl daemon-reload

systemctl start redis

systemctl status redis

image.png

 

六、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

配置/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.png

 

七、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.png

 

八、php7安装igbinary扩展

pecl install igbinary

配置/etc/php.ini 添加

extension=igbinary.so

重启php

killall php-fpm

/usr/local/php/sbin/php-fpm

访问http://58.53.128.101/index.php,查找igbinary

image.png

 

九、php7安装swoole扩展

下载安装包

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 添加

extension= swoole.so

重启php

killall php-fpm

/usr/local/php/sbin/php-fpm

访问http://58.53.128.101/index.php,查找swoole.so

image.png

 

十、在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_index  index.php;

       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

       include        fastcgi_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_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 {}内容

include /usr/local/nginx/conf/vhost/*.conf;

image.png

         重启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_index  index.php;

       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

       include        fastcgi_params;

      }

 

}

image.png

重启ngnix 就可以通过游览器使用https访问了

         /usr/local/nginx/sbin/nginx -s reload

 

十一、       部署denyhosts防止ssh暴力破解

DenyHostsPython语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重 复的***时就会记录IP/etc/hosts.deny文件,从而达到自动屏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默认安装到/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 #阻止服务名

  DENY_THRESHOLD_INVALID = 1 #允许无效用户登录失败的次数

  DENY_THRESHOLD_VALID = 10 #允许普通用户登录失败的次数

  DENY_THRESHOLD_ROOT = 5 #允许root登录失败的次数

  WORK_DIR = /usr/share/denyhosts/data #denyhostip纪录到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

chkconfig denyhosts on

                   启动denyhosts服务并查看状态

service denyhosts start

service denyhosts status

查看屏蔽IP

cat /etc/hosts.deny

image.png


VPS岛 的文章和资源来自互联网,仅作为参考资料,如果有侵犯版权的资源请尽快联系站长,我们会在24h内删除有争议的资源。丨 转载请注明Centos 7.5 搭建mysql+nginx+php+redis+php+ssl+denyhos
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址