硬核部署LNMP(可根据需求上生产)

涉及相关:nginx,mysql5.7,php-fpm

该文不适合零基础或部分需要快速搭建LNMP环境的同学阅读,适用于想要深入了解LNMP部署过程中可能遇到的以及想要有针对性的部署自己的架构的同学阅读。

该文仅代表个人观点,欢迎评论区讨论。

为使文章看起来简洁且通俗易懂,部分正常无意义输出将省去。

 

基础环境配置

[root@test ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

[root@test ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
[root@test ~]# getenforce
Permissive
[root@test ~]# firewall-cmd --state
running
[root@test ~]# sed -i.default 's|[=enforcing]\+$|=permissive|g' /etc/selinux/config  #永久关闭selinux,如果selinux配置文件已改为permissive/disable则无需执行此步

编译前环境搭建

由于CentOS的特性,基础仓库已经EPEL提供的php均为5.x版本。要使用高版本的php主要有两种途径,一是使用第三方镜像源(webtatic/ius/remi)提供的高版本rpm包进行安装,二则是使用官方提供的源码进行编译安装。
mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz  MySQL 5.7.30二进制包
php-7.4.16.tar.gz                                           php 7.4.16源码包
 

[root@test ~]# ll
total 660760
-rw-------. 1 root root      1672 Aug  8  2020 anaconda-ks.cfg
-rw-r--r--. 1 root root 660017902 Apr 10 18:02 mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
-rw-r--r--. 1 root root  16588926 Apr 10 17:56 php-7.4.16.tar.gz
[root@test ~]# yum install epel-release -y
[root@test ~]# yum clean all
Cleaning repos: epel extras os updates
[root@test ~]# yum repolist
repo id                             repo name                                                         status
epel/x86_64                         Extra Packages for Enterprise Linux 7 - x86_64                    13,575
extras/7/x86_64                     Qcloud centos extras - x86_64                                        468
os/7/x86_64                         Qcloud centos os - x86_64                                         10,072
updates/7/x86_64                    Qcloud centos updates - x86_64                                     1,901
repolist: 26,016

nginx

该web服务不管是使用源码编译还是使用官方的yum仓库安装,跟其它几个服务比起来都可以说是相对简单,而且使用官方yum仓库安装目录结构清晰,所以这里使用yum安装。

这里不建议使用CentOS编译的或红帽编译的rh-nginx116包,因为目录结构太过于复杂且初始配置文件也复杂。

[root@test ~]# cat > /etc/yum.repos.d/nginx.repo <<EOF
>
> [nginx-stable]
> name=nginx stable repo
> baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
> gpgcheck=1
> enabled=1
> gpgkey=https://nginx.org/keys/nginx_signing.key
> module_hotfixes=true
>
> [nginx-mainline]
> name=nginx mainline repo
> baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
> gpgcheck=1
> enabled=0
> gpgkey=https://nginx.org/keys/nginx_signing.key
> module_hotfixes=true
> EOF
[root@test ~]#
[root@test ~]# yum install nginx -y
[root@test ~]# firewall-cmd --add-service=http --permanent
success
[root@test ~]# firewall-cmd --reload
success
[root@test ~]# cp /etc/nginx/conf.d/default.conf{,.default}
[root@test ~]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;
    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;

    location / {
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
[root@test ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@test ~]# systemctl start nginx

mysql

[root@test ~]# useradd mysql -U -M -s /sbin/nologin
[root@test ~]# yum  install libaio-devel -y
[root@test ~]# mkdir /app/mysql-5.7.30 /data/mysql/data /data/mysql/logs/binlog -p
[root@test ~]# cat > /etc/my.cnf <<EOF
> [mysqld]
> socket=/tmp/mysql.sock
> datadir=/data/mysql/data
> basedir=/app/mysql-5.7.30/
> server_id=6
> port=3306
> #secure_file_priv=''
> autocommit=1
> log_error=/data/mysql/logs/log.err
> log_bin=/data/mysql/logs/binlog/mysql-bin
> binlog_format=row
> slow_query_log=1
> slow_query_log_file=/data/mysql/logs/log.slow
> long_query_time=0.1
> log_queries_not_using_indexes
> gtid-mode=on
> enforce-gtid-consistency=true
> [mysql]
> socket=/tmp/mysql.sock
> port=3306
> EOF

[root@test ~]# touch /data/mysql/logs/log.{err,slow}
[root@test ~]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /app/
[root@test ~]# cd /app/mysql-5.7.30
[root@test mysql-5.7.30]# mv ../mysql-5.7.30-linux-glibc2.12-x86_64/* .
[root@test mysql-5.7.30]# rm -rf ../mysql-5.7.30-linux-glibc2.12-x86_64/
[root@test mysql-5.7.30]# cd bin/
[root@test bin]# chown -R mysql. /app/mysql-5.7.30/ /data/mysql/ /etc/my.cnf
[root@test bin]# ./mysqld --initialize-insecure --user=mysql --basedir=/app/mysql-5.7.30 --datadir=/data/mysql/data
[root@test bin]# cat > /lib/systemd/system/mysqld.service <<EOF
> [Unit]
> Description=MySQL Server
> Documentation=man:mysqld(8)
> Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
> After=network.target
> After=syslog.target
> [Install]
> WantedBy=multi-user.target
> [Service]
> User=mysql
> Group=mysql
> ExecStart=/app/mysql-5.7.30/bin/mysqld --defaults-file=/etc/my.cnf
> LimitNOFILE = 5000
> EOF
[root@test bin]# systemctl daemon-reload
[root@test bin]# systemctl start mysqld
[root@test bin]# ss -ntlp
State      Recv-Q Send-Q         Local Address:Port                        Peer Address:Port
LISTEN     0      128                        *:22                                     *:*                   users:(("sshd",pid=6876,fd=3))
LISTEN     0      80                        :::3306                                  :::*                   users:(("mysqld",pid=13796,fd=23))
LISTEN     0      128                       :::22                                    :::*                   users:(("sshd",pid=6876,fd=4))
[root@test bin]# ./mysqladmin -u root -p password 123.com  # 此处的 123.com 是你要设置的新密码
Enter password:    # 因为之前初始化时使用的是空密码,所以这里直接回车就行
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@test bin]# cd
[root@test ~]# for i in $(ls /app/mysql-5.7.30/bin/)
> do
> ln -s /app/mysql-5.7.30/bin/$i /usr/bin/$i
> done
[root@test ~]# mysql -uroot -p123.com
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> ^DBye  # Ctrl+D

php

php涉及的编译选项和依赖根据需求来各不相同,我这里以wordpress为例编译安装一个php

[root@test ~]# yum install gcc make autoconf automake libtool libxml2 libxml2-devel sqlite-devel openssl-devel libcurl-devel libxslt-devel -y
[root@test ~]# ls
anaconda-ks.cfg  mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz  php-7.4.16.tar.gz
[root@test ~]# tar xf php-7.4.16.tar.gz
[root@test ~]# cd php-7.4.16/
[root@test php-7.4.16]# ./configure --enable-fpm --with-pear \
> --with-curl \
> --with-iconv \
> --with-mhash \
> --with-zlib \
> --with-xmlrpc \
> --with-xsl \
> --with-openssl \
> --with-mysqli=mysqlnd \
> --with-pdo-mysql=mysqlnd \
> --disable-debug \
> --enable-mysqlnd \
> --enable-sockets \
> --enable-soap \
> --enable-inline-optimization \
> --enable-xml \
> --enable-ftp \
> --enable-exif \
> --enable-bcmath \
> --enable-calendar \
> --enable-shmop \
> --enable-dba \
> --enable-sysvsem \
> --enable-sysvshm \
> --enable-sysvmsg
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
···
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

[root@test php-7.4.16]# make && make install
···
[PEAR] PEAR           - installed: 1.10.12
Wrote PEAR system config file at: /usr/local/etc/pear.conf
You may want to add: /usr/local/lib/php to your php.ini include_path
/root/php-7.4.16/build/shtool install -c ext/phar/phar.phar /usr/local/bin/phar.phar
ln -s -f phar.phar /usr/local/bin/phar
Installing PDO headers:           /usr/local/include/php/ext/pdo/

[root@test php-7.4.16]# cp php.ini-development /usr/local/php/php.ini
[root@test php-7.4.16]# cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
[root@test php-7.4.16]# cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
[root@test php-7.4.16]# sed -i '24a error_log = /var/log/php/php-fpm.log' /usr/local/etc/php-fpm.conf
[root@test php-7.4.16]# mkdir /var/log/php/ -p
[root@test php-7.4.16]# touch /var/log/php/php-fpm.log
[root@test php-7.4.16]# sed -i 's|include=NONE/|include=|g' /usr/local/etc/php-fpm.conf
[root@test php-7.4.16]# sed -e  "s|user = nobody|user = nginx|g" -e "s|group = nobody|group = nginx|g" -i /usr/local/etc/php-fpm.d/www.conf
[root@test php-7.4.16]# cat >/usr/share/nginx/html/index.php << EOF
> <?php phpinfo(); ?>
> EOF
[root@test php-7.4.16]# cp sapi/fpm/php-fpm.service /lib/systemd/system/ -a
[root@test php-7.4.16]# systemctl start php-fpm.service
[root@test php-7.4.16]# ss -ntlp
State      Recv-Q Send-Q         Local Address:Port                        Peer Address:Port
LISTEN     0      128                127.0.0.1:9000                                   *:*                   users:(("php-fpm",pid=13838,fd=8),("php-fpm",pid=13837,fd=8),("php-fpm",pid=13836,fd=6))
LISTEN     0      128                        *:22                                     *:*                   users:(("sshd",pid=6852,fd=3))
LISTEN     0      128                        *:80                                     *:*                   users:(("nginx",pid=16056,fd=6),("nginx",pid=16055,fd=6))
LISTEN     0      100                127.0.0.1:25                                     *:*                   users:(("master",pid=6952,fd=13))
LISTEN     0      80                        :::3306                                  :::*                   users:(("mysqld",pid=7567,fd=34))
LISTEN     0      128                       :::22                                    :::*                   users:(("sshd",pid=6852,fd=4))
LISTEN     0      100                      ::1:25                                    :::*                   users:(("master",pid=6952,fd=14))

安装完成

浏览器访问ip得到以下网页则部署成功


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值