五分钟自建 LNMP 架构 + Redis,你值得拥有!

一、搭建服务前准备工作

1、关掉 selinux
 # sed -i 's/^\<SELINUX\>=enforcing/SELINUX=disabled/' /etc/selinux/config 
 立即生效
 # setenforce 0
2、关闭防火墙
# iptables -F
# iptables -X
# systemctl stop firewalld
# systemctl disable firewalld
3、安装BBR;升级内核到最新版本/5.0.2 (看需求,可以不安装)
载入公钥和yum源
# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
# yum install -y yum-plugin-fastestmirror
# yum --enablerepo=elrepo-kernel install kernel-ml kernel-ml-devel -y
将kernel-ml 选为第一启动
# grub2-set-default 0
# reboot
重启后,通过 uname -a 查看内核是否切换到最新版
# echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
# echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
# sysctl -p    保存配置文件
开启BBR
# sysctl net.ipv4.tcp_available_congestion_control
# sysctl net.ipv4.tcp_congestion_control
查看是否开启了bbr
# lsmod | grep bbr
tcp_bbr                20480  6 

二、安装 LNMP 服务

1、安装 Nginx/1.16.0
# rpm -Uvh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.16.0-1.el7.ngx.x86_64.rpm
# yum install -y nginx
启动 nginx
# systemctl enable nginx
# nginx
2、安装 PHP/7.2.19 和 php-redis/3.1.6
# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
# yum -y install php72w-fpm
# echo 'export LC_ALL=C' > /etc/profile
# source /etc/profile
替换 apache 组和用户为 nginx
# sed -i 's/^\<user\> = apache*/user = nginx/' /etc/php-fpm.d/www.conf
# sed -i 's/^\<group\> = apache*/group = nginx/' /etc/php-fpm.d/www.conf
# yum -y install php72w-common php72w-cli php72w-opcache php72w-bcmath php72w-dba php72w-devel php72w-embedded php72w-enchant php72w-gd php72w-imap php72w-interbase php72w-intl php72w-ldap php72w-mbstring php72w-mcrypt php72w-mysql php72w-mysqlnd php72w-odbc php72w-pdo php72w-pdo_dblib php72w-pear php72w-pecl-apcu php72w-pecl-imagick php72w-pecl-memcached php72w-pecl-mongodb php72w-pecl-redis php72w-pecl-xdebug php72w-pgsql php72w-phpdbg php72w-process php72w-pspell php72w-recode php72w-snmp php72w-soap php72w-tidy php72w-xml php72w-xmlrpc --skip-broken
启动php
# systemctl enable php-fpm
# systemctl start php-fpm
3、安装 redis/5.0.5 (看需求,可以不安装)
# yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# yum -y --enablerepo=remi install redis
# systemctl start redis
# systemctl enable redis.service
4、安装 MySQL/5.7.27
# yum localinstall  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm -y
# yum install mysql-community-server -y
# yum install mysql-community-devel -y
启动数据库
# systemctl enable mysqld
# systemctl start mysqld

三、修改配置

1、修改数据库密码,授权用户连接登录
初始化前密码在此路径下
grep 'temporary password' /var/log/mysqld.log
修改 mysql root 密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
授权用户登录
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
2、修改 Nginx 配置(如下配置)
vim /etc/nginx/conf.d/www.conf 
server {
    listen          80;
    server_name localhost;
    index index.html index.htm index.php;
    root  /var/www/html/;
    #add_header 'Access-Control-Allow-Origin' '*';
    #add_header 'Access-Control-Allow-Headers' 'X-Requested-With,content-type,app_type,sign,did,time';
    #add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
    location ~ \.php($|/) {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_read_timeout 300;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include         fastcgi_params;
    }
    location / {
       # if ($request_method = 'OPTIONS') {
           #return 204;
       # }
       # add_header 'Access-Control-Allow-Origin' '*';                  
    #    add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';  
        try_files $uri $uri/ /index.php$is_args$query_string;
        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php?s=$1 last;
            break;
        }
    }
    access_log off;
}
重启 Nginx
nginx -s reload
3、修改 Redis 配置文件
vim /etc/redis.conf
......
bind 0.0.0.0        允许所有地址登录
loglevel verbose    设置日志类型
requirepass 123456  设置密码
......
重启redis
service redis restart
4、编写phpinfo测试是否环境是否正常
vim /var/www/html/index.html
    <?php
        phpinfo();
    ?>

访问本地IP测试,如果出现php的界面,就证明php环境没有问题

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章