在nix包管理里面配置mnmp环境

  • 配置phpfpm

# 安装php73
nix-env -i php-with-extensions-7.3.25
# 查看具体包文件位置
ls -l /Users/jcleng/.nix-profile/bin/php-fpm
# 通过ls -l找到具体的php-fpm.conf.default文件位置
/nix/store/mhlr5lc5gnkiagqcwsx4n4mkm991gj2l-php-7.3.25/etc/php-fpm.conf.default
# 进入配置文件夹,对配置进行修改
cd /nix/store/mhlr5lc5gnkiagqcwsx4n4mkm991gj2l-php-7.3.25/etc/php-fpm.d
cp www.conf.default www.conf
  • 编辑fpm配置文件

code www.conf
# 修改监听地址,为未被占用的
listen = 127.0.0.1:9000
# 用户改为一个执行php脚本的用户,或使用当前用户运行,注释这两行代码即可
user = www
group = www
# 运行
sudo php-fpm -y /nix/store/mhlr5lc5gnkiagqcwsx4n4mkm991gj2l-php-7.3.25/etc/php-fpm.d/www.conf
# 查看进程
ps aux|grep fpm
netstat -an | grep 9000
# 全部端口占用
sudo lsof -i -P | grep -i "listen"
# 结束进程
sudo pkill php-fpm
  • 配置nginx

# 我是nixpkg安装的nginx(macos源码安装,编译时间比较长),查看配置是否异常
nginx -t
# 一般提示
# nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (2: No such file or directory)
sudo mkdir -p /var/cache/nginx
sudo chmod 777 /var/cache/nginx
# 重新指定错误日志位置
touch /tmp/nginx_error.log
nginx -e /tmp/nginx_error.log -t
# 运行
nginx -e /tmp/nginx_error.log
# 重启
nginx -s reload
  • nginx的配置原文(直接新增include文件夹)


#user  nobody;
worker_processes  1;

error_log  /tmp/conf_nginx_error.log;
error_log  /tmp/conf_nginx_error.log  notice;
error_log  /tmp/conf_nginx_error.log  info;

pid        /tmp/nginx.pid;


