1. 程式人生 > >CentOS7.X安裝LMMP環境Nginx+PHP+Mysql詳解

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
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
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
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的安裝

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安裝

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安裝

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; //重新整理許可權

至此全部成功安裝了