LNMP安装
目录
1 编译安装 Nginx
1.1 解决依赖
apt-get install -y zlib1g-dev libpcre3-dev libssl-dev gcc make
1.2 创建用户
groupadd --system nginx
useradd --system -g nginx -s /sbin/nologin -c "Nginx web server" nginx
1.3 下载Nginx
wget http://nginx.org/download/nginx-1.24.0.tar.gz
1.4 解压
tar zxf nginx-1.24.0.tar.gz
cd nginx-1.24.0
1.5 生成编译配置
./configure \
--prefix=/usr/local/nginx \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module
1.6 编译安装
make && make install
1.7 配置 systemctl 管理
创建nginx.service
文件并插入以下内容
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
1.8 修改 Nginx 进程的运行用户
user nginx;
1.9 修改 nginx 目录权限
chown -R nginx:nginx /usr/local/nginx
1.10 启动 nginx
systemctl start nginx.service
2 二进制安装 MySQL
2.1 安装依赖
apt-get install libncurses5
2.2 创建用户
创建 mysql 的用户组和用户
groupadd --system mysql
useradd --system -g mysql -s /sbin/nologin -c "MySQL Server" mysql
2.3 下载二进制包并解压
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
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
2.4 创建数据目录
在 mysql 下面创建数据库文件目录
mkdir -p /usr/local/mysql/{data,logs}
chown -R mysql:mysql /usr/local/mysql
2.5 配置 my.cnf
vi /usr/local/mysql/my.cnf
chown mysql:mysql /usr/local/mysql/my.cnf
根据需要增减配置
[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
2.5.1 配置环境变量
将MySQL命令添加环境变量
#set mysql environment
export MYSQL_HOME=/usr/local/mysql
export PATH=${PATH}:${MYSQL_HOME}/bin
source /etc/profile
2.6 初始化 mysql
mysqld --defaults-file=/usr/local/mysql/my.cnf --initialize
在 log-error 中可以看到临时密码
grep 'temporary password' /usr/local/mysql/logs/error.log
2.7 配置服务并启动
配置服务,使用systemctl
管理
vi /usr/lib/systemd/system/mysql.service
[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
2.8 启动
systemctl start mysql.service
3 编译安装 PHP
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
3.1 下载
wget https://www.php.net/distributions/php-8.3.11.tar.gz
3.2 解压
tar -xzf php-8.3.11.tar.gz
cd php-8.3.11
3.3 安装依赖
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
3.4 生成配置文件
./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
正常如下图显示
3.5 编译安装
make -j4
make install
配置环境变量
echo 'export PHP_HOME=/usr/local/php' >> /etc/profile
echo 'export PATH=${PATH}:${PHP_HOME}/bin' >> /etc/profile
source /etc/profile
3.6 配置 php 配置文件
# 复制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
3.7 php-fpm 服务化
3.7.1 修改 php-fpm.conf
vi /usr/local/php/etc/php-fpm.conf
找到 pid 并修改
pid = run/php-fpm.pid
3.7.2 修改 www.conf
vi /usr/local/php/etc/php-fpm.d/www.conf
修改用户和组,改为和 nginx 相同用户
# 修改用户和组,改为和 nginx 相同用户
user = nginx
group = nginx
# 修改监听
listen = /var/run/php-fpm.sock
listen.owner = nginx
listen.group = nginx
3.7.3 创建 php-fpm 服务文件
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
3.7.4 启动 php-fpm 服务
systemctl start php-fpm.service
3.7.5 nginx 配置
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;
}
4 测试
修改 nginx.conf
include vhost/*.conf;
创建 vhost
mkdir /usr/local/nginx/conf/vhost
创建 default.conf
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;
}
}
创建虚拟主机目录并授权
mkdir /data/www/default
chown -R nginx:nginx /data/www
创建 php 测试文件
echo "<?php phpinfo(); ?>" > /data/www/default/index.php