本地LNMP开发环境搭建

本文最后由 森林生灵 于 2017/10/08 12:52:52 编辑

文章目录 (?) [+]

    本文仅适用于在 Deepin Linux amd64 15.4.1 系统下搭建本地 LNMP 测试环境,其它版本系统和生产环境请酌情参考。

            学习 PHP 的过程是避免不了与开发环境打交道的,之前在 Windows 系统时用的是 phpStudy 集成环境,图形化的界面和高度集成的傻瓜式操作,确实是方便简单了不少,但也因此让我忽视了环境配置。后来转到 Deepin 系统了,使用的也是使用一个集成开发环境 —— XAMPP,这次是在 Linux 环境了,配置环境不能再像 phpStudy 那样点点鼠标就行了,但由于前期在 Windows 上并没有注重这些,开始用的时候确实十分不顺手。又用过一段时间,发现几个 XAMPP 有许多小问题,比如不能编译插件,配置繁琐。中间有想换成 LNMP 的,可是搜索后发现 Linux 上并没有此类集成开发环境,想到服务器是用的一键 LNMP 安装包便想安装一下,但没成想这个一键安装包会卸载 DDE 桌面环境......终于被逼无奈的搭建了适合自己的本地 LNMP 开发环境,本文由此开始......


    LNMP


    # 我的平台信息
    choi@lanseyujie:~$ uname -a
    Linux lanseyujie 4.9.0-deepin10-amd64 #1 SMP PREEMPT Deepin 4.9.40-1 (2017-07-28) x86_64 GNU/Linux
    # 所有编译的源文件都在 ~/lnmp/
    # 网站根目录
    /home/choi/Project/wwwroot/default/
    # 数据库文件目录
    /data/mysql/
    # Nginx 和 PHP-FPM 用户和组都是 choi,MySQL 的用户和组都是 mysql

    Nginx

    编译安装

    # 到 https://nginx.org/en/download.html 下载最新稳定版
    cd ~ && mkdir lnmp && cd lnmp/
    wget -c https://nginx.org/download/nginx-1.12.1.tar.gz
    tar zxvf nginx-1.12.1.tar.gz && cd nginx-1.12.1/
    # 安装依赖
    sudo apt install libpcre3 libpcre3-dev openssl libssl-dev
    # 检查配置
    ./configure --user=choi --group=choi --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module
    # 编译安装
    make
    sudo make install
    sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

    环境配置

    文件位置:/usr/local/nginx/conf/nginx.conf

    user choi choi;
    worker_processes  auto;
    pid /usr/local/nginx/logs/nginx.pid;
    worker_rlimit_nofile 51200;
    
    events {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
    
        sendfile on;
        tcp_nopush on;
        keepalive_timeout 60;
        tcp_nodelay on;
    
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_intercept_errors on;
    
        gzip on;
        gzip_min_length 1k;
        gzip_buffers 4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied expired no-cache no-store private auth;
        gzip_disable "MSIE [1-6]\.";
    
        server_tokens off;
        access_log off;
    
        server {
            listen       80;
            server_name  localhost;
    
            root   /home/choi/Project/wwwroot/default/;
            index  index.html index.htm index.php;
    
            location ~ [^/]\.php(/|$) {
                try_files $uri =404;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
            }
    
            location / {
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime on;
            }        
    
            location /nginx_status {
                stub_status on;
                access_log off;
            }
    
            location ~ /\. {
                deny all;
            }
    
            #access_log  logs/host.access.log  main;
        }
    }

    常用命令

    # 启动
    sudo nginx
    # 测试 nginx.conf
    sudo nginx -t
    # 重载
    sudo nginx -s reload
    # 停止
    sudo nginx -s stop

    环境信息

    nginx path prefix: "/usr/local/nginx"

    nginx binary file: "/usr/local/nginx/sbin/nginx"

    nginx modules path: "/usr/local/nginx/modules"

    nginx configuration prefix: "/usr/local/nginx/conf"

    nginx configuration file: "/usr/local/nginx/conf/nginx.conf"

    nginx pid file: "/usr/local/nginx/logs/nginx.pid"

    nginx error log file: "/usr/local/nginx/logs/error.log"

    nginx http access log file: "/usr/local/nginx/logs/access.log"

    nginx http client request body temporary files: "client_body_temp"

    nginx http proxy temporary files: "proxy_temp"

    nginx http fastcgi temporary files: "fastcgi_temp"

    nginx http uwsgi temporary files: "uwsgi_temp"

    nginx http scgi temporary files: "scgi_temp"

    PHP

    编译安装

    # 到 http://php.net/downloads.php 下载最新稳定版
    cd ~/lnmp/
    wget -c http://am1.php.net/distributions/php-7.1.8.tar.gz
    tar zxvf php-7.1.8.tar.gz && cd php-7.1.8/
    # 安装依赖
    sudo apt install autoconf libfreetype6-dev libmcrypt-dev libjpeg-dev libpng-dev libxml2 libxml2-dev curl libcurl4-gnutls-dev openssl libssl-dev
    # 检查配置
    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=choi --with-fpm-group=choi --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache
    # 编译安装(耗时约 20 分钟)
    make
    make test
    sudo make install

    参数解释

    --with-iconv-dir 启用 XMLRPC-EPI:iconv 支持

    --with-freetype-dir 指定 FreeType2 的安装目录

    --with-jpeg-dir 指定 libjpeg 的安装目录

    --with-png-dir 指定 libpng 的安装目录

    --with-zlib 用于 http 压缩传输

    --disable-rpath 禁用传递其他运行库搜索路径

    --enable-bcmath 公元前风格精度数学

    --enable-shmop --enable-sysvsem 支持 IPC 相关函数

    -enable-inline-optimization 优化线程

    --enable-mbregex 支持多字节正则表达式

    --enable-mbstring 支持多字节字符串处理

    --enable-intl 启用国际化支持

    --with-mcrypt mcrypt 算法扩展

    --enable-gd-native-ttf 启用 TureType 字符功能

    --with-mhash mhash 算法扩展

    --enable-pcntl 启用 pcntl 支持(仅限 CLI/CGI 模式)

    --with-gettext 包含 GNU gettext 支持

    常见错误

    # libjpeg-dev 缺失

    configure: error: jpeglib.h not found.

    # 解决方法

    sudo apt install libjpeg-dev

    # libxml2-dev 缺失

    configure: error: xml2-config not found.

    # 解决方法

    sudo apt install libxml2-dev

    # libcurl4-gnutls-dev 未安装或路径问题

    configure: error: Please reinstall the libcurl distribution -

        easy.h should be in <curl-dir>/include/curl/

    # 解决方法

    sudo apt install curl libcurl4-gnutls-dev
    sudo ln -s /usr/include/x86_64-linux-gnu/curl/ /usr/local/include/curl

    # libmcrypt-dev 缺失

    configure: error: mcrypt.h not found. Please reinstall libmcrypt.

    # 解决方法

    sudo apt install libmcrypt-dev

    常用命令

    # 启动
    sudo /usr/local/php/sbin/php-fpm
    # 停止
    sudo killall php-fpm

    环境配置

    cd /usr/local/php/etc/
    sudo cp php-fpm.conf.default php-fpm.conf
    
    cd /usr/local/php/etc/php-fpm.d/
    sudo cp www.conf.default www.conf
    
    sudo cp ~/lnmp/php-7.1.8/php.ini-development /usr/local/php/etc/php.ini
    
    # 使用 unix socket 优化并发
    sudo nano www.conf
    
    ;listen = 127.0.0.1:9000
    listen = /tmp/php-cgi.sock
    listen.owner = choi
    listen.group = choi
    listen.mode = 0660

    环境信息

    Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/

    Installing PHP CLI binary:        /usr/local/php/bin/

    Installing PHP CLI man page:      /usr/local/php/php/man/man1/

    Installing PHP FPM binary:        /usr/local/php/sbin/

    Installing PHP FPM defconfig:     /usr/local/php/etc/

    Installing PHP FPM man page:      /usr/local/php/php/man/man8/

    Installing PHP FPM status page:   /usr/local/php/php/php/fpm/

    Installing phpdbg binary:         /usr/local/php/bin/

    Installing phpdbg man page:       /usr/local/php/php/man/man1/

    Installing PHP CGI binary:        /usr/local/php/bin/

    Installing PHP CGI man page:      /usr/local/php/php/man/man1/

    Installing build environment:     /usr/local/php/lib/php/build/

    Installing header files:          /usr/local/php/include/php/

    Installing helper programs:       /usr/local/php/bin/

      program: phpize

      program: php-config

    Installing man pages:             /usr/local/php/php/man/man1/

      page: phpize.1

      page: php-config.1

    Installing PEAR environment:      /usr/local/php/lib/php/

    [PEAR] Archive_Tar    - installed: 1.4.3

    [PEAR] Console_Getopt - installed: 1.4.1

    [PEAR] Structures_Graph- installed: 1.1.1

    [PEAR] XML_Util       - installed: 1.4.2

    [PEAR] PEAR           - installed: 1.10.5

    Wrote PEAR system config file at: /usr/local/php/etc/pear.conf

    You may want to add: /usr/local/php/lib/php to your php.ini include_path

    /home/choi/lnmp/php-7.1.8/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin

    ln -s -f phar.phar /usr/local/php/bin/phar

    Installing PDO headers:           /usr/local/php/include/php/ext/pdo/

    MariaDB

    方法一

    参考文档 https://mariadb.com/kb/en/library/installing-mariadb-binary-tarballs/

    二级制文件安装

    # 到 https://downloads.mariadb.org 下载最新稳定版
    cd ~/lnmp/
    wget -c http://ftp.yz.yamagata-u.ac.jp/pub/dbms/mariadb//mariadb-10.2.9/bintar-linux-x86_64/mariadb-10.2.9-linux-x86_64.tar.gz
    tar zxvf mariadb-10.2.9-linux-x86_64.tar.gz
    cd mariadb-10.2.9-linux-x86_64/
    sudo cp -r mariadb-10.2.9-linux-x86_64/ /usr/local/mariadb

    环境配置

    cd /usr/local/mariadb/support-files/
    sudo cp my-small.cnf my.cnf
    mkdir ~/database
    # 基础配置
    sudo nano my.cnf
    
    [client]
    default-character-set = utf8
    [mysqld]
    datadir = /home/choi/database
    character-set-server = utf8
    
    # 初始化数据库
    cd /usr/local/mariadb/scripts
    ./scripts/mysql_install_db --defaults-file=/usr/local/mariadb/support-files/my.cnf
    
    # 环境变量
    echo -e '\n\nexport PATH=$PATH:/usr/local/mariadb/bin\n' >> /etc/profile && source /etc/profile

    常用命令

    # 修改 root 密码
    mysqladmin -uroot password '123456'
    
    # 启动
    mysqld_safe --defaults-file=/usr/local/mariadb/support-files/my.cnf &
    # 停止
    sudo killall mysqld

    环境信息

    Installing MariaDB/MySQL system tables in '/home/choi/database' ...

    OK


    To start mysqld at boot time you have to copy

    support-files/mysql.server to the right place for your system


    PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !

    To do so, start the server, then issue the following commands:


    './bin/mysqladmin' -u root password 'new-password'

    './bin/mysqladmin' -u root -h lanseyujie password 'new-password'


    Alternatively you can run:

    './bin/mysql_secure_installation'


    which will also give you the option of removing the test

    databases and anonymous user created by default.  This is

    strongly recommended for production servers.


    See the MariaDB Knowledgebase at http://mariadb.com/kb or the

    MySQL manual for more instructions.


    You can start the MariaDB daemon with:

    cd '.' ; ./bin/mysqld_safe --datadir='/home/choi/database'


    You can test the MariaDB daemon with mysql-test-run.pl

    cd './mysql-test' ; perl mysql-test-run.pl


    Please report any problems at http://mariadb.org/jira


    The latest information about MariaDB is available at http://mariadb.org/.

    You can find additional information about the MySQL part at:

    http://dev.mysql.com

    Consider joining MariaDB's strong and vibrant community:

    https://mariadb.org/get-involved/

    方法二

    此种方法耗时较长,不推荐。

    编译安装

    # 到 https://downloads.mariadb.org 下载最新稳定版
    cd ~/lnmp/
    wget -c http://mirrors.neusoft.edu.cn/mariadb//mariadb-10.2.8/source/mariadb-10.2.8.tar.gz
    tar zxvf mariadb-10.2.8.tar.gz && cd mariadb-10.2.8/
    # 安装依赖
    sudo apt install cmake autoconf bison libncurses5-dev
    # https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
    # 检查配置
    cmake \
    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  \
    -DMYSQL_DATADIR=/data/mysql  \
    -DSYSCONFDIR=/etc/mysql \
    -DMYSQL_USER=mysql \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
    -DWITH_MEMORY_STORAGE_ENGINE=1 \
    -DWITH_READLINE=1 \
    -DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
    -DMYSQL_TCP_PORT=3306 \
    -DENABLED_LOCAL_INFILE=1 \
    -DENABLE_DOWNLOADS=1 \
    -DWITH_PARTITION_STORAGE_ENGINE=1  \
    -DEXTRA_CHARSETS=all \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DWITH_DEBUG=0 \
    -DMYSQL_MAINTAINER_MODE=0 \
    -DWITH_SSL:STRING=bundled \
    -DWITH_ZLIB:STRING=bundled \
    -DWITHOUT_MROONGA_STORAGE_ENGINE=1
    # 编译安装(耗时约 1 小时)
    make
    sudo make install

    环境配置

    # 环境变量
    echo -e '\n\nexport PATH=$PATH:/usr/local/mysql/bin\n' >> /etc/profile && source /etc/profile
    # echo -e '\n\nexport PATH=$PATH:/usr/local/mysql/bin\n' >> ~/.bashrc && source ~/.bashrc
    
    # 权限配置
    sudo groupadd mysql
    sudo useradd -g mysql mysql
    sudo mkdir -p /data/mysql
    sudo mkdir /var/run/mysqld
    sudo mkdir /etc/mysql
    sudo chown -R mysql:mysql /data/mysql/
    sudo chown -R mysql:mysql /var/run/mysqld/
    
    # 基础配置
    sudo cp support-files/my-small.cnf /etc/mysql/my.cnf
    sudo nano /etc/mysql/my.cnf
    
    [client]
    default-character-set = utf8
    [mysqld]
    datadir = /data/mysql
    character-set-server = utf8
    
    # 初始化数据库
    cd /usr/local/mysql
    scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql

    常用命令

    # 修改 root 密码
    mysqladmin -uroot password '123456'
    # 或者
    mysql -uroot
    mysql> SET PASSWORD = PASSWORD('123456');
    # 启动
    mysqld_safe --defaults-file=/etc/mysql/my.cnf --user=mysql &
    # 停止
    sudo killall mysqld

    环境信息

    Installing MariaDB/MySQL system tables in '/data/mysql' ...

    OK

    To start mysqld at boot time you have to copy

    support-files/mysql.server to the right place for your system

    PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !

    To do so, start the server, then issue the following commands:

    '/usr/local/mysql/bin/mysqladmin' -u root password 'new-password'

    '/usr/local/mysql/bin/mysqladmin' -u root -h lanseyujie password 'new-password'

    Alternatively you can run:

    '/usr/local/mysql/bin/mysql_secure_installation'

    which will also give you the option of removing the test

    databases and anonymous user created by default.  This is

    strongly recommended for production servers.

    See the MariaDB Knowledgebase at http://mariadb.com/kb or the

    MySQL manual for more instructions.

    You can start the MariaDB daemon with:

    cd '/usr/local/mysql' ; /usr/local/mysql/bin/mysqld_safe --datadir='/data/mysql'

    You can test the MariaDB daemon with mysql-test-run.pl

    cd '/usr/local/mysql/mysql-test' ; perl mysql-test-run.pl

    Please report any problems at http://mariadb.org/jira

    The latest information about MariaDB is available at http://mariadb.org/.

    You can find additional information about the MySQL part at:

    http://dev.mysql.com

    Consider joining MariaDB's strong and vibrant community:

    https://mariadb.org/get-involved/

    PHPMyAdmin

    安装过程

    # 到 https://www.phpmyadmin.net/downloads/ 下载最新稳定版
    cd ~/lnmp/
    wget -c https://files.phpmyadmin.net/phpMyAdmin/4.7.3/phpMyAdmin-4.7.3-all-languages.tar.gz
    tar zxvf phpMyAdmin-4.7.3-all-languages.tar.gz
    mv phpMyAdmin-4.7.3-all-languages ~/Project/wwwroot/default/phpmyadmin

    环境配置

    cd ~/Project/wwwroot/default/phpmyadmin
    cp config.sample.inc.php config.inc.php
    
    nano config.inc.php
    
    $cfg['blowfish_secret'] = 'testkey2017'; // 密码短语必须设置
    $cfg['Servers'][$i]['auth_type'] = 'cookie';
    $cfg['Servers'][$i]['host'] = '127.0.0.1'; // 数据库采用方法二安装的需要修改此行,原来的 localhost 不能使用是因为这种方式使用 Unix 套接字,而默认的 Unix 套接字 /tmp/mysql.sock 并不存在


    本文标题:本地LNMP开发环境搭建
    本文链接:https://lanseyujie.com/post/build-local-lnmp-development-environment.html
    版权声明:本文使用「署名-非商业性使用-相同方式共享」创作共享协议,转载或使用请遵守署名协议。
    点赞 0 分享 0
    本文已有4条留言:
    • 头像
      倚竹聽雨  GNU/Linux x64GNU/Linux x64 Firefox 57.0Firefox 57.0
      发表于 2018-01-18 23:32:41 来自 加拿大 的朋友 回复该评论
    • 哇大佬也用deepin。
      • 头像
        森林生灵 [作者]  GNU/Linux x64GNU/Linux x64 Google Chrome 64.0.3282.99Google Chrome 64.0.3282.99
        发表于 2018-01-19 14:31:27  回复该评论
      • 呃,我不是大佬... Deepin 这么漂亮又特别适合开发,而且还有一个很棒的应用中心,桌面操作系统非它莫属啊,哈哈~
    • 头像
      Jigen  GNU/Linux x64GNU/Linux x64 Google Chrome 58.0.3029.110Google Chrome 58.0.3029.110
      发表于 2017-09-22 20:55:55 来自 山东省青岛市 的朋友 回复该评论
    • mariadb地址有问题,新地址:
      http://mirrors.neusoft.edu.cn/mariadb//mariadb-10.2.8/source/mariadb-10.2.8.tar.gz