lnmp部署
环境说明
系统平台 | IP | 需要安装的服务 |
---|---|---|
centos8 | 192.168.91.129 | nginx(1.20.2) |
centos8 | 192.168.91.139 | mysql(5.7.38) |
centos8 | 192.168.91.145 | php(8.1.11) |
nginx安装与配置
//关闭防火墙,selinux子系统
[root@nginx ~]# systemctl stop firewalld.service
[root@nginx ~]# systemctl disable firewalld.service
[root@nginx ~]# sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
[root@nginx ~]# setenforce 0
//创建系统用户nginx
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
[root@nginx ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[root@nginx ~]# cd /usr/local/src/
[root@nginx src]# wget https://nginx.org/download/nginx-1.20.2.tar.gz
//编译安装
[root@nginx src]# ls
nginx-1.20.2.tar.gz
[root@nginx src]# tar -xf nginx-1.20.2.tar.gz
[root@nginx src]# cd nginx-1.20.2/
[root@nginx nginx-1.20.2]# ./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 \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@nginx nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//nginx安装后配置
//配置环境变量
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# . /etc/profile.d/nginx.sh
//设置nginx 开机自启
[root@nginx ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginx.service
[root@nginx ~]# vim /usr/lib/systemd/system/nginx.service
[root@nginx ~]# cat /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 ~]# systemctl daemon-reload
[root@nginx ~]# systemctl enable --now nginx.service
[root@nginx ~]# ss -anlt
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
安装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
//创建用户和组
[root@mysql ~]# groupadd mysql
[root@mysql ~]# useradd -M -s /sbin/nologin -g mysql mysql
解压软件至/usr/local/
[root@mysql ~]# tar -xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql ~]# mv /usr/local/mysql-5.7.38-linux-glibc2.12-x86_64 mysql
[root@mysql ~]# cd /usr/local/
修改目录/usr/local/mysql的属主属组
[root@mysql local]# chown -R mysql.mysql /usr/local/mysql
[root@mysql local]# ll mysql/
total 276
drwxr-xr-x. 2 mysql mysql 4096 Oct 11 14:00 bin
drwxr-xr-x. 2 mysql mysql 55 Oct 11 14:00 docs
drwxr-xr-x. 3 mysql mysql 4096 Oct 11 14:00 include
drwxr-xr-x. 5 mysql mysql 4096 Oct 11 14:00 lib
-rw-r--r--. 1 mysql mysql 259251 Mar 22 2022 LICENSE
drwxr-xr-x. 4 mysql mysql 30 Oct 11 14:00 man
-rw-r--r--. 1 mysql mysql 566 Mar 22 2022 README
drwxr-xr-x. 28 mysql mysql 4096 Oct 11 14:00 share
drwxr-xr-x. 2 mysql mysql 90 Oct 11 14:00 support-files
//添加环境变量
[root@mysql ~]# echo 'export PATH=$PATH:/usr/local/mysql/bin/' > /etc/profile.d/mysql.sh
[root@mysql ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin/
//设置头文件到系统里面,系统默认的头文件在/usr/include
[root@mysql ~]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
'/usr/include/mysql' -> '/usr/local/mysql/include/'
//添加lib
[root@mysql ~]# vim /etc/ld.so.conf.d/mysql-x86_64.conf
[root@mysql ~]# cat /etc/ld.so.conf.d/mysql-x86_64.conf
/usr/local/mysql/lib
//man文档
[root@mysql ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man 加上mysql,man文档的绝对路径
//建立数据存放目录
[root@mysql ~]# vim /etc/man_db.conf
[root@mysql ~]# mkdir -p /opt/data
[root@mysql ~]# chown -R mysql.mysql /opt/data
[root@mysql ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Oct 11 14:08 data
[root@mysql ~]# /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir /opt/data/
2022-10-11T06:09:13.630451Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-11T06:09:13.868698Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-11T06:09:13.910851Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-11T06:09:13.935893Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 3aec9827-492b-11ed-b795-000c2933c1e3.
2022-10-11T06:09:13.936577Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-11T06:09:14.095306Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T06:09:14.095354Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T06:09:14.095714Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T06:09:14.136428Z 1 [Note] A temporary password is generated for root@localhost: !zjuWrK8jKg%
//启动服务
[root@mysql ~]# cd /usr/lib/systemd/system
[root@mysql system]# cp sshd.service mysql.service
[root@mysql system]# vim mysql.service
[root@mysql system]# cat mysql.service
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.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 system]# systemctl daemon-reload
[root@mysql system]# systemctl start mysql.service
[root@mysql system]# ss -anlt
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 system]# yum -y install ncurses-compat-libs
[root@mysql system]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.38
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
安装php
[root@php ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
[root@php ~]# yum -y install 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 readline readline-devel libxslt libxslt-devel php-mysqlnd libxml2-devel sqlite-devel https://vault.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm https://vault.centos.org/centos/8/AppStream/x86_64/os/Packages/libzip-devel-1.5.1-2.module_el8.2.0+313+b04d0a66.x86_64.rpm --nobest
//下载php
[root@php ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
//解压php
[root@php ~]# tar -xf php-8.1.11.tar.gz -C /usr/local/
[root@php ~]# tar -xf php-8.1.11.tar.gz -C /usr/local/
[root@php ~]# cd /usr/local/php-8.1.11/
[root@php php-8.1.11]# ./configure --prefix=/usr/local/php8 --with-config-file-path=/etc --enable-fpm --enable-inline-optimization --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-json --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
[root@php php-8.1.11]# make && make install
//设置环境变量
[root@php php-8.1.11]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@php php-8.1.11]# . /etc/profile.d/php8.sh
//配置php-fpm
[root@php php-8.1.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php php-8.1.11]# chmod +x /etc/rc.d/init.d/php-fpm
[root@php php-8.1.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@php php-8.1.11]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
//启动php-fpm
[root@php php-8.1.11]# service php-fpm start
Starting php-fpm done
[root@php php-8.1.11]# ss -anlt
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 [::]:*
部署完成之后连接nginx和php
//在php上创建虚拟主机目录并生成php测试页面
[root@php ~]# mkdir /www
[root@php ~]# cd /www/
[root@php www]# vim index.php
[root@php www]# cat index.php
<?php
phpinfo();
?>
//修改nginx.conf配置文件
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm; //添加index.php
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /www; //指向php里面创建的index.php文件
fastcgi_pass 192.168.91.145:9000; //php服务器的ip
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@nginx ~]# systemctl restart nginx.service
//修改php的配置文件
[root@php ~]# vim /usr/local/php8/etc/php-fpm.d/www.conf
listen = 192.168.91.145:9000 ##注:监听的IP地址改为服务器的IP地址,端口为9000
listen.allowed_clients = 192.168.91.129 ##注:允许192.168.91.129这台服务器访问
[root@php ~]# service php-fpm restart
访问测试: