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

发布时间:2018-11-28 17:04:04编辑:丝画阁阅读(244)


阅读目录

  • 一、编译环境的安装:GCC,make,C++编译器
  • 二、类库的安装:PCRE库、Zlib库、openssl
  • 三、Nginx的安装
  • 四、PHP安装
  • 五、Nginx服务器解析PHP文件之Nginx.conf简易配置
  • 六、Mysql安装
  • 七、设置nginx和php-fpm的开机自启动
  • 八、常见问题解决办法

前言:

作为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.输入内容,保存