Ubuntu PHP 运行环境安装说明

非云环境的 ubuntu 主机关闭 cloud-init

sudo systemctl disable cloud-init-local cloud-init cloud-config cloud-final &&\
sudo systemctl stop cloud-init-local cloud-init cloud-config cloud-final

sudo: xxx: command not found 解决方法

sudo vim /etc/sudoers

#在 secure_path 里添加你的命令路径
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/data/nginx/sbin:/data/mysql/bin:/data/php/bin:/data/php/sbin:/data/postgresql/bin"

安装依赖

sudo apt install gcc g++ make libpcre3-dev libssl-dev zlib1g-dev pkg-config libxml2-dev libsqlite3-dev libbz2-dev libcurl4-openssl-dev libpng-dev libonig-dev libedit-dev libzip-dev autoconf libc-ares-dev libzstd-dev libevent-dev zip libnuma-dev

安装 nginx

#configure
./configure --prefix=/data/nginx --user=admin1 --group=admin1 --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-pcre --with-zlib= --with-openssl=

#成功后提示
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/data/nginx"
  nginx binary file: "/data/nginx/sbin/nginx"
  nginx modules path: "/data/nginx/modules"
  nginx configuration prefix: "/data/nginx/conf"
  nginx configuration file: "/data/nginx/conf/nginx.conf"
  nginx pid file: "/data/nginx/logs/nginx.pid"
  nginx error log file: "/data/nginx/logs/error.log"
  nginx http access log file: "/data/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"

#make
make

#install
make install

#准备配置文件
cp /data/nginx/conf/nginx.conf /data/nginx/conf/nginx.conf.backup &&\
mkdir /data/nginx/conf/certs &&\
mkdir /data/nginx/conf/conf.d

