Linux中详细搭建Lnmp架构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux中详细搭建Lnmp架构相关的知识,希望对你有一定的参考价值。

环境说明

ip 需要安装的程序
192.168.209.12 nginx mysql php
 *lnmp架构的安装的顺序*
 ①安装nginx
 ②安装mysql
 ③安装php
 ④最后配置nginx和创建php测试程序
 ⑤验证

*******************①安装nginx***********************
1.//创建系统用户nginx
 [[email protected] ~]# useradd -r -M -s /sbin/nologin nginx

//安装依赖环境
[[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel
[[email protected] ~]# yum install -y gd-devel-2.0.35-26.el7.x86_64.rpm 
[[email protected] ~]# yum -y groups mark install ‘Development Tools‘

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

//下载nginx
[[email protected] ~]# cd /usr/src/ 
[[email protected] src]# wgethttp://nginx.org/download/nginx-1.14.0.tar.gz

//编译安装
[[email protected] src]# tar xf nginx-1.14.0.tar.gz
[[email protected] src]# cd  nginx-1.14.0

[[email protected] nginx-1.14.0]# ./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

 [[email protected] nginx-1.14.0]# make -j $(grep ‘processor‘ /proc/cpuinfo | wc -l) && make install

 2.nginx安装后配置
 //配置环境变量   
  [[email protected] ~]# echo ‘export PATH=/usr/local/nginx/sbin:$PATH‘ > /etc/profile.d/nginx.sh
  [[email protected] ~]# source /etc/profile.d/nginx.sh 
  [[email protected] ~]# nginx
  [[email protected] ~]# ss -antl
  State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port              
  LISTEN     0      128                     *:80                                  *:*                  
  LISTEN     0      128                     *:22                                  *:*                  
  LISTEN     0      100             127.0.0.1:25                                  *:*    
  LISTEN     0      128                    :::22                                 :::*                  
  LISTEN     0      100                   ::1:25                                 :::*                   

 ***********************②安装mysql**************************
 //安装依赖 包
[[email protected] ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

//创建用户和组
[[email protected] ~]# cd /usr/src/
[[email protected] src]# groupadd -r -g 306 mysql
[[email protected] src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

//下载二进制格式的mysql软件包

[[email protected] src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

//解压软件至/usr/local
[[email protected] src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  -C /usr/local/
[[email protected] src]# cd /usr/local/

//创建软连接
[[email protected] local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql

//修改目录/usr/local/mysql的属主属组
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] local]# ll /usr/local/mysql

//添加环境变量
[[email protected] ~]# ls /usr/local/mysql
[[email protected] ~]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[[email protected] ~]# source /etc/profile.d/mysql.sh
[[email protected] ~]# echo $PATH 

//建立数据存放目录
[[email protected] ~]# mkdir /opt/data
[[email protected] ~]# chown -R mysql.mysql /opt/data/
[[email protected] ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 8月  19 13:20 data

//初始化数据库 注意这个命令后会生成临时密码 要记住 jd?ajfrKY4pQ
[[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/

//配置mysql
//软连接
[[email protected] ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
[[email protected] ~]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf
[[email protected] ~]# ldconfig -v

//生成配置文件
[[email protected] ~]# 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

//配置服务启动脚本
[[email protected] ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] ~]# sed -ri ‘s#^(basedir=).*#1/usr/local/mysql#g‘ /etc/init.d/mysqld
[[email protected] ~]# sed -ri ‘s#^(datadir=).*#1/opt/data#g‘ /etc/init.d/mysqld

//启动mysql
[[email protected] ~]# service mysqld start
[[email protected] ~]# ps -ef |grep mysql
[[email protected] ~]#  ss -antl
    State      Recv-Q Send-Q      Local Address:Port                      Peer Address:Port              
LISTEN     0      128                     *:80                                  *:*                  
LISTEN     0      128                     *:22                                  *:*                  
LISTEN     0      100             127.0.0.1:25                                  *:*                                
LISTEN     0      128                    :::22                                 :::*                  
LISTEN     0      100                   ::1:25                                 :::*                  
LISTEN     0      80                     :::3306                               :::*         

//修改密码 使用临时密码登入
[[email protected] ~]# mysql -uroot -p
jd?ajfrKY4pQ 这是以上步骤的临时密码
mysql> set password = password(‘lanzhiyong‘);

****************************③安装php*************************
3.安装php
//配置yum源
[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo 
[[email protected] ~]# sed -i ‘s/$releasever/7/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[[email protected] ~]# sed -i ‘s/^enabled=.*/enabled=1/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[[email protected] ~]# yum install -y epel-release

//安装依赖包
[[email protected] ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel

//下载php
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz

//编译安装php   
[[email protected] src]# tar xf php-7.2.8.tar.xz
[[email protected] src]# cd php-7.2.8
[[email protected] php-7.2.8]#
./configure --prefix=/usr/local/php7 --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir=/usr --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-jpeg-dir --with-png-dir --with-xmlrpc --with-xsl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip
[[email protected] php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor |wc -l)
[[email protected] php-7.2.8]# make install

//安装后配置
[[email protected] ~]# echo ‘export PATH=/usr/local/php7/bin:$PATH‘ >  /etc/profile.d/php7.sh
[[email protected] ~]# source /etc/profile.d/php7.sh
[[email protected] ~]# cd /usr/src/php-7.2.8
[[email protected] php-7.2.8]# which php
/usr/local/php7/bin/php
[[email protected] php-7.2.8]# php -v

//配置php-fpm
[[email protected] php-7.2.8]# cp php.ini-production /etc/php.ini
[[email protected] php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[[email protected] php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[[email protected] php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf

[[email protected] php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

//配置fpm的相关选项为你所需的值:
[[email protected] ~]# vim /usr/local/php7/etc/php-fpm.conf
*****在配置文件最后面添加一下内容
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
[[email protected] ~]# tail /usr/local/php7/etc/php-fpm.conf

//启动php-fpm
[[email protected] ~]# service php-fpm start
Starting php-fpm  done
[[email protected] ~]# ss -antl 
State      Recv-Q Send-Q      Local Address:Port       Peer Address:Port              
LISTEN     0      128                     *:80                                  *:*                  
LISTEN     0      128                     *:22                                  *:*                  
LISTEN     0      100             127.0.0.1:25                            *:*                  
LISTEN     0      128             127.0.0.1:9000                        *:*                  
LISTEN     0      128                    :::22                                 :::*                  
LISTEN     0      100                   ::1:25                                :::*                  
LISTEN     0      80                     :::3306                              :::*   
 [[email protected] ~]# ps -ef |grep php

*******************④最后配置nginx和创建php测试程序****************************

//nginx配置文件/usr/local/nginx/conf/nginx.conf,只要修改配置文件中的server{ } 配置块中的内容,修改location块,追加index.php让nginx服务器默认支持index.php为首页
技术分享图片

技术分享图片

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
…………………………………………
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm//后面添加 index.php;
}
…… ……………………………………
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ .php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#主要修改的就是fastcgi_param中的/scripts为$document_root
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include        fastcgi_params;
}
#deny access to .htaccess files, if Apache‘s document root

//修改配置文件后重新加载nginx
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

//创建测试php程序,在/usr/local/nginx/html/test.php,打印一下php配置
[[email protected] ~]# cat > /usr/local/nginx/html/test.php << EOF
> ?php
> phpinfo();
> ?>
> EOF

  ***********************⑤验证******************************
最后打开浏览器输入 192.168.209.12/test.php 看到输出的页面,说明nginx和php都配置成功了

技术分享图片

以上是关于Linux中详细搭建Lnmp架构的主要内容,如果未能解决你的问题,请参考以下文章

CentOS 7 搭建 LNMP 架构详细过程 + Discuz 论坛安装 (手工编译)

分离部署LNMP搭建WORDPRESS详细步骤

详述Linux系统中搭建LNMP架构+Discuz论坛

LNMP架构搭建Discuz论坛(实战!)

Linux:LNMP架构的搭建

在服务器上搭建多个WordPress博客网站教程(超级详细)