CentOS 7.4搭建LNMP环境

小柒助手 Linux290,1224字数 1607阅读5分21秒阅读模式

之前有一篇博客写的是LAMP的环境搭建,今天来详细介绍一下另外一个模式LNMP=Linux+Nginx+MySQL+PHP。和LAMP不同的是,LNMP中的N指的是Nginx(类似于Apache的一种WEB服务软件),并且PHP是作为一个独立服务存在的,这个服务叫做php-fpm,Nginx直接处理静态请求,动态请求会转发给php-fpm。

目前LNMP环境的应用非常多。在静态页面的处理上,Nginx比Apache更强;但在动态页面的处理上,Nginx并不占优势。
文章源自小柒网-https://www.yangxingzhen.cn/4538.html

目录

  • 准备工作
  • 安装Nginx服务
  • 安装Mysql服务
  • 搭建PHP运行环境

一、准备工作

一、服务器信息
文章源自小柒网-https://www.yangxingzhen.cn/4538.html

操作系统文章源自小柒网-https://www.yangxingzhen.cn/4538.html

服务器IP文章源自小柒网-https://www.yangxingzhen.cn/4538.html

CPU文章源自小柒网-https://www.yangxingzhen.cn/4538.html

内存文章源自小柒网-https://www.yangxingzhen.cn/4538.html

CentOS 7.4文章源自小柒网-https://www.yangxingzhen.cn/4538.html

192.168.0.117文章源自小柒网-https://www.yangxingzhen.cn/4538.html

1C文章源自小柒网-https://www.yangxingzhen.cn/4538.html

2G文章源自小柒网-https://www.yangxingzhen.cn/4538.html

软件版本:

Nginx:1.13.4

MYSQL:5.7.22

PHP:7.2.9

二、下载软件包

#Nginx软件包

https://mirrors.yangxingzhen.com/nginx/nginx-1.13.4.tar.gz

#MYSQL软件包

https://mirrors.yangxingzhen.com/mysql/mysql-5.7.22.tar.gz

#MYSQL依赖包

https://mirrors.yangxingzhen.com/mysql/boost_1_59_0.tar.gz

#PHP软件包

https://mirrors.yangxingzhen.com/php/php-7.2.9.tar.gz

#PHP依赖包

https://mirrors.yangxingzhen.com/libiconv/libiconv-1.15.tar.gz

三、将防火墙与selinux关闭

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# systemctl disable firewalld

[root@localhost ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config

二、安装Nginx服务

1、下载软件包和安装编译工具及依赖包

[root@localhost ~]# yum -y install wget gcc gcc-c++ make pcre-devel pcre perl openssl openssl-devel

[root@localhost ~]# wget -c https://mirrors.yangxingzhen.com/nginx/nginx-1.13.4.tar.gz

2、解压软件包

[root@localhost ~]# tar zxf nginx-1.13.4.tar.gz

3、创建用户和隐藏版本号

[root@localhost nginx-1.13.4]# sed -i 's/1.13.4/ /;s/nginx\//nginx/' src/core/nginx.h

[root@localhost nginx-1.13.4]# useradd -s /sbin/nologin www

4、预编译

[root@localhost ~]# cd nginx-1.13.4

[root@localhost nginx-1.13.4]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module

5、编译及安装

[root@localhost nginx-1.13.4]# make && make install

6、创建软连接

[root@localhost nginx-1.13.4]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin

7、启动Nginx服务

[root@localhost nginx-1.13.4]# nginx -t

[root@localhost nginx-1.13.4]# nginx

8、访问测试

#浏览器访问服务器(192.168.0.117),如下图

三、安装Mysql服务

#MYSQL安装

1、下载boost库(MYSQL5.7及以上需要,必须安装)

[root@localhost ~]# yum -y install wget

[root@localhost ~]# wget -c https://mirrors.yangxingzhen.com/mysql/boost_1_59_0.tar.gz

[root@localhost ~]# tar zxf boost_1_59_0.tar.gz -C /usr/local/

2、下载依赖包

[root@localhost ~]# yum -y install ncurses ncurses-devel perl perl-devel gcc gcc-c++ cmake make openssl-devel openssl

3、建立MYSQL用户

[root@localhost ~]# useradd -s /sbin/nologin mysql

4、下载MYSQL-5.7.22源码包

[root@localhost ~]# wget -c https://mirrors.yangxingzhen.com/mysql/mysql-5.7.22.tar.gz

[root@localhost ~]# tar zxf mysql-5.7.22.tar.gz

[root@localhost ~]# cd mysql-5.7.22

[root@localhost mysql-5.7.22]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

-DMYSQL_DATADIR=/data/mysql \

-DSYSCONFDIR=/etc \

-DEXTRA_CHARSETS=all \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_MEMORY_STORAGE_ENGINE=1 \

-DWITH_PARTITION_STORAGE_ENGINE=1 \

-DDOWNLOAD_BOOST=1 \

-DWITH_BOOST=/usr/local/boost_1_59_0 \

-DENABLED_LOCAL_INFILE=1 \

-DMYSQL_TCP_PORT=3306 \

-DWITH_READLINE=1 \

-DMYSQL_USER=mysql \

-DWITH_SSL=yes

[root@localhost mysql-5.7.22]# make && make install

5、创建MYSQL数据存储目录

[root@localhost mysql-5.7.22]# mkdir -p /data/mysql

[root@localhost mysql-5.7.22]# chown -R mysql.mysql /data/mysql

6、编辑MYSQL主配置文件

[root@localhost mysql-5.7.22]# vim /etc/my.cnf

[client]
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
log-error = /tmp/mysqld.log
server-id = 1
log-bin =mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

7、初始化数据库(生成空密码,生成随机密码--initialize)

[root@localhost mysql-5.7.22]# cd /usr/local/mysql/bin

[root@localhost bin]# ./mysqld --initialize-insecure --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql

8、配置成系统服务

[root@localhost bin]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@localhost bin]# chmod o+x /etc/init.d/mysqld

