LNMP架构的部署
LNMP架构的部署
实验环境
系统 | 主机 | IP |
---|---|---|
centos8 | nginx | 192.168.169.139 |
centos8 | mysql | 192.168.169.142 |
centos8 | php-fpm | 192.168.169.145 |
前置环境准备
//配置网络仓库 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo dnf clean all dnf list all //关闭防火墙和selinux systemctl disable --now firewalld.service sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config reboot
1. 安装nginx
在nginx主机上完成
//下载nginx源码包 [root@nginx ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz //创建nginx用户 [root@nginx ~]# useradd -rMs /sbin/nologin nginx //下载编译环境和依赖包 [root@nginx ~]# dnf -y install gcc gcc-c++ make pcre-devel openssl openssl-devel gd-devel //解压nginx源码包 [root@nginx ~]# tar xf nginx-1.22.0.tar.gz [root@nginx ~]# cd nginx-1.22.0/ [root@nginx nginx-1.22.0]# ./configure --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-debug \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_image_filter_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_stub_status_module [root@nginx nginx-1.22.0]# make && make install //配置全局环境变量 [root@nginx ~]# echo 'export PATH=$PATH:/usr/local/nginx/sbin' > /etc/profile.d/nginx.sh [root@nginx ~]# source /etc/profile.d/nginx.sh //使用systemd风格管理nginx [root@nginx nginx]# vim /usr/lib/systemd/system/nginx.service [Unit] Description=Nginx server daemon After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecStop=/usr/local/nginx/sbin/nginx -s stop ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target [root@nginx nginx]# systemctl daemon-reload [root@nginx nginx]# systemctl start nginx.service [root@nginx nginx]# systemctl enable nginx.service Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service. [root@nginx nginx]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
2. 安装mysql
在mysql主机上完成
//下载mysql安装包 [root@mysql ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz //安装ncurses-compat-libs [root@mysql ~]# dnf -y install ncurses-compat-libs //创建mysql用户 [root@mysql ~]# useradd -rMs /sbin/nologin mysql //创建mysql数据存放目录 [root@mysql ~]# mkdir -p /opt/data [root@mysql ~]# chown -R mysql.mysql /opt/data/ //解压mysql [root@mysql ~]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@mysql local]# ln -s mysql-5.7.38-linux-glibc2.12-x86_64 mysql [root@mysql local]# chown -R mysql.mysql mysql-5.7.38-linux-glibc2.12-x86_64 [root@mysql local]# chown -R mysql.mysql mysql [root@mysql local]# cd mysql [root@mysql mysql]# ls LICENSE README bin docs include lib man share support-files //配置环境变量 [root@mysql mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh [root@mysql mysql]# source /etc/profile.d/mysql.sh //配置头文件 [root@mysql mysql]# ln -s /usr/local/mysql/include /usr/include/mysql [root@mysql mysql]# ll /usr/include/mysql lrwxrwxrwx. 1 root root 24 Sep 7 15:21 /usr/include/mysql -> /usr/local/mysql/include //配置库文件 [root@mysql mysql]# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf [root@mysql mysql]# ldconfig //配置mysql帮助文档 [root@mysql mysql]# sed -i '22a MANDATORY_MANPATH /usr/local/mysql/man' /etc/man_db.conf //初始化mysql并将密码保存至/tmp/mysql.pass文件下 [root@mysql mysql]# /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir /opt/data/ | grep 'password' | awk -F' ' '{print $NF}' > /tmp/pass //生成mysql配置文件 [root@mysql mysql]# cat > /etc/my.cnf <<EOF [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid user = mysql skip-name-resolve EOF [root@mysql mysql]# cat /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid user = mysql skip-name-resolve //使用systemd风格管理mysql [root@mysql ~]# vim /usr/lib/systemd/system/mysqld.service [Unit] Description=MySQL server daemon After=network.target [Service] Type=forking ExecStart=/usr/local/mysql/support-files/mysql.server start ExecStop=/usr/local/mysql/support-files/mysql.server stop ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target [root@mysql ~]# systemctl daemon-reload [root@mysql ~]# systemctl start mysqld.service [root@mysql ~]# systemctl enable mysqld.service Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service. [root@mysql ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 80 *:3306 *:* LISTEN 0 128 [::]:22 [::]:* //登录数据库 [root@mysql ~]# mysql -uroot -p'hid/qgUxj6B)' //hid/qgUxj6B)是我的数据库初始密码 //初始化数据库密码 mysql> set password = password('123456'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> exit Bye [root@mysql ~]# //使用更改后的密码进行登录 [root@mysql ~]# mysql -uroot -p'123456'
3. 安装php
在php-fpm主机上完成
//下载php源码包 [root@php-fpm ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz //安装epel源 [root@php-fpm ~]# dnf -y install epel-release //安装依赖包 [root@php-fpm ~]# dnf -y install gcc gcc-c++ make libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm libzip-devel //解压php [root@php-fpm ~]# tar xf php-8.1.11.tar.gz [root@php-fpm ~]# cd php-8.1.11/ [root@php-fpm php-8.1.11]# ./configure --prefix=/usr/local/php8 \ --with-config-file-path=/etc \ --enable-fpm \ --disable-debug \ --disable-rpath \ --enable-shared \ --enable-soap \ --with-openssl \ --enable-bcmath \ --with-iconv \ --with-bz2 \ --enable-calendar \ --with-curl \ --enable-exif \ --enable-ftp \ --enable-gd \ --with-jpeg \ --with-zlib-dir \ --with-freetype \ --with-gettext \ --enable-mbstring \ --enable-pdo \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-readline \ --enable-shmop \ --enable-simplexml \ --enable-sockets \ --with-zip \ --enable-mysqlnd-compression-support \ --with-pear \ --enable-pcntl \ --enable-posix ………… +--------------------------------------------------------------------+ | 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@php-fpm php-8.1.11]# make && make install [root@php-fpm php-8.1.11]# cd /usr/local/php8/ [root@php-fpm php8]# ls bin etc include lib php sbin var //配置环境变量 [root@php-fpm php8]# echo 'export PATH=$PATH:/usr/local/php8/bin' > /etc/profile.d/php8.sh [root@php-fpm php8]# source /etc/profile.d/php-fpm.sh //配置头文件 [root@php-fpm php8]# ln -s /usr/local/php8/include /usr/include/php8 [root@php-fpm php8]# ll /usr/include/php8 lrwxrwxrwx 1 root root 23 Oct 11 05:34 /usr/include/php8 -> /usr/local/php8/include //配置库文件 [root@php-fpm php8]# echo "/usr/local/php8/lib" > /etc/ld.so.conf.d/php8.conf [root@php-fpm php8]# ldconfig //配置php-fpm [root@php-fpm php8]# cd ~/php-8.1.11/ [root@php-fpm php-8.1.11]# cp php.ini-production /etc/php.ini cp: overwrite '/etc/php.ini'? y [root@php-fpm php-8.1.11]# cp ~/php-8.1.11/sapi/fpm/init.d.php-fpm /usr/local/php8/bin/php-fpm [root@php-fpm php-8.1.11]# chmod +x /usr/local/php8/bin/php-fpm [root@php-fpm php-8.1.11]# cd /usr/local/php8/etc/ [root@php-fpm etc]# cp php-fpm.conf.default php-fpm.conf [root@php-fpm etc]# cd php-fpm.d/ [root@php-fpm php-fpm.d]# cp www.conf.default www.conf //使用systemd风格管理php-fpm [root@php-fpm ~]# vim /usr/lib/systemd/system/php-fpm.service [Unit] Description=php-fpm server daemon After=network.target [Service] Type=forking ExecStart=/usr/local/php8/bin/php-fpm start ExecStop=/usr/local/php8/bin/php-fpm stop ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target [root@php-fpm ~]# systemctl daemon-reload [root@php-fpm ~]# systemctl start php-fpm.service [root@php-fpm ~]# systemctl enable php-fpm.service Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service. [root@php-fpm ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 127.0.0.1:9000 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
4. 配置nginx
在nginx主机上完成
[root@nginx ~]# cd /usr/local/nginx/ [root@nginx nginx]# cp conf/nginx.conf conf/nginx.conf.bak [root@nginx nginx]# vim conf/nginx.conf location / { root html; index index.html index.htm index.php; //加上index.php } //取消如下内容的注释 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; } //进行修改 location ~ \.php$ { root html; fastcgi_pass 192.168.169.145:9000; //监听php端的9000端口 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; //编写php测试页面 [root@nginx nginx]# cat > /usr/local/nginx/html/index.php <<EOF > <?php > phpinfo(); > ?> > EOF //重启nginx [root@nginx nginx]# systemctl restart nginx.service
在php-fpm主机上完成
[root@php-fpm ~]# cd /usr/local/php8/etc/php-fpm.d/ [root@php-fpm php-fpm.d]# vim www.conf listen = 192.168.169.145:9000 listen.allowed_clients = 192.168.169.139 //编写php测试页面 [root@php-fpm php-fpm.d]# cat > /usr/local/nginx/html/index.php <<EOF > <?php > phpinfo(); > ?> > EOF //重启php-fpm [root@php-fpm php-fpm.d]# systemctl restart php-fpm.service
在浏览器访问nginx端的80端口下的index.php