centos7搭建lnmp

一、准备

1. 修改网络yum源

先将系统自带的yum配置文件重命名或删除,然后下载下面两个文件

阿里云:http://mirrors.aliyun.com/repo/Centos-7.repo

eprl扩展:http://mirrors.aliyun.com/repo/epel-7

下载完之后,需要使用命令清除原来的yum缓存,使用新的配置文件建立新的缓存

yum clean all                #清除原来的缓存
yum makecache                #建立新的缓存列表
yum update                   #将所有可更新的软件更新

2. 安装编译工具和依赖软件

yum -y install gcc gcc-c++ pcre-devel openssl openssl-devel zlib-devel ncurses-devel cmake bison libxml2-devel libpng-devel

3. nginx,mysql,php软件源码包下载地址

  nginx:http://nginx.org/en/download.html

  mysql:https://dev.mysql.com/downloads/mysql/

  php:http://www.php.net/

  版本选用:

  nginx:  1.16.*  #选用软件的稳定版本即可

  mysql:  5.5.*  #5.5以上版本需要1G以上的内存,否则无法安装

  php:  7.3.*  #我使用的是php7

注:每次安装LNMP时,软件的小版本都不一样,官方会对其大版本下的小版本进行覆盖式更新,请按照下载版本进行安装。

二、源码软件包安装

1. nginx

nginx是一款轻量级的web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like协议下发行。特点是占用内存少,并发能力强。

1.1 下载nginx源码包

[root@centos2 /lnmp]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

1.2 创建用于运行nginx的用户

[root@centos2 /lnmp]# useradd -r -s /sbin/nologin nginx

1.3 解压缩nginx并安装

[root@centos2 /lnmp]# tar -zxf nginx-1.16.1.tar.gz
[root@centos2 /lnmp/nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
[root@centos2 /lnmp/nginx-1.16.1]# make && make install

1.4 上传编写好的nginx启动脚本

注:先检查文件中是否有字符集问题

#################################Nginx启动管理脚本##################################
#!/bin/bash
#Author:nanshan
#chkconfig: 2345 99 33
#description: nginx server control tools

ngxc="/usr/local/nginx/sbin/nginx"
pidf="usr/local/nginx/logs/nginx.pid"
ngxc_fpm="/usr/local/php/sbin/php-fpm"
pidf_fpm="/usr/local/php/var/run/php-fpm.pid"
case "$1" in
    start)
        $ngxc -t  &> /dev/null
        if [ $? -eq 0 ];then
                    $ngxc
                    $ngxc_fpm
                    echo "nginx service start success!"
        else
                    $ngxc -t
        fi
        ;;
    stop)
        kill -s QUIT $(cat $pidf)
        kill -s QUIT $(cat $pidf_fpm)
                echo "nginx service stop success!"
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        $ngxc -t &> /dev/null
        if [ $? -eq 0 ];then
                    kill -s HUP $(cat $pidf)
                    kill -s HUP $(cat $pidf_fpm)
                    echo "reload nginx config success!"
        else
                    $ngxc -t
        fi
        ;;
    *)
        echo "please input stop|start|restart|reload."
        exit 1
esac

2. MySQL

下载:https://dev.mysql.com/downloads/mysql/

选择:Looking for previous GA versions?

选择:Select Version:按照自己要求选择

   Select Operating System: Source Code

   Select OS Version: Generic Linux

格式:mysql-N.N.NN.tar.gz

[root@centos2 /lnmp/nginx-1.16.1]# wget https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.62.tar.gz

2.1 创建运行mysql的用户

[root@centos2 /lnmp]# useradd -r -s /sbin/nologin mysql

2.2 解压缩mysql并安装

