五分鐘自建 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環境沒有問題

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