LNMP架构搭建

这是一套非常经典的架构模型,也是一名运维工程师必须要掌握的一项技能。在搭建以前一定要将自己所要架构的版本与开发人员对接起来,否则会出现版本不兼容的状态。

我使用的是Linux7.6+nginx1.16.0+mysql5.7+php7.2,版本可能会有些老,但是一些企业还是会在使用,并且这些都是换汤不换药的,可以类比这些去搭建。值得注意的是,一定要注意搭建顺序,否则会发生软件相互找不到的情况。

环境准备:

先安装linux7.6修改主机名称,按照功能+公司域名的原则,然后关闭防火墙、设置静态IP地址、然后将网络管理器关闭、配置好yum源(我使用的是我曾经的配置过的,详细可以参考CSDN)、最后将selinux关闭。

配置主机和IP地址
# hostnamectl set-hostname web01.zld.cn
# su
# vim /etc/sysconfig/network-scripts/ifcfg-ens33
...
IPADDR=10.1.1.11
# vim /etc/hosts
10.1.1.11 web01 web01.zld.cn
# systemctl restart network
关闭一些安全措施
# systemctl stop firewalld
# systemctl disable firewalld
# setenforce 0
# vim /etc/selinux/config
    SELINUX=disabled
关闭网络管理器
# systemctl stop NetworkManager
# systemctl disable NetworkManager
时间同步
# ntpdate ntp.aliyun.com

1.搭建mysql 

第一步:先将mysql软件包上传到linux系统中,这里我用的是MobaXterm远程软件链接
第二步:将包解压并移动到另一个文件夹下,并创建用户
# tar -xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
# rm -rf /usr/local/mysql
# mv mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql
# useradd -r -s /sbin/nologin mysql
第三步:移动到这目录下创建文件,修改相应权限和拥有者
# cd /usr/local/mysql/
# mkdir mysql-files
# chown mysql.mysql mysql-files
# chmod 750 mysql-files
# rm -rf /etc/my.cnf
第四步:初始化操作(记得记录下来临时密码,在最后一行的最后几个像乱码一样的字符)
# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql
......
.....
2024-12-25T09:04:02.905210Z 1 [Note] A temporary password is generated for root@localhost: -kYx?u,:j7WU
第五步:将文件放在启动项中,可以让mysql随时随地启动

# echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
# source /etc/profile
# cp support-files/mysql.server /etc/init.d/mysqld
# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/web01.zld.cn.err'.
 SUCCESS!
第六步:登录mysql然后密码初始化,并且安全设置
# mysql -p
Enter password:临时密码


mysql> set password='123';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

# mysql_secure_installation 看清楚选项再选择,前两项可选,后边选y就可以
第七步:设置为开机自启
# chkconfig --add mysqld
# chkconfig mysqld on

# chkconfig --list

3-5为on就可以

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

2.安装Nginx

第一步:解压创建用户,进入目录后设置配置文件、编译安装

# # yum install pcre-devel zlib-devel openssl-devel -y
# tar -xf nginx-1.16.0.tar.gz
# useradd -r -s /sbin/nologin www
# cd nginx-1.16.0
[root@web01 nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_   status_module --with-http_realip_module

# make & make install

第二步:配置到整个环境都可以使用

# vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=Nginx Web Server
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target


#systemctl start nginx
# systemctl enable nginx

3.安装php

第一步:安装依赖库

# yum -y install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl-devel openssl-devel

第二步:配置编译安装(注意先安装依赖库,然乎再解压编译,否则会报错)

# tar -xf php-7.2.12.tar.gz
# cd php-7.2.12

# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-libzip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts

# make && make install

第三步:配置文件,目的是为启动配置环境

# cp /root/php-7.2.12/php.ini-development /usr/local/php/etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

第四步:设置启动项,并启动

# cp /root/php-7.2.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on


# service php-fpm start
# ps -ef |grep php-fpm
# netstat -tnlp |grep 9000

第五步:配置环境变量

# echo 'export PATH=$PATH:/usr/local/php/bin' >> /etc/profile
# source /etc/profile

4.*****让nginx与php联动******

第一步:进入到目录中,先进行备份

# cd /usr/local/nginx
# cp conf/nginx.conf conf/nginx.conf.bak

第二步:修改nginx配置文件,可以全部删除替换

# vim /conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        root html;
        location / {
            index  index.html index.htm;
        }
        location ~ \.php$ {
        	fastcgi_pass   127.0.0.1:9000;
        	fastcgi_index  index.php;
        	fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        	include        fastcgi_params;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }
    }
}

# systemctl reload nginx

第三步:测试

# vim /usr/local/nginx/html/demo.php



浏览器输入http://主机的ip地址/demo.php

 

看到这里的小伙伴一定是非常热爱学习的人,制作不易可以点赞收藏加关注,我会出更多免费的更有用的教程,谢谢大家的鼓励,爱你们哦!

你可能感兴趣的:(运维,linux,mysql,php,nginx)