CentOS7.X安装LMMP环境Nginx+PHP+Mysql详解

前言:

作为PHP开发者,我们常用的线上环境就是LNMP,合理的搭建也是必须掌握的技能,下面就利用源码的方式详细介绍下LNMP环境Nginx+PHP+Mysql的详细搭建步骤:

版本说明:

Nginx:nginx-1.14.1(最新版本)

PHP:php-7.2.12(最新版本)

Mysql:mysql-5.7.24

 

一、编译环境的安装:GCC,make,C++编译器

yum -y install gcc automake autoconf libtool make
yum install gcc gcc-c++

二、类库的安装:PCRE库、Zlib库、openssl 

1.选定源码下载目录

可以是任何目录,建议目录为/usr/local/src

cd /usr/local/src

2.安装PCRE库,官方地址ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz
tar -zxvf pcre-8.42.tar.gz
cd pcre-8.42
./configure
make && make install

3.安装Zlib库,官方地址http://www.zlib.net/

cd /usr/local/src
wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install

4.安装openssl官方地址:https://www.openssl.org/source/

cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
tar -zxvf openssl-1.1.1a.tar.gz
cd openssl-1.1.1
./configure
make && make install

三、Nginx的安装

官方地址:http://nginx.org/en/download.html

1.下载Nginx官方稳定包并解压:

cd /usr/local/src
wget http://nginx.org/download/nginx-1.14.1.tar.gz
tar -zxvf nginx-1.14.1.tar.gz
cd nginx-1.14.1

2.添加nginx用户和用户组

groupadd -r nginx
useradd -r -g nginx nginx

3.配置nginx安装参数

./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_mp4_module  \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre=/usr/local/src/pcre-8.39 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.1.1

注意:当前所在目录为/usr/local/src/nginx-1.14.1

     –with-pcre=/usr/local/src/pcre-8.42 指的是pcre-8.42 的源码路径。

     –with-zlib=/usr/local/src/zlib-1.2.11 指的是zlib-1.2.11 的源码路径。

     –with-openssl=/usr/local/src/openssl-1.1.1 指的是openssl-1.1.1的源码路径。

4.编译安装

make && make install

5.启动

/usr/local/nginx/sbin/nginx

注意:若mkdir /var/tmp/nginx/client failed的提示,请手动创建:

mkdir -p /var/tmp/nginx/client

打开浏览器访问此服务器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功!

四、PHP安装

官方地址:http://www.php.net/downloads.php

1.添加C、C++编译器

yum -y install gcc gcc-c++ glibc

2.添PHP扩展依赖

yum -y install libmcrypt-devel mhash-devel libxslt-devel \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \
krb5 krb5-devel libidn libidn-devel openssl openssl-devel \
--setopt=protected_multilib=false zlib

3.下载PHP官方稳定包并解压:

cd /usr/local/src
wget http://php.net/distributions/php-7.2.12.tar.gz
tar -zvxf php-7.2.12.tar.gz
cd php-7.2.12

4.添加nginx用户和用户组

groupadd -r www
useradd -r -g www www

5.配置PHP安装参数

./configure --prefix=/usr/local/php  --enable-fpm \
--enable-mbstring --enable-pdo --with-curl --disable-debug  --disable-rpath \
--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-pdo-mysql --with-mysqli \
--with-gd --with-jpeg-dir --with-freetype-dir --enable-calendar

注意:不同PHP版本,配置参数有部分不同,仅供参考

6.编译安装

make && make install

7.为php-fpm提供配置文件

cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf

cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf

8.启动

/usr/local/php/sbin/php-fpm

验证是否启动成功(如果此命令输出有几个php-fpm进程就说明启动成功了)

ps aux | grep php-fpm

五、Nginx服务器解析PHP文件之Nginx.conf简易配置

1.编辑Nginx.conf

vim /usr/local/nginx/nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  127.0.0.1;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /var/local/www;
            index index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/local/www;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /var/local/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

注意:

  • 红色标记部分为修改部分;
  • 原文件目录存放默认位置为:/usr/local/nginx/html,现将文件存放目录修改为:/var/local/www;

