CentOS 7 环境下脚本一键部署 LNMP

基础环境:

Centos7         # cat /etc/os-release
mysql  5.7.44   # mysql -v
nginx 1.26.1    #nginx -v
php 7.4.33      #php -v

将以下脚本保存为install_lnmp.sh并运行:


#!/bin/bash

# 更新系统
#yum update -y

# 安装依赖
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel epel-release

# 安装 MySQL 5.7
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
rpm -Uvh http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm
yum install -y mysql-community-server
systemctl start mysqld
systemctl enable mysqld
grep 'temporary password' /var/log/mysqld.log
mysql_secure_installation

# 安装 Nginx
yum install epel-release -y
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install -y nginx
systemctl start nginx
systemctl enable nginx

# 安装 PHP 7.4
rpm -ivh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php74
yum install php74-php php74-php-cli php74-php-fpm php74-php-mysqlnd php74-php-mbstring php74-php-xml php74-php-gd php74-php-opcache -y
yum install php74-php-curl php74-php-bcmath php74-php-intl php74-php-soap php74-php-json -y
ln -s /usr/bin/php74 /usr/bin/php
systemctl enable php74-php-fpm
systemctl start php74-php-fpm

# 配置 Nginx
echo "" > /usr/share/nginx/html/info.php
echo "location ~ \.php\$ {
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html\$fastcgi_script_name;
}" >> /etc/nginx/conf.d/default.conf
systemctl restart nginx

# 防火墙配置
#firewall-cmd --permanent --add-service=http
#firewall-cmd --permanent --add-service=https
#firewall-cmd --reload

echo "LNMP 安装完成!访问 http://<服务器IP>/info.php 查看 PHP 信息。"

注意修改nginx的配置文件:

vim /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

你可能感兴趣的:(centos,linux,运维)