lnmp


部署lnmp

环境说明

系统 IP 服务
centos8 192.168.26.10 nginx
centos8 192.168.26.11 mysql
centos8 192.168.26.12 php

安装nginx

//创建系统用户nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx

//安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget

//关闭防火墙和selinux
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@localhost ~]# systemctl disable --now firewalld.service

//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx

//下载nginx源码包,并解压安装
[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@localhost ~]# tar xf nginx-1.20.2.tar.gz 
[root@localhost ~]# cd nginx-1.20.2
[root@localhost 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@localhost nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

//安装成功
[root@localhost nginx-1.20.2]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf  html  logs  sbin

//配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# source /etc/profile.d/nginx.sh 
[root@localhost ~]# which nginx
/usr/local/nginx/sbin/nginx

//启动nginx
[root@localhost ~]# nginx
[root@localhost ~]# 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               [::]:*     

//关闭服务
[root@localhost ~]# nginx -s stop
[root@localhost ~]# 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                [::]:22               [::]:*       

//编写service文件
[root@localhost ~]# vi /usr/lib/systemd/system/nginx.service
[root@localhost ~]# 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@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl enable --now nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@localhost ~]# 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:*     

安装mysql

//安装依赖包
[root@mysql ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs

//创建用户和组
[root@mysql ~]#  useradd -r -M -s /sbin/nologin 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

//解压软件至/usr/local/
[root@mysql ~]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql ~]# cd /usr/local/
[root@mysql local]# ls
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.38-linux-glibc2.12-x86_64  share
[root@mysql local]# mv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
[root@mysql local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src

//修改目录/usr/local/mysql的属主属组
[root@mysql local]# chown -R mysql.mysql mysql

//配置mysql
[root@mysql local]# ls /usr/local/mysql/
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@mysql local]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@mysql local]# echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf
[root@mysql local]# vim /etc/man_db.conf 
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/mysql/man   //添加这行

//添加环境变量
[root@mysql local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql local]# source /etc/profile.d/mysql.sh 
[root@mysql local]#  which mysql
/usr/local/mysql/bin/mysql


//建立数据存放目录
[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 12 00:32 data

//初始化数据库
[root@mysql ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2022-10-11T16:33:09.816724Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T16:33:09.845921Z 1 [Note] A temporary password is generated for root@localhost: p(f6SawqfqB?
[root@mysql ~]# echo 'p(f6SawqfqB?' > pass
[root@mysql ~]# cat pass 
p(f6SawqfqB?

//卸载mariadb的所有包
[root@mysql ~]# dnf -y remove mariadb*

//生成配置文件
[root@mysql ~]# vi /etc/my.cnf
[root@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

//配置服务启动脚本
[root@mysql ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql ~]# vi /etc/init.d/mysqld
basedir=/usr/local/mysql    
datadir=/opt/data
[root@mysql ~]# chmod +x /etc/init.d/mysqld 

//启动mysql 并设置开机自启
[root@mysql ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/mysql.err'.
 SUCCESS! 
[root@mysql ~]# 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                [::]:22               [::]:*              
LISTEN  0       80                    *:3306                *:*    

[root@mysql ~]# chkconfig --add mysqld
[root@mysql ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

//修改密码
//使用临时密码登录
[root@mysql ~]# cat pass 
p(f6SawqfqB?
[root@mysql ~]# mysql -uroot -p'p(f6SawqfqB?'
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 2
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)

安装php

//下载php
[root@php ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz


//安装依赖包
[root@php ~]# dnf -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 libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make vim wget libsqlite3x-devel libzip-devel http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

//编译安装php
[root@php ~]# tar xf php-8.1.11.tar.gz 
[root@php ~]# cd 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
[root@php php-8.1.11]# 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]# source /etc/profile.d/php8.sh 
[root@php php-8.1.11]# which php
/usr/local/php8/bin/php
[root@php php-8.1.11]# php -v
PHP 8.1.11 (cli) (built: Oct 12 2022 02:49:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.11, Copyright (c) Zend Technologies

//配置php-fpm
[root@php php-8.1.11]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[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/init.d/php-fpm 
[root@php etc]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@php etc]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

//启动php-fpm
[root@php etc]# service php-fpm start
Starting php-fpm  done
[root@php etc]# 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               [::]:*    

//设置开机自启
[root@php etc]# chkconfig --add php-fpm
[root@php etc]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

php-fpm        	0:off	1:off	2:on	3:on	4:on	5:on	6:off

  • 在192.168.26.12上修改配置文件

[root@php ~]# vim /usr/local/php8/etc/php-fpm.d/www.conf
listen = 192.168.26.12:9000
;listen.allowed_clients = 192.168.26.10


[root@php ~]# mkdir -p /usr/local/nginx/html
[root@php ~]# vim /usr/local/nginx/html/index.php
<?php
     phpinfo();
?>
[root@php ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
  • 在192.168.26.10上修改配置
[root@nginx ~]# cd /usr/local/nginx/
[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.26.12:9000;		//监听php端的9000端口
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$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

在浏览器访问nginx端的80端口下的index.php

posted @ 2022-10-11 21:19  世界的尽头*  阅读(172)  评论(0)    收藏  举报