LNMP安装

bash

apt-get install -y zlib1g-dev libpcre3-dev libssl-dev gcc make

bash

groupadd --system nginx
useradd --system -g nginx -s /sbin/nologin -c "Nginx web server" nginx

bash

wget http://nginx.org/download/nginx-1.24.0.tar.gz

bash

tar zxf nginx-1.24.0.tar.gz
cd nginx-1.24.0

bash

./configure \
--prefix=/usr/local/nginx \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module

bash

make && make install

创建nginx.service文件并插入以下内容

bash

cat > /usr/lib/systemd/system/nginx.service <<'EOF'
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
WorkingDirectory=/usr/local/nginx
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

text

user  nginx;

bash

chown -R nginx:nginx /usr/local/nginx

bash

systemctl start nginx.service

bash

apt-get install libncurses5

创建 mysql 的用户组和用户

bash

groupadd --system mysql
useradd --system -g mysql -s /sbin/nologin -c "MySQL Server" mysql

bash

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz

解压缩到 /usr/local/ 下面,mysql 的主目录命名为 mysql

bash

tar zxf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.32-linux-glibc2.12-x86_64 /usr/local/mysql

在 mysql 下面创建数据库文件目录

bash

mkdir -p /usr/local/mysql/{data,logs}
chown -R mysql:mysql /usr/local/mysql

bash

vi /usr/local/mysql/my.cnf
chown mysql:mysql /usr/local/mysql/my.cnf

根据需要增减配置

text

[mysqld]
# dir
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
log-error = /usr/local/mysql/logs/error.log

# server
user = mysql
port = 3306
skip-ssl
skip-name-resolve
character_set_server = utf8mb4
collation_server = utf8mb4_bin
default-time-zone = '+08:00'
default-storage-engine = INNODB

将MySQL命令添加环境变量

bash

#set mysql environment
export MYSQL_HOME=/usr/local/mysql
export PATH=${PATH}:${MYSQL_HOME}/bin

source /etc/profile

shell

mysqld --defaults-file=/usr/local/mysql/my.cnf --initialize

在 log-error 中可以看到临时密码

bash

grep 'temporary password' /usr/local/mysql/logs/error.log

配置服务,使用systemctl管理

shell

vi /usr/lib/systemd/system/mysql.service

text

[Unit]
Description=MySQL Server
Documentation=man:mysqld(5.7)
Documentation=https://dev.mysql.com/doc/refman/5.7/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target
Alias=mysql.service

[Service]
User=mysql
Group=mysql
Type=forking
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=5
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf --daemonize
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false

shell

systemctl start mysql.service

PHP 7.0 需要 OpenSSL >= 0.9.8。PHP 7.1-8.0 需要 OpenSSL >= 1.0.1,< 3.0。PHP >= 8.1 支持 OpenSSL >= 1.0.2,< 4.0。

Ubuntu22 openssl 版本是 3.0,所以我下载 php8

bash

wget https://www.php.net/distributions/php-8.3.11.tar.gz

bash

tar -xzf php-8.3.11.tar.gz
cd php-8.3.11

bash

apt-get install -y pkg-config libtool libssl-dev libcurl4-openssl-dev libpng-dev libjpeg-dev libfreetype-dev libsqlite3-dev libxml2-dev libonig-dev libzip-dev libzip4 zlib1g-dev

bash

./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-jpeg \
--with-freetype \
--with-zlib \
--with-curl \
--enable-mbstring \
--with-openssl \
--with-mysqli \
--with-pdo-mysql \
--enable-gd \
--enable-bcmath \
--enable-sockets \
--enable-exif \
--with-zip

正常如下图显示

image-20240920142431561

bash

make -j4
make install

配置环境变量

bash

echo 'export PHP_HOME=/usr/local/php' >> /etc/profile
echo 'export PATH=${PATH}:${PHP_HOME}/bin'  >> /etc/profile
source /etc/profile

bash

# 复制php配置文件的位置,并修改名称
cp $PHP_HOME/etc/php-fpm.conf.default $PHP_HOME/etc/php-fpm.conf
cp $PHP_HOME/etc/php-fpm.d/www.conf.default $PHP_HOME/etc/php-fpm.d/www.conf
# 复制php.ini文件
cp /root/php-8.3.11/php.ini-production $PHP_HOME/etc/php.ini

bash

vi /usr/local/php/etc/php-fpm.conf

找到 pid 并修改

bash

pid = run/php-fpm.pid

text

vi /usr/local/php/etc/php-fpm.d/www.conf

修改用户和组,改为和 nginx 相同用户

text

# 修改用户和组,改为和 nginx 相同用户
user = nginx
group = nginx
# 修改监听
listen = /var/run/php-fpm.sock
listen.owner = nginx
listen.group = nginx

bash

cat > /usr/lib/systemd/system/php-fpm.service <<'EOF'
[Unit]
Description=The PHP FastCGI Process Manager
Documentation=http://php.net/docs.php
After=network.target

[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
EOF

bash

systemctl start php-fpm.service

nginx 中添加如下配置即可解析 php 页面

location ~ \.php$ {
    #fastcgi_pass   127.0.0.1:9000;
    fastcgi_pass   unix:/var/run/php-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

修改 nginx.conf

text

include vhost/*.conf;

创建 vhost

bash

mkdir /usr/local/nginx/conf/vhost

创建 default.conf

text

server {
  listen 80;
  server_name localhost;
  root /data/www/default;
  index index.php index.html index.htm;

  location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
    fastcgi_index index.php;
  }
}

创建虚拟主机目录并授权

bash

mkdir /data/www/default
chown -R nginx:nginx /data/www

创建 php 测试文件

bash

echo "<?php phpinfo(); ?>" > /data/www/default/index.php