#nginx.conf 配置
##################################################
user  admin1;
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;

    #localhost
    server {
        listen          80;
        listen          18080;
        server_name     localhost;
        charset         utf-8;

        set             $deployment_path  /data/wwwroot/localhost;
        root            $deployment_path;
        index           index.html index.php;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            root           $deployment_path;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    include /data/nginx/conf/conf.d/*.conf;
}
##################################################

#启动脚本
sudo vim /usr/lib/systemd/system/nginx.service
##################################################
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/After=network-online.target remote-fs.targe nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/data/nginx/logs/nginx.pid
ExecStart=/data/nginx/sbin/nginx -c /data/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target
##################################################

#设置环境变量
sudo vim /etc/profile
#添加
export PATH=$PATH:/data/nginx/sbin

#开机启动
sudo systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.

安装 mysql

#mysql 配置文件读取顺序
#Default options are read from the following files in the given order:
#/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf

#安装依赖
sudo apt install libaio1 libtinfo5

#创建 mysql 用户组和 mysql 用户
sudo groupadd mysql
sudo useradd -r -g mysql -s /bin/false mysql

#准备目录
cd /data
tar xvf mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
mv mysql-8.0.31-linux-glibc2.12-x86_64 mysql
cd mysql
mkdir mysql-files
sudo chown mysql:mysql mysql-files
sudo chmod 750 mysql-files

#准备配置文件
sudo vim /etc/my.cnf
##################################################
[mysqld]
port=3306

basedir="/data/mysql"

datadir="/data/mysql/data"

pid-file = mysql.pid

character-set-server=utf8mb4

collation-server = utf8mb4_0900_ai_ci
init_connect = 'SET collation_connection=utf8mb4_0900_ai_ci'
init_connect = 'SET NAMES utf8mb4_0900_ai_ci'

#default_authentication_plugin=mysql_native_password
default_authentication_plugin=caching_sha2_password

default-storage-engine=INNODB

sql-mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"

log-output=FILE
general-log=0
general_log_file="mysql.log"
slow-query-log=1
slow_query_log_file="mysql-slow.log"

# 慢查询记录
# 建议根据您的环境将 long_query_time 调短
# 通常建议设为 1-5 秒
# 这里设置超过 10 秒的查询才被记录为慢查询
long_query_time=10

log-error=mysql.err

log-bin=mysql-bin

relay_log=mysql-relay-bin

server-id=1
##################################################

# Create the default database and exit. Create a super user with empty password.
bin/mysqld --initialize-insecure --user=mysql

#TODO:了解这个作用
bin/mysql_ssl_rsa_setup

#设置环境变量
sudo vim /etc/profile
#添加
export PATH=$PATH:/data/mysql/bin

#启动脚本
sudo cp support-files/mysql.server /etc/init.d/mysql &&\
sudo chmod 755 /etc/init.d/mysql
#编辑 /etc/init.d/mysql
sudo vim /etc/init.d/mysql
#修改如下内容
basedir="/data/mysql"
datadir="/data/mysql/data"

#文件夹所有者变更为 mysql:mysql
sudo chown -R mysql:mysql /data/mysql

#启动 mysql
sudo /etc/init.d/mysql start

#修改 root 密码为 123456
bin/mysqladmin -u root -p password 123456
#提示输密码,这里我们初始化数据库的时候是空密码,所以直接回车即可。
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

#重启 mysql
sudo /etc/init.d/mysql restart

#开机启动
sudo systemctl enable mysql
mysql.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable mysql

安装 php

#配置
./configure --prefix=/data/php --exec-prefix=/data/php --enable-fpm --with-fpm-user=admin1 --with-fpm-group=admin1 --with-config-file-path=/data/php --with-config-file-scan-dir=/data/php/conf.d --enable-libgcc --with-openssl --with-zlib --with-bz2 --with-curl --enable-gd --enable-intl --enable-mbstring --with-mysqli --enable-pcntl --with-pdo-mysql --with-zlib-dir --with-libedit --with-readline --enable-sockets --with-zip --enable-mysqlnd --with-pear

#编译
make

#安装
make install

#成功后提示
Installing shared extensions:     /data/php/lib/php/extensions/no-debug-non-zts-20210902/
Installing PHP CLI binary:        /data/php/bin/
Installing PHP CLI man page:      /data/php/php/man/man1/
Installing PHP FPM binary:        /data/php/sbin/
Installing PHP FPM defconfig:     /data/php/etc/
Installing PHP FPM man page:      /data/php/php/man/man8/
Installing PHP FPM status page:   /data/php/php/php/fpm/
Installing phpdbg binary:         /data/php/bin/
Installing phpdbg man page:       /data/php/php/man/man1/
Installing PHP CGI binary:        /data/php/bin/
Installing PHP CGI man page:      /data/php/php/man/man1/
Installing build environment:     /data/php/lib/php/build/
Installing header files:          /data/php/include/php/
Installing helper programs:       /data/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /data/php/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /data/php/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.14
[PEAR] Console_Getopt - installed: 1.4.3
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.5
warning: pear/PEAR dependency package "pear/Archive_Tar" installed version 1.4.14 is not the recommended version 1.4.4
[PEAR] PEAR           - installed: 1.10.13
Wrote PEAR system config file at: /data/php/etc/pear.conf
You may want to add: /data/php/lib/php to your php.ini include_path
/data/software/php-8.1.13/build/shtool install -c ext/phar/phar.phar /data/php/bin/phar.phar
ln -s -f phar.phar /data/php/bin/phar
Installing PDO headers:           /data/php/include/php/ext/pdo/

#准备配置文件
cp php.ini-production /data/php/php.ini &&\
mkdir /data/php/conf.d &&\
cp /data/php/etc/php-fpm.conf.default /data/php/etc/php-fpm.conf &&\
cp /data/php/etc/php-fpm.d/www.conf.default /data/php/etc/php-fpm.d/www.conf &&\
sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm &&\
sudo chmod 755 /etc/init.d/php-fpm

#设置环境变量
sudo vim /etc/profile
#添加
export PATH=$PATH:/data/php/bin
export PATH=$PATH:/data/php/sbin

#开机启动
sudo systemctl enable php-fpm
php-fpm.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable php-fpm

安装 composer
wget https://getcomposer.org/download/2.4.4/composer.phar
sudo mv composer.phar /usr/local/bin/composer &&\
sudo chmod 755 /usr/local/bin/composer &&\
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

#安装 swoole 扩展
pecl install swoole

#成功后提示
Build process completed successfully
Installing '/data/php/lib/php/extensions/no-debug-non-zts-20210902/swoole.so'
Installing '/data/php/include/php/ext/swoole/config.h'
Installing '/data/php/include/php/ext/swoole/php_swoole.h'
install ok: channel://pecl.php.net/swoole-5.0.1
configuration option "php_ini" is not set to php.ini location
You should add "extension=swoole.so" to php.ini

#安装 igbinary 扩展
pecl install igbinary

#成功后提示
Build process completed successfully
Installing '/data/php/lib/php/extensions/no-debug-non-zts-20210902/igbinary.so'
Installing '/data/php/include/php/ext/igbinary/php_igbinary.h'
Installing '/data/php/include/php/ext/igbinary/igbinary.h'
Installing '/data/php/include/php/ext/igbinary/src/php7/php_igbinary.h'
Installing '/data/php/include/php/ext/igbinary/src/php7/igbinary.h'
install ok: channel://pecl.php.net/igbinary-3.2.12
configuration option "php_ini" is not set to php.ini location
You should add "extension=igbinary.so" to php.ini

#安装 redis 扩展
pecl install redis

#成功后提示
Build process completed successfully
Installing '/data/php/lib/php/extensions/no-debug-non-zts-20210902/redis.so'
install ok: channel://pecl.php.net/redis-5.3.7
configuration option "php_ini" is not set to php.ini location
You should add "extension=redis.so" to php.ini

#安装 event 扩展
pecl install event

#成功后提示
Build process completed successfully
Installing '/data/php/lib/php/extensions/no-debug-non-zts-20210902/event.so'
install ok: channel://pecl.php.net/event-3.0.8
configuration option "php_ini" is not set to php.ini location
You should add "extension=event.so" to php.ini

安装 postgresql

tar zxvf postgresql-15.1.tar.gz

#configure
./configure --prefix=/data/postgresql --with-openssl

#make
make

#install
make install

#初始化数据库
/data/postgresql/bin/initdb -D /data/postgresql/data -A password -U postgres -W

##################################################
The files belonging to this database system will be owned by user "admin1".
This user must also own the server process.

The database cluster will be initialized with locale "C.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

Enter new superuser password:
Enter it again:

creating directory /data/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:

    /data/postgresql/bin/pg_ctl -D /data/postgresql/data -l logfile start
##################################################

#修改文件夹所有者为 postgres
sudo chown -R admin1:admin1 /data/postgresql

#启动脚本
sudo vim /etc/systemd/system/postgresql.service

##################################################
[Unit]
Description=PostgreSQL Database Server
After=network.target

[Service]
Type=forking
User=admin1
Group=admin1
ExecStart=/data/postgresql/bin/pg_ctl -D /data/postgresql/data start
ExecStop=/data/postgresql/bin/pg_ctl -D /data/postgresql/data stop
ExecReload=/data/postgresql/bin/pg_ctl -D /data/postgresql/data reload
PIDFile=/data/postgresql/data/postmaster.pid

[Install]
WantedBy=multi-user.target
##################################################

# 重新加载 Systemd 配置
sudo systemctl daemon-reload

#启动服务
sudo systemctl start postgresql.service

#停止服务
sudo systemctl stop postgresql.service

# 开机启动
#sudo systemctl enable postgresql.service

#设置环境变量
sudo vim /etc/profile
#添加
# postgresql
export PATH=$PATH:/data/postgresql/bin

#登录
psql -p 5432 -U postgres -d postgres

安装 git

sudo apt install git &&\
git config --global user.name "773416080" &&\
git config --global user.email "773416080@qq.com"

安装 nodejs v18.x

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\
sudo apt-get install -y nodejs &&\
npm config set registry http://registry.npmmirror.com

生成 SSH 公钥

#以ED25519算法为例
ssh-keygen -t ed25519

#查看
cat ~/.ssh/id_ed25519.pub

将 admin1 用户添加到 mysql 用户组和 postgres 用户组

sudo usermod -a -G mysql admin1
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 4

为啥不直接使用 apt 安装全套呢,php、nginx 这些还编译安装

1年前 评论
mayingbiao89 (楼主) 1年前

docker 不是更好的选择吗?

1年前 评论
mayingbiao89 (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!