lnmp部署

lnmp部署

  1. 安装nginx
  2. 安装mysql
  3. 安装php

环境

版本IP
centos8192.168.147.66
//关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

安装nginx

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

[root@localhost ~]# yum -y groups mark install 'Development Tools'

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

//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
[root@localhost ~]# id nginx
uid=973(nginx) gid=972(nginx)=972(nginx)

//下载nginx
[root@localhost ~]# wget -c http://nginx.org/download/nginx-1.20.1.tar.gz
--2021-06-23 06:39:02--  http://nginx.org/download/nginx-1.20.1.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5704::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1061461 (1.0M) [application/octet-stream]
正在保存至: “nginx-1.20.1.tar.gz”

nginx-1.20.1.tar.gz      100%[================================>]   1.01M  22.0KB/s  用时 32s     

2021-06-23 06:39:34 (32.4 KB/s) - 已保存 “nginx-1.20.1.tar.gz” [1061461/1061461])

[root@localhost ~]# ls
  anaconda-ks.cfg       nginx-1.20.1.tar.gz
  initial-setup-ks.cfg
[root@localhost ~]# tar xf nginx-1.20.1.tar.gz 
[root@localhost ~]# ls
  anaconda-ks.cfg       nginx-1.20.1
 initial-setup-ks.cfg  nginx-1.20.1.tar.gz

[root@localhost ~]# cd nginx-1.20.1/
编译安装nginx
[root@localhost nginx-1.20.1]# ./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.1]# nproc
2
//使用两个内核去安装
[root@localhost nginx-1.20.1]# make -j 2
[root@localhost nginx-1.20.1]# make install

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


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


//修改配置文件使nginx能够识别php文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

user nginx nginx; //添加这行
        location / {
             root   html;
             index  index.php index.html index.htm; //添加index.ph
         }

//取消下列内容每行前面的#,并添加nginx网站发布目录的路径即可
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
//启动测试
[root@localhost ~]# nginx
[root@localhost ~]# 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:*             

安装mysql

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

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

//解压软件至/usr/local/
下载软件包去mysql官网下下载到本地后拖到虚拟机里
[root@localhost ~]# ls
anaconda-ks.cfg       mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz  nginx-1.20.1.tar.gz
initial-setup-ks.cfg  nginx-1.20.1

[root@localhost ~]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

//修改名称
[root@localhost ~]# cd /usr/local
[root@localhost local]# mv mysql-5.7.33-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  nginx  sbin  share  src

//修改目录/usr/local/mysql的属主属组
[root@localhost local]# ll /usr/local/mysql -d
drwxr-xr-x. 9 root root 129 624 06:44 /usr/local/mysql
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll /usr/local/mysql -d
drwxr-xr-x. 9 mysql mysql 129 624 06:44 /usr/local/mysql

//添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >/etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh 
[root@localhost ~]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf  //添加下列一行内容

/usr/local/mysql/lib

[root@localhost ~]# ldconfig
[root@localhost ~]# vim /etc/man_db.conf   //添加下列一行内容
MANDATORY_MANPATH                       /usr/local/mysql/man


//建立数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 624 1853 data


//生成配置文件
[root@localhost ~]# 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@localhost ~]# 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@localhost ~]# ls /usr/local/mysql/
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost ~]# ls /usr/local/mysql/support-files/
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@localhost ~]# vim /etc/init.d/mysqld 
//补齐这两行=后面的路径
basedir=/usr/local/mysql
datadir=/opt/data

//启动mysql
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS! 
[root@localhost ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process             
LISTEN   0        128                    *:80                  *:*              
LISTEN   0        128                 [::]:22               [::]:*                         
LISTEN   0        80                     *:3306                *:*    

[root@localhost ~]# mysql -uroot -p'x/xHyJC2%wB7'
//修改密码
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

//设置开机自启
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

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

安装php

[root@localhost ~]# 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
[root@localhost ~]# dnf -y install php*
[root@localhost ~]# php -v
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.24, Copyright (c) 1999-2018, by Zend Technologies


//启动php-fpm
[root@localhost ~]# systemctl start php-fpm
[root@localhost ~]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-06-24 07:11:10 EDT; 6s ago
 Main PID: 66434 (php-fpm)
   Status: "Ready to handle connections"
    Tasks: 6 (limit: 4612)
   Memory: 27.3M
   CGroup: /system.slice/php-fpm.service
           ├─66434 php-fpm: master process (/etc/php-fpm.conf)
           ├─66435 php-fpm: pool www
           ├─66436 php-fpm: pool www
           ├─66437 php-fpm: pool www
           ├─66438 php-fpm: pool www
           └─66439 php-fpm: pool www

624 07:11:10 localhost.localdomain systemd[1]: Starting The PHP FastCGI Process Manager...
624 07:11:10 localhost.localdomain systemd[1]: Started The PHP FastCGI Process Manage

//设置开机自启
[root@localhost ~]# systemctl enable php-fpm    
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.


[root@localhost ~]# vim /etc/php-fpm.d/www.conf

;listen = /run/php-fpm/www.sock   //在行首加;
listen = 0.0.0.0:9000             //添加这一行

[root@localhost ~]# systemctl restart php-fpm
[root@localhost ~]# ss -antl
State      Recv-Q     Send-Q         Local Address:Port         Peer Address:Port     Process     
LISTEN     0          128                  0.0.0.0:9000              0.0.0.0:*                                       
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          80                         *:3306                    *:*                                        
LISTEN     0          128                     [::]:22                   [::]:*                                        
LISTEN     0          100                    [::1]:25                   [::]:*        


创建虚拟主机目录并生成php测试页面
[root@localhost html]# cd /usr/local/nginx/html
[root@localhost html]# ls
50x.html  index.html  
[root@localhost html]# vim index.php
//添加下列内容
<?php
   phpinfo();
?>

[root@localhost ~]# vim /etc/php.ini 

;cgi.fix_pathinfo=0    //参数修改为0即可

//重启php
[root@localhost etc]# systemctl restart php-fpm


[root@localhost ~]# cd /usr/local/nginx/conf
[root@localhost conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@localhost conf]# vim nginx.conf

server {
        listen       80;
        server_name  localhost;
        root /www;
        index index.php index.html;

//重启nginx
[root@localhost conf]# nginx -s stop
[root@localhost conf]# nginx

在这里插入图片描述
配置完成

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值