2.重启Nginx服务器

/usr/loca/nginx/sbin/nginx -s reload

3.测试php文件

创建index.php文件

vi /var/local/www/index.php

4.输入内容,保存

<?php
    phpinfo();
?>

5.浏览器访问http://服务器ip/index.php,看到如下信息即表示成功。

六、Mysql安装

官方地址:https://dev.mysql.com/downloads/mysql/

1.下载源码包,并解压

wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
tar -zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

2.添加Mysql用户和用户组

groupadd mysql
useradd -r -g mysql mysql

3.复制

cp -r mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql

4.修改Mysql目录所属者

cd /usr/local/mysql
chown -R mysql:mysql ./

5.初始化,安装Mysql数据库,并生成临时密码(注意:版本不同可能格式不同)

bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

如图:

输入以上命令可能出现的问题:bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

原因:缺少numactl软件包

解决:安装numactl软件包

yum -y install numactl

再次执行初始化命令,即可成功;

6.配置my.cnf(简易配置)

vi /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

character_set_server=utf8
init_connect='SET NAMES utf8 lower_case_table_names = 1


[mysqld_safe]
log-error=/var/log/mysqld.log pid-file=/usr/local/mysql/data/mysqld.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

注意:红色为修改部分;

7.添加开机启动

cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld

8.修改   vim /etc/init.d/mysqld 

vim /etc/init.d/mysqld

在46行 修改代码:

basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

9.启动mysql服务

service mysqld start

10.通过以下脚本可实现对nginx服务的管理

/etc/init.d/mysqld start
/etc/init.d/mysqld stop
/etc/init.d/mysqld restart
/etc/init.d/mysqld reload
/etc/init.d/mysqld status
11.将mysqld服务加入chkconfig管理列表:
chkconfig --add /etc/init.d/mysqld

12.设置终端模式开机启动:

chkconfig nginx on

13.执行完上述命令后,就可以使用service对nginx服务的管理

service nginx start
service nginx stop
service nginx restart
service nginx reload
service nginx status

14.根据生成的原始密码,登陆Mysql

mysql -u root -p

输入以上登陆Mysql命令可能出现的问题:-bash: mysql: command not found

解决:添加软连接

ln -s /usr/local/mysql/bin/mysql /usr/bin

15.修改原密码为root,并刷新权限(注意:要在登陆Mysql情况下修改)

alter user 'root'@'localhost' identified by 'root';  

flush privileges; //刷新权限

至此全部成功安装了😊

七、设置nginx和php-fpm的开机自启动

具体实现步骤,请点击查看:

  1.设置nginx的开机自启动方法

  2.设置php-fpm的开机自启动方法

八、常见问题解决办法

1.CentOS下如何完全卸载php文件

注意:当我们使用命令:yum remove php,是无法完全卸载php文件的,输入查看php版本命令:php -v,还是可以看到php版本。

正确做法如下:(强制删除全部php软件包)

a.查看全部php软件包

rpm -qa|grep php

php-cli-5.3.3-22.el6.x86_64
php-pdo-5.3.3-22.el6.x86_64
php-gd-5.3.3-22.el6.x86_64
php-fpm-5.3.3-22.el6.x86_64
php-common-5.3.3-22.el6.x86_64
php-5.3.3-22.el6.x86_64
php-xml-5.3.3-22.el6.x86_64
php-pear-1.9.4-4.el6.noarch

b.以此卸载软件包(注意:卸载要先卸载没有依赖的)

rpm -e php-fpm-5.3.3-22.el6.x86_64
rpm-e php-pdo-5.3.3-22.el6.x86_64
rpm -e php-pear-1.9.4-4.el6.noarch
rpm-e php-cli-5.3.3-22.el6.x86_64
rpm -e php-5.3.3-22.el6.x86_64
rpm-e php-xml-5.3.3-22.el6.x86_64
rpm -e php-gd-5.3.3-22.el6.x86_64
rpm-e php-common-5.3.3-22.el6.x86_64

再次输入查看php版本命令:php -v,版本信息已经没有提示,卸载成功。