lnmp部署


lnmp部署

部署环境

主机名 IP地址 服务 系统
nginx 192.168.34.130 nginx centos8
mysql 192.168.34.131 mysql centos8
lnmp 192.168.34.139 php centos8

nginx部署

关闭防火墙与selinux
[root@httpd ~]# systemctl  stop firewalld
[root@httpd ~]# cat /etc/sysconfig/selinux
SELINUX=disabled
创建系统用户
[root@nginx src]# useradd -r -M -s /sbin/nologin nginx
安装所需的包
[root@nginx src]# yum -y install pcre-devel openssl openssl-devel make gd-devel gcc gcc-c++
创建日志存放目录
[root@nginx src]# mkdir -p /var/log/nginx
[root@nginx src]# chown -R nginx.nginx /var/log/nginx/
安装nginx包
[root@nginx src]# wget https://nginx.org/download/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 && make install
配置环境变量
[root@nginx nginx-1.20.2]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx nginx-1.20.2]# source /etc/profile.d/nginx.sh 
关闭nginx
[root@nginx nginx-1.20.2]# nginx
[root@nginx nginx-1.20.2]# nginx -s stop
编写service文件
[root@nginx nginx-1.20.2]# 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 nginx-1.20.2]# systemctl daemon-reload
[root@nginx nginx-1.20.2]# systemctl enable --now nginx

mysql二进制安装

安装所需的包
[root@mysql ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建用户和组
[root@mysql ~]# groupadd -r -g 306 mysql
[root@mysql ~]# useradd -rMs /sbin/nologin -g 306 -u 306 mysql
解压mysql

[root@mysql src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
[root@mysql src]# tar xf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz -C /usr/local
做软连接
[root@mysql src]# cd /usr/local
[root@mysql local]# ln -s mysql-5.7.39-linux-glibc2.12-x86_64/ mysql
配置环境变量

[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 ~]#  ln -s /usr/local/mysql/include/ /usr/include/mysql/
[root@mysql ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@mysql ~]# ldconfig 
添加以下内容
[root@mysql ~]# vi /etc/man_db.conf
MANDATORY_MANPATH                       /usr/local/mysql/man
创建存放目录
[root@mysql ~]# mkdir /opt/data
[root@mysql ~]# chown -R mysql.mysql /opt/data/
初始化数据库
[root@mysql mysql]# /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir /opt/data/
记住初始化时的密码
卸载自带的mariadb
[root@mysql mysql]# dnf -y remove mariadb*
[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
[root@mysql mysql]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# cp mysql.server /etc/init.d/mysqld
//找到basedir添加内容
[root@mysql support-files]# vim /etc/init.d/mysqld
basedir=/usr/local/mysqld
datadir=/opt/data
[root@mysql support-files]# chmod +x /etc/init.d/mysqld
[root@mysql support-files]# service mysqld start
[root@mysql support-files]# yum -y install libncurses*
[root@localhost support-files]# mysql -uroot -p'hb;9Qww,j%+.'
设置新密码
mysql>  set password = password('liuyang123!');

php部署

下载epel源
[root@lnmp src]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@lnmp src]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@lnmp src]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
下载所需的包
[root@lnmp src]# 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 libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd
[root@lnmp src]# yum -y install sqlite-devel openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ 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.rpmstream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
下载php安装包
[root@lnmp src]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
解压php,编译三部曲
[root@lnmp src]# tar xf php-8.1.11.tar.gz 
[root@lnmp php-8.1.11]# ./configure --prefix=/usr/local/php  --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@lnmp php-8.1.11]# make && make install
配置php-fpm
[root@lnmp php]#  cp etc/php-fpm.conf.default etc/php-fpm.conf 
[root@lnmp php]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
配置环境变量
[root@lnmp php-8.1.11]#  echo 'export PATH=/usr/local/php/bin:$PATH' > /etc/profile.d/php.sh
[root@lnmp php-8.1.11]# source /etc/profile.d/php.sh
编写库文件
[root@lnmp php-8.1.11]# cat /etc/ld.so.conf.d/php.conf
/usr/local/php/lib
编写头文件
[root@lnmp php-8.1.11]# ln -s /usr/local/php/include/ /usr/include/php
编写service文件
[root@lnmp php-8.1.11]# cat /lib/systemd/system/php.service
[Unit]
Description=php server daemon
After=network.target sshd-keygen.target
Wants=sshd-keygen.target
 
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=ps -ef |grep php |grep -v grep|awk '{print$2}'|xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID
 
[Install]
WantedBy=multi-user.target
[root@lnmp ~]# systemctl start php.service 
[root@lnmp ~]# systemctl enable php.service 

连接php和nginx

php主机

[root@lnmp ~]# mkdir -p /usr/local/nginx/html
编写测试主页
[root@lnmp ~]# cat /usr/local/nginx/html/index.php 
<?php    
   phpinfo();
?>
[root@lnmp ~]# vi /usr/local/php/etc/php-fpm.d/www.conf
listen = 192.168.34.139:9000		//php服务监听的端口	
listen.allowed_clients = 192.168.34.130		//允许访问的ip(nginx的ip)
[root@lnmp ~]# systemctl restart php

nginx主机

编写测试主页
[root@nginx html]# cat /usr/local/nginx/html/index.php 
<?php
   phpinfo();
?>
[root@nginx ~]# systemctl restart nginx.service 
//nginx端编辑配置文件		修改以下配置
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
root           /usr/local/nginx/html;	//定义的nginx主页存放目录
        location ~ \.php$ {
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index  index.php;
                #下面两句是给fastcgi权限,可以支持 ?s=/module/controller/action的url访问模式
                fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                #下面两句才能真正支持 index.php/index/index/index的pathinfo模式
                fastcgi_param  PATH_INFO  $fastcgi_path_info;
                fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
                include        fastcgi_params;
}
location / {
           #try_files $uri $uri/ /index.php?s=$uri&$args;
           if ( !-e $request_filename) {
                rewrite ^/(.*)$ /index.php?$1 last;
                break;
           }
           try_files $uri $uri/ /index.php$args;
  }
[root@nginx ~]# systemctl restart nginx.service 

测试

posted @ 2022-10-11 21:03  Tqing  阅读(44)  评论(0编辑  收藏  举报