[root@centos2 /lnmp]# tar -zxvf mysql-5.5.62.tar.gz
[root@centos2 ~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -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_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
[root@centos2 ~]# make && make install
[root@centos2 ~]# ln -s /usr/local/mysql/bin/* /usr/local/bin/

2.3 修改安装后的目录权限

[root@centos2 ~]# cd /usr/local/mysql
[root@centos2 ~]# chown -R root .
[root@centos2 ~]# chown -R mysql data

2.4 生成mysql配置文件

[root@centos2 ~]# cp -a /lnmp/mysql-5-5.62/support-files/my-medium.cnf /etc/my.cnf

2.5 初始化,生成授权表

[root@centos2 ~]# cd /usr/local/mysql
[root@centos2 ~]# ./scripts/mysql_install_db --user=mysql
#初始化成功标志:两个OK

2.6 生成mysql的启动和自启动脚本

[root@centos2 ~]# cd /lnmp/mysql-5.5.62/support-files/

[root@centos2 /lnmp/mysql-5.5.62/support-files]# cp -a mysql.server /etc/init.d/mysqld

[root@centos2 /etc/init.d]# chmod +x mysqld 

[root@centos2 /etc/init.d]# chkconfig --add mysqld 
[root@centos2 /etc/init.d]# chkconfig mysqld on

[root@centos2 /etc/init.d]# systemctl start mysqld
[root@centos2 /etc/init.d]# systemctl status mysqld

2.7 给mysql的root用户设置密码

[root@centos2 /usr/local/mysql]# mysqladmin -uroot password 123456
[root@centos2 /usr/local/mysql]# mysql -uroot -p
Enter password: 

3. PHP

下载:https://www.php.net/

           http://mirrors.sohu.com/php/

[root@centos2 /lnmp]# wget https://www.php.net/distributions/php-7.3.12.tar.gz

3.1 解压缩并安装

[root@centos2 /lnmp/php-7.3.12]# tar -zxf php-7.3.12.tar.gz
[root@centos2 /lnmp/php-7.3.12]# ./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc/ --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-soap --enable-mbstring=all --enable-sockets --with-pdo-mysql=/usr/local/mysql --with-gd --without-pear --enable-fpm
[root@centos2 /lnmp/php-7.3.12]# make
[root@centos2 /lnmp/php-7.3.12]# make install

3.2 生成php配置文件

[root@centos2 ~]# cp -a /lnmp/php-7.3.12/php.ini-production  /usr/local/php/etc/php.ini
#复制源码包内的配置文件到安装目录下,并改名即可

3.3 创建软连接,使用php相关命令更方便

[root@centos2 ~]# ln -s /usr/local/php/bin/* /usr/local/bin/
[root@centos2 ~]# ln -s /usr/local/php/sbin/* /usr/local/sbin/

4. 配置nginx连接php

4.1 nginx连接php需启动php-fpm服务

[root@centos2 ~]# cd /usr/local/php/etc/
[root@centos2 /usr/local/php/etc]# cp php-fpm.conf.default php-fpm.conf
[root@centos2 /usr/local/php/etc]# vim php-fpm.conf
#修改指定参数
pid = run/php-fpm.pid
[root@centos2 /usr/local/php/etc]# cd /usr/local/php/etc/php-fpm.d/
[root@centos2 /usr/local/php/etc/php-fpm.d]# cp -a www.conf.default www.conf
[root@centos2 /usr/local/php/etc/php-fpm.d]# vim www.conf
#修改用户和组的指定用户
user = nginx
group = nginx

修改nginx启动脚本;将php-fpm的注释取消即可

4.2 修改nginx的配置文件,使其能识别.php后缀的文件

[root@centos2 /usr/local/php/etc/php-fpm.d]# cd ../../../nginx/conf/
[root@centos2 /usr/local/nginx/conf]# vim nginx.conf
#取消下列行的注释,并修改include 选项后的后缀为fastcgi.conf,并注意每一行结尾的分号和大括号

        location ~ .php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;    #修改为fastcgi.conf
        }

测试:

重启nginx,创建php测试文件,访问并查看是否解析

4.3 修改nginx配置文件,使其默认自动加载php文件

location / {
    root   html;                #nginx默认的网页路径:prefix/html
    index  index.html index.php index.htm; #设置默认加载的页面,以及优先级
}
原文地址:https://www.cnblogs.com/sswind/p/11996629.html