Docker LNMP 请参考 https://github.com/lanseyujie/docker-lnmp
文中内容仅供在 Deepin Linux amd64 15.7 系统环境下搭建 测试 环境参考,其它版本系统和生产环境请酌情参考。
学习 PHP 的过程是避免不了与开发环境打交道的,之前在 Windows 系统时用的是 phpStudy 集成环境,图形化的界面和高度集成的傻瓜式操作,确实是方便简单了不少,但也因此让我忽视了环境配置。后来转到 Deepin 系统了,使用的也是使用一个集成开发环境 —— XAMPP,这次是在 Linux 环境了,配置环境不能再像 phpStudy 那样点点鼠标就行了,但由于前期在 Windows 上并没有注重这些,开始用的时候确实十分不顺手。又用过一段时间,发现几个 XAMPP 有许多小问题,比如不能编译插件,配置繁琐。中间有想换成 LNMP 的,可是搜索后发现 Linux 上并没有此类集成开发环境,想到服务器是用的一键 LNMP 安装包便想安装一下,但没成想这个一键安装包会卸载 DDE 桌面环境......终于被逼无奈的搭建了适合自己的本地 LNMP 开发环境,本文由此开始......
# 我的平台信息 choi@lanseyujie:~$ uname -a Linux lanseyujie 4.15.0-29deepin-generic #31 SMP Fri Jul 27 07:12:08 UTC 2018 x86_64 GNU/Linux # 当前用户根路径 $HOME # 所有编译的源文件都在 $HOME/lnmp # 网站根目录 $HOME/wwwroot # 默认站点路径 $HOME/wwwroot/default # Nginx 和 PHP-FPM 用户和组都是 choi,MySQL 的用户和组都是 mysql (如果编译安装的话),编译时需要根据实际情况更换用户名
Nginx
编译安装
# 到 https://nginx.org/en/download.html 下载主线版本 cd ~ && mkdir lnmp && cd lnmp wget -c http://nginx.org/download/nginx-1.15.2.tar.gz tar zxvf nginx-1.15.2.tar.gz && cd nginx-1.15.2 # 安装依赖 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 --with-stream --with-stream_ssl_module # 编译安装 make -j4 sudo make install sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
环境配置
文件位置:/usr/local/nginx/conf/nginx.conf,下载示例 nginx.conf 文件 https://www.lanseyujie.com/download/nginx.conf
user choi choi; worker_processes auto; error_log logs/error.log crit; pid 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; #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; 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; 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/wwwroot/default; index index.html index.htm index.php; #access_log logs/host.access.log main; location ~ [^/]\.php(/|$) { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; } #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 html; } location / { autoindex on; autoindex_exact_size off; autoindex_localtime on; } location /nginx_status { stub_status on; access_log off; } location ~ /\. { deny all; } } include vhost/*.conf; }
常用命令
# 启动 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.2.9.tar.gz tar zxvf php-7.2.9.tar.gz && cd php-7.2.9 # 安装依赖 sudo apt install g++ autoconf openssl libssl-dev libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev libxml2 libxml2-dev libxslt1-dev libzip-dev curl libcurl4-gnutls-dev # 查看可用的配置选项 ./configure -h # 检查配置 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-libdir=/lib/x86_64-linux-gnu --disable-rpath --enable-fpm --with-fpm-user=choi --with-fpm-group=choi --with-openssl --enable-bcmath --with-zlib --with-curl --enable-exif --enable-ftp --with-gd --with-webp-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr --with-freetype-dir=/usr --with-gettext --with-mhash --enable-intl --enable-mbstring --enable-mbregex --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --with-xmlrpc --with-libxml-dir=/usr --with-iconv-dir --with-xsl --enable-zip --with-libzip --enable-mysqlnd # 编译安装(耗时约 5 分钟) make -j4 sudo make install sudo ln -s /usr/local/php/sbin/php-fpm /usr/sbin/php-fpm
安装扩展
请参考 https://www.lanseyujie.com/post/php-extension-compile-and-installation.html
参数解释
--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-mbregex 支持多字节正则表达式
--enable-mbstring 支持多字节字符串处理
--enable-intl 启用国际化支持
--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
常用命令
# 启动 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 sudo cp ~/lnmp/php-7.2.9/php.ini-development /usr/local/php/etc/php.ini cd /usr/local/php/etc/php-fpm.d sudo cp www.conf.default www.conf # 使用 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 # 环境变量(root用户下执行) echo -e '\n\nexport PATH=$PATH:/usr/local/php/bin\n' >> /etc/profile && source /etc/profile
环境信息
/bin/bash /home/choi/lnmp/php-7.2.9/libtool --silent --preserve-dup-deps --mode=install cp ext/opcache/opcache.la /home/choi/lnmp/php-7.2.9/modules
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/
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.2.9/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 到最新稳定版页面右侧选择 Generic Linux cd ~/lnmp/ wget -c http://mirror.lstn.net/mariadb//mariadb-10.3.8/bintar-linux-x86_64/mariadb-10.3.8-linux-x86_64.tar.gz tar zxvf mariadb-10.3.8-linux-x86_64.tar.gz sudo mv mariadb-10.3.8-linux-x86_64 /usr/local/mariadb
环境配置
# 初始化数据库 cd /usr/local/mariadb ./scripts/mysql_install_db # 环境变量(root用户下执行) echo -e '\n\nexport PATH=$PATH:/usr/local/mariadb/bin\n' >> /etc/profile && source /etc/profile
环境信息
Installing MariaDB/MySQL system tables in './data' ...
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='./data'
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/
常用命令
# 启动 mysqld_safe >/dev/null & # 停止 sudo killall mysqld # 修改 root 密码 mysqladmin -uroot password '123456'
环境信息
数据库文件位置 /usr/local/mariadb/data
方法二
此种方法比较麻烦且耗时较长,不推荐。
编译安装
# 到 https://downloads.mariadb.org 到最新稳定版页面右侧选择 Source Code 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=utf8mb4 \ -DDEFAULT_COLLATION=utf8mb4_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.7/phpMyAdmin-4.7.7-all-languages.tar.gz tar zxvf phpMyAdmin-4.7.7-all-languages.tar.gz mv phpMyAdmin-4.7.7-all-languages /home/choi/wwwroot/default/phpmyadmin
环境配置
cd /home/choi/wwwroot/default/phpmyadmin cp config.sample.inc.php config.inc.php nano config.inc.php $cfg['blowfish_secret'] = md5('testkey'); // 32 位的密码短语必须设置 $cfg['Servers'][$i]['auth_type'] = 'cookie'; $cfg['Servers'][$i]['host'] = 'localhost'; // 数据库采用 方法二 安装的需要修改此行为 '127.0.0.1',原来的 localhost 不能使用是因为这种方式使用 Unix 套接字,而默认的 Unix 套接字 /tmp/mysql.sock 并不存在
Redis
安装过程
wget -c http://download.redis.io/releases/redis-5.0.0.tar.gz tar -zxvf redis-5.0.0.tar.gz && cd redis-5.0.0/ make cd src sudo make PREFIX=/usr/local/redis install # 修改权限以避免因权限问题而无法退出 sudo chown $USER:$USER -R /usr/local/redis
环境配置
sudo cp redis.conf /usr/local/redis/ cd /usr/local/redis/ sudo nano redis.conf # 或者在启动后使用 CONFIG SET 命令进行配置
常用命令
# 启动服务端 nohup redis-server /usr/local/redis/redis.conf >/dev/null 2>&1 & # 打开客户端 ./bin/redis-cli
常见问题
内存设置问题
现象
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
原因
0 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2 表示内核允许分配超过所有物理内存和交换空间总和的内存。
解决
# 临时解决 sudo su echo 1 > /proc/sys/vm/overcommit_memory # 永久解决(修改后需要重启生效) sudo su echo \"vm.overcommit_memory=1\" > /etc/sysctl.conf
透明大页问题
现象
WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
原因
内核使用透明大页可能会导致 redis 延迟和内存使用问题。
解决
# 临时解决 sudo su echo never > /sys/kernel/mm/transparent_hugepage/enabled # 永久解决(修改后需要重启生效) sudo su # 如果存在 /etc/rc.local 文件需要追加这一句 echo \"echo never > /sys/kernel/mm/transparent_hugepage/enabled\" >> /etc/rc.local # 如果不存在 /etc/rc.local 则需要创建添加修改 echo -e '#!/bin/sh \\n echo never > /sys/kernel/mm/transparent_hugepage/enabled' > rc.local
无法退出问题
现象
User requested shutdown...
Saving the final RDB snapshot before exiting.
Failed opening the RDB file dump.rdb (in server root dir /usr/local/redis) for saving: Permission denied
Error trying to save the DB, can't exit.
原因
redis 路径权限不够
解决
sudo chown $USER:$USER -R /usr/local/redis