当前位置: 雨过天晴 » 运维笔记 » Ubuntu 22.04 手动搭建 LNMP 环境

Ubuntu 22.04 手动搭建 LNMP 环境

本文介绍Ubuntu 22.04手动搭建LNMP环境并做基础配置。nginxphp使用Ondřej Surýppa源进行安装和升级,mysql使用Ubuntu自带的软件源进行安装。本文操作在root账户下进行,非root用户需要使用sudo提升执行权限。

安装NGINX

添加nginx软件源

稳定版本

apt install software-properties-common -y && add-apt-repository ppa:ondrej/nginx -y

主线版本

apt install software-properties-common -y && add-apt-repository ppa:ondrej/nginx-mainline -y

安装nginx

apt install nginx-extras -y

查看nginx版本号

nginx -v

查看服务运行状态

systemctl status nginx

设置nginx服务开机自启动

systemctl enable nginx

设置nginx日志格式

vim /etc/nginx/conf.d/main-log-format.conf

复制以下内容到 main-log-format.conf ,保存

log_format main '$http_x_forwarded_for - $remote_addr - [$time_local] "$request" ' '$status $upstream_response_time $request_time "$http_referer"' '"$http_user_agent" $body_bytes_sent ';

启用ip黑名单

cat >> /etc/nginx/blocklist.conf << EOF
#deny 1.1.1.1;
EOF

重启nginx服务

systemctl restart nginx

安装PHP

添加php软件源

apt install software-properties-common -y && add-apt-repository ppa:ondrej/php -y

安装php8.1及常用扩展

apt install php8.1 php8.1-fpm php8.1-curl php8.1-mbstring php8.1-ldap php8.1-tidy php8.1-xml php8.1-zip php8.1-gd php8.1-mysql -y

配置php

cat > /etc/php/8.1/fpm/conf.d/config_php.ini << EOF
expose_php              = Off  
error_reporting         = E_ALL & ~E_NOTICE  
display_errors          = Off  
display_startup_errors  = Off  
log_errors              = On  
error_log                = /var/log/php_errors.log
ignore_repeated_errors  = Off  

allow_url_fopen         = On  
allow_url_include       = Off  
variables_order         = "GPCS"  
allow_webdav_methods    = On  
memory_limit            = 128M  
max_execution_time      = 300  
output_buffering        = On  
output_handler          = ""  
zlib.output_compression = Off  
zlib.output_handler     = ""  
safe_mode               = Off  
register_globals        = Off  
magic_quotes_gpc        = Off  

file_uploads            = On
upload_max_filesize     = 50M  
post_max_size           = 50M  

enable_dl               = Off  
disable_functions       = ""  
disable_classes         = ""  
session.save_handler     = files  
session.use_cookies      = 1  
session.use_only_cookies = 1  
session.auto_start       = 0  
session.cookie_lifetime  = 0  
session.cookie_httponly  = 1  
session.save_path = "/var/lib/php/sessions"

opcache.enable=1
opcache.enable_cli=1
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=1

date.timezone            = "PRC"
EOF

重启php服务

systemctl restart php8.1-fpm.service

安装ionCube Loader模块(可选)

下载ionCube Loader扩展

wget https://downloads.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.zip && unzip ioncube_loaders_lin_x86-64.zip

ioncube_loader需与php版本相对应,这里选择php8.1版本的ioncube_loader。将ioncube_loader_lin_8.1.so复制到php8.1的扩展目录/usr/lib/php/20210902

cp ioncube/ioncube_loader_lin_8.1.so /usr/lib/php/20210902

添加ioncube_loader模块配置文件

cat >> /etc/php/8.1/mods-available/ioncube.ini << EOF
zend_extension = ioncube_loader_lin_8.1.so
EOF

ioncube_loader模块接入php-fpmphp-cli并重启php8.1-fpm

ln -s /etc/php/8.1/mods-available/ioncube.ini /etc/php/8.1/fpm/conf.d/01-ioncube.ini && \
ln -s /etc/php/8.1/mods-available/ioncube.ini /etc/php/8.1/cli/conf.d/01-ioncube.ini && \
systemctl restart php8.1-fpm.service

检查ioncube_loader模块是否生效

php -v

终端输出信息中包含with the ionCube PHP Loader v12.0.2, Copyright (c) 2002-2022, by ionCube Ltd.说明ionCube模块安装成功

PHP 8.1.12 (cli) (built: Oct 28 2022 17:39:57) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.12, Copyright (c) Zend Technologies
with the ionCube PHP Loader v12.0.2, Copyright (c) 2002-2022, by ionCube Ltd.
with Zend OPcache v8.1.12, Copyright (c), by Zend Technologies

删除临时文件

rm -rf ioncube ioncube_loaders_lin_x86-64.zip

安装MySQL

使用Ubuntu自带的软件源进行安装,本例安装mysql8.0

apt install -y mysql-server-8.0 -y

登录mysql控制台,密码默认为空

mysql -uroot -p

设置root用户密码,本文以设置root用户密码www.ygtq.cc为例

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'www.ygtq.cc';

使用quitexit命令退出控制台,初始化数据库

mysql_secure_installation

按终端提示进行设置,期间可以再次修改root密码

未经允许不得转载:雨过天晴 » Ubuntu 22.04 手动搭建 LNMP 环境

相关文章