events {
    worker_connections  128;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    include /Volumes/D/nginxvhost/*.conf;

    #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  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        # 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;
    #    }
    #}

}
  • 文件创建配置文件/Volumes/D/nginxvhost/fatadmin.conf


server {
    listen       80;
    server_name  lxx.test.net;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        # root /Users/jcleng/work/promake/tpAdmin/public; #如果由静态文件指定php的根目录
        index index.php;
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=/$1  last;
        }
    }

    location ~ \.php$ {
            root /Users/jcleng/work/promake/tpAdmin/public; #指定php的根目录
            fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000
            fastcgi_index index.php;
            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;
    #}
}
  • 配置一个db管理站点

server {
    listen       80;
    server_name  lxx.db.net;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root /Volumes/D/work/dbninja; #指定php的根目录
        index index.php;
    }

    location ~ \.php$ {
            root /Volumes/D/work/dbninja; #指定php的根目录
            fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000
            fastcgi_index index.php;
            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;
    #}
}
  • wsl配置fastadmin

server {
    listen       8070;
    server_name  lxx.test.net;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;


    location / {
            # index2.php 是加密的入口文件
            # http://lxx.test.net:8070/index2.php/index/login?url=%2Findex2.php
            # wsl在主配置文件http{}里面添加
            # fastcgi_buffering off;
            # include /mnt/f/wslnginx.conf/*.conf;

            root /mnt/f/runbox/centos7/work/fastadmin/public;
            if (!-e $request_filename) {
                rewrite ^(.+?\.php)(/.+)$ /index2.php?s=$2 last;
                rewrite ^(.*)$ /index.php?s=$1 last;
                break;
            }
    }
    location ~ \.php$ {
            root /mnt/f/runbox/centos7/work/fastadmin/public;
            fastcgi_pass unix:/tmp/fpm-cgi71.sock;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  • 数据库 请查看文章: https://gitee.com/jcleng/blog/blob/master/jcleng.gitee.io/mysql(mariadb)%E9%85%8D%E7%BD%AE%E4%B8%BB%E4%BB%8E.md

nix-env -iA nixpkgs.mariadb nixpkgs.mariadb-client

# 查看配置文件
mysqld --verbose --help |grep -A 1 'Default options'

# 初始化权限
# 进入
cd /nix/store/pxpc4b49rj335yqi027ch1hrsnq6myfl-mariadb-10.5.8/

# 初始化,只能在软件的根目录执行
sudo mkdir data
sudo chmod 777 data/
# 参数
./scripts/mysql_install_db --basedir=/nix/store/pxpc4b49rj335yqi027ch1hrsnq6myfl-mariadb-10.5.8 --datadir=/nix/store/pxpc4b49rj335yqi027ch1hrsnq6myfl-mariadb-10.5.8/data

# 提示如下,即可
# Installing MariaDB/MySQL system tables in './data' ...
# OK

# 打印基本信息,显示配置文件位置
my_print_defaults

# 修改配置信息如文章最后
touch ~/.my.cnf
vi ~/.my.cnf

# 运行一次,查看是否有错误
mysqld
# 或者指定data目录
chmod 777 /nix/store/pxpc4b49rj335yqi027ch1hrsnq6myfl-mariadb-10.5.8/data
mysqld --datadir=/nix/store/pxpc4b49rj335yqi027ch1hrsnq6myfl-mariadb-10.5.8/data

# 杀死进程
sudo pkill mysql
sudo pkill maria

# 后台运行
mysqld_safe &

# 重置密码
## 通过socket直接进入数据库
mysql
# 进入mysql控制台之后输入
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
exit;

# 密码登录
mysql -u root -p


# my.cnf文件
[client]
port            = 3306
socket          = /tmp/mariadb1058.sock

[mysqld]
port            = 3306
bind-address    = 0.0.0.0
socket          = /tmp/mariadb1058.sock
server_id       = 1
datadir =  /nix/store/pxpc4b49rj335yqi027ch1hrsnq6myfl-mariadb-10.5.8/data
#slow_query_log  = 1
#慢查询时间 超过1秒则为慢查询
#long_query_time = 2
#slow_query_log_file = /tmp/mariadb1058slow.log
# user=mysql
[mysqld_safe]
# log-error=/tmp/mariadb1058.log
# pid-file=/tmp/mariadb1058.pid
  • 查看进程

ps aux|grep php
jcleng            3025   0.0  0.0  4268284    684 s000  S+   10:33PM   0:00.00 grep --color=auto php
_www              2671   0.0  0.1  4512012   7004   ??  S    10:28PM   0:00.04 /Users/jcleng/.nix-profile/bin/php-fpm -y /nix/store/mhlr5lc5gnkiagqcwsx4n4mkm991gj2l-php-7.3.25/etc/php-fpm.d/www.conf
_www              2670   0.0  0.1  4512012   7712   ??  S    10:28PM   0:00.04 /Users/jcleng/.nix-profile/bin/php-fpm -y /nix/store/mhlr5lc5gnkiagqcwsx4n4mkm991gj2l-php-7.3.25/etc/php-fpm.d/www.conf
root              2669   0.0  0.0  4511868   1404   ??  Ss   10:28PM   0:00.02 /Users/jcleng/.nix-profile/bin/php-fpm -y /nix/store/mhlr5lc5gnkiagqcwsx4n4mkm991gj2l-php-7.3.25/etc/php-fpm.d/www.conf


ps aux|grep nginx
jcleng            3070   0.0  0.0  4277500    692 s000  R+   10:33PM   0:00.00 grep --color=auto nginx
jcleng            2708   0.0  0.0  4323080   1404   ??  S    10:28PM   0:00.02 nginx: worker process
jcleng            2707   0.0  0.0  4323080    544   ??  Ss   10:28PM   0:00.00 nginx: master process nginx -e /tmp/nginx_error.log -c /nix/store/inx46l401dkcarxamiy9xvrf5mr2x475-nginx-1.19.6/conf/nginx.conf


ps aux|grep mariadb
jcleng            3092   0.0  0.0  4268284    684 s000  S+   10:33PM   0:00.00 grep --color=auto mariadb
jcleng            2543   0.0  0.7  4814692  58624   ??  S    10:26PM   0:00.37 /Users/jcleng/.nix-profile/bin/mysqld --basedir=/Users/jcleng/.nix-profile --datadir=/nix/store/ids2k0pf0z9jllzmq69q6gky6zmaync2-mariadb-server-10.4.15/data --plugin-dir=/Users/jcleng/.nix-profile/lib/mysql/plugin --log-error=/nix/store/ids2k0pf0z9jllzmq69q6gky6zmaync2-mariadb-server-10.4.15/data/bogon.err --pid-file=bogon.pid --socket=/tmp/mariadb1058.sock --port=3306
  • pgsql

nix-env -i postgresql-11.10

cd /nix/store/2lwzc9n41k96csghw0mmjn3fxr4jx20a-postgresql-11.10/

sudo mkdir data
sudo chmod 0750 data/
## 文件夹设置为当前用户权限
sudo chown -R jcleng:jcleng data
# 查看配置信息
pg_config
# 初始化数据
initdb --username=jcleng --encoding=UTF-8 --no-locale --pgdata=/nix/store/2lwzc9n41k96csghw0mmjn3fxr4jx20a-postgresql-11.10/data

# 提示即可
Success. You can now start the database server using:
/home/jcleng/.nix-profile/bin/pg_ctl -D /nix/store/2lwzc9n41k96csghw0mmjn3fxr4jx20a-postgresql-11.10/data -l logfile start

# 在data配置文件(https://github.com/postgres/postgres/blob/master/src/backend/utils/misc/postgresql.conf.sample)
listen_addresses = '*'

# 直接运行
postgres -D /nix/store/2lwzc9n41k96csghw0mmjn3fxr4jx20a-postgresql-11.10/data -k /tmp

# 如果提示: Unable to connect to PostgreSQL server: FATAL:  no pg_hba.conf entry for host "192.168.1.116", user "jcleng", database "postgres" ... 是因为非本地不可连接
# 配置pg_hba.conf文件允许所有ip连接, 增加一行: host all all 0.0.0.0/0 trust

# 重启用pg_ctl,pkill会出现异常
pg_ctl restart -D /nix/store/2lwzc9n41k96csghw0mmjn3fxr4jx20a-postgresql-11.10/data
pg_ctl stop -D /nix/store/2lwzc9n41k96csghw0mmjn3fxr4jx20a-postgresql-11.10/data

# 用户名: jcleng
# 密码: root

# 连接
psql --host=/tmp/ --dbname='postgres' --user=jcleng
psql --host=127.0.0.1 --dbname='postgres' --user=jcleng
  • mongodb

# 安装
nix-env -iA nixpkgs.mongodb

# 创建配置文件
cd /nix/store/6jg1j1ynkm578j740wq44n7m8bw5s5ja-mongodb-3.4.24
mkdir conf
mkdir mydata

cd conf
touch mongodb.conf
# 文件原文

port=22345
bind_ip=0.0.0.0
logpath=/tmp/mongod.log
pidfilepath=/tmp/mongod.pid
logappend=true
## 数据库位置
dbpath=/nix/store/6jg1j1ynkm578j740wq44n7m8bw5s5ja-mongodb-3.4.24/mydata
## 后台运行
fork=true

# 运行
mongod -f /nix/store/6jg1j1ynkm578j740wq44n7m8bw5s5ja-mongodb-3.4.24/conf/mongodb.conf
# 检查
ps aux|grep mongo
# 连接
mongo --port 22345 --host 127.0.0.1
  • 其他

# 开发安装不同版本php,除了`nix-env --set-flag priority NUMBER INSTALLED_PKGNAME`命令之外,还可以使用nix-shell配合自己的环境来使用,文档: https://nixos.wiki/wiki/Development_environment_with_nix-shell

# 如:我安装了nginx,然后创建nix_httpd.nix文件搭建apache环境,注意`nix-collect-garbage`会清空该环境
/home/jcleng/nix.shell/nix_httpd.nix
# 或者直接对default.nix进行使用,如: https://github.com/jcleng/nix-dev-jcleng-php72-darwin/blob/edf616ebcd03bb42de52335afd678f0cc0025f1f/default.nix

# 文件原文
{ pkgs ? import <nixpkgs> {} }:
  pkgs.mkShell {
    # * nixpkgs.apacheHttpd (apache-httpd)
    # Apache HTTPD, the world's most popular web server
    # * nixpkgs.git (git-2.31.1)
    # Distributed version control system
    nativeBuildInputs = [ pkgs.buildPackages.apacheHttpd pkgs.git ];
}


# 进入环境
nix-shell nix_httpd.nix
# 就可以使用ab命令了,exit退出