9、启动MYSQL服务、添加开启自启动

[root@localhost bin]# chown -R mysql.mysql /usr/local/mysql

[root@localhost bin]# service mysqld start

[root@localhost bin]# chkconfig --add mysqld

[root@localhost bin]# chkconfig mysqld on

10、创建软连接、访问数据库

[root@localhost bin]# ln -s /usr/local/mysql/bin/* /usr/bin

[root@localhost bin]# mysql

#数据库安装配置成功,退出则输入"quit"

四、搭建PHP运行环境

#安装libiconv

1、下载libiconv软件包

[root@localhost ~]# wget -c https://mirrors.yangxingzhen.com/libiconv/libiconv-1.15.tar.gz

2、解压libiconv软件包

[root@localhost ~]# tar zxf libiconv-1.15.tar.gz

[root@localhost ~]# cd libiconv-1.15

3、预编译

[root@localhost libiconv-1.15]# ./configure --prefix=/usr/local/libiconv

4、编译及安装

[root@localhost libiconv-1.15]# make && make install

#安装 PHP-7.2.9

1、安装编工具及插件(下载阿里云yum源)

[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

[root@localhost ~]# yum -y install gcc gcc-c++ libvpx libvpx-devel libjpeg-turbo libjpeg-turbo-devel libzip libpng libpng-devel freetype-devel libxml2 libxml2-devel gd gd-devel libxslt-devel libcurl-devel libmcrypt-devel mhash mcrypt openssl openssl-devel

2、解压PHP软件包

[root@localhost ~]# tar zxf php-7.2.9.tar.gz

[root@localhost ~]# cd php-7.2.9

3、预编译

[root@localhost php-7.2.9]# ln -s /usr/lib64/libldap* /usr/lib

[root@localhost php-7.2.9]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64

[root@localhost php-7.2.9.]# ./configure --prefix=/usr/local/php \

--with-config-file-path=/usr/local/php/etc \

--with-mysql=/usr/local/mysql \

--with-mysqli=/usr/local/mysql/bin/mysql_config \

--with-pdo-mysql=/usr/local/mysql \

--enable-opcache \

--with-iconv-dir=/usr/local/libiconv \

--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-fpm \

--enable-mbstring \

--with-mcrypt \

--with-gd \

--enable-gd-native-ttf \

--with-openssl \

--with-mhash \

--enable-pcntl \

--enable-sockets \

--with-xmlrpc \

--enable-soap \

--enable-short-tags \

--enable-static \

--with-fpm-user=www \

--with-fpm-group=www \

--enable-ftp \

--enable-zip \

--with-gettext

4、编译及安装

[root@localhost php-7.2.9.]# make && make install

4、配置PHP

[root@localhost php-7.2.9]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@localhost php-7.2.9]# cp php.ini-production /usr/local/php/etc/php.ini

[root@localhost php-7.2.9]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

[root@localhost php-7.2.9]# ln -s /usr/local/php/sbin/php-fpm /usr/bin

5、编辑PHP测试页面

[root@localhost php-7.2.9]# cd /usr/local/nginx/html

[root@localhost html]# vim index.php

<?php
phpinfo();
?>

6、配置Nginx

#编辑nginx.conf配置文件,内容如下

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf

    user www www;
    worker_processes 1;
    worker_cpu_affinity 00000001;
    worker_rlimit_nofile 10240;
    error_log /usr/local/nginx/logs/error.log;
    pid /usr/local/nginx/logs/nginx.pid;
events {
    use epoll;
    worker_connections 10240;
    multi_accept on;
    }
http {
    include mime.types;
    default_type application/octet-stream;
    access_log /usr/local/nginx/logs/access.log;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 120;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    client_max_body_size 10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    large_client_header_buffers 4 4k;
    client_header_buffer_size 4k;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;
    include vhosts/*;
server {
    listen 80;
    server_name localhost;
    location / {
    root html;
    index index.php 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;
    }
}
}

#保存退出

7、重启Nginx服务,启动php-fpm服务

[root@localhost html]# nginx -s reload

[root@localhost html]# /usr/local/php/sbin/php-fpm

8、浏览器访问服务器:http://192.168.0.117

9、测试数据库

[root@localhosthtml]# mysql

mysql> create database test;

mysql> grant all on test.* to test@'%' identified by '123456';

mysql> flush privileges;

[root@localhost html]# vim info_mysql.php

<?php
$link=mysqli_connect('192.168.0.117','test','123456');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>

10、访问测试

浏览器访问http://192.168.0.117/info_mysql.php

至此,Liunx环境LNMP环境搭建完毕。

若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!

继续阅读
Wechat
微信扫一扫,加我!
weinxin
我的微信
微信号已复制
微信公众号
微信扫一扫,关注我!
weinxin
我的公众号
公众号已复制
Linux最后更新:2022-11-23
小柒助手
  • 本文由 小柒助手 发表于 2019年6月28日 17:24:12
  • 声明:本站所有文章,如无特殊说明或标注,本站文章均为原创。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。转载请务必保留本文链接:https://www.yangxingzhen.cn/4538.html
评论  2  访客  2
    • 运维老司机
      运维老司机 6

      亲测,写的不错 :razz:

    匿名

    发表评论

    匿名网友
    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    拖动滑块以完成验证