LNMP部署
LNMP:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Mysql是一个小型关系型数据库管理系统。PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
nginx安装
nginx软件包 下载
# 创建用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
# 安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@localhost ~]# yum -y groups mark install 'Development Tools'
# 创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
# 解压安装包
[root@localhost src]# tar xf nginx-1.20.1.tar.gz
[root@localhost src]# ls
debug kernels nginx-1.20.1 nginx-1.20.1.tar.gz
# 编译安装
[root@localhost src]# tar xf nginx-1.20.1.tar.gz
[root@localhost src]# ls
debug kernels nginx-1.20.1 nginx-1.20.1.tar.gz
[root@localhost src]# cd nginx-1.20.1/
[root@localhost nginx-1.20.1]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.20.1]# make && make install
# 配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# source /etc/profile.d/nginx.sh
#设置开机自启
[root@localhost ~]# cat > /usr/lib/systemd/system/nginx.service <<EOF
> [Unit]
> Description=nginx
> After=network.target
>
> [Service]
> Type=forking
> ExecStart=/usr/local/nginx/sbin/nginx
> ExecReload=/usr/local/nginx/sbin/nginx -s reload
> ExecStop=/usr/local/nginx/sbin/nginx -s quit
> PrivateTmp=true
>
> [Install]
> WantedBy=multi-user.target
> EOF
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
mysql安装
mysql软件包下载
# 安装mysql所需依赖包
[root@localhost ~]# yum -y install ncurses-compat-libs
# 创建mysql用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
# 解压软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
# 修改文件属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql/
# 添加环境变量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh
# 初始化数据库
[root@localhost ~]# mysqld --initialize-insecure --user mysql --datadir /opt/data
2021-09-23T08:45:10.561076Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-09-23T08:45:10.725149Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-09-23T08:45:10.750948Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-09-23T08:45:10.814212Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 8fd8ab0f-1c4a-11ec-863d-000c297ee75a.
2021-09-23T08:45:10.814904Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-09-23T08:45:11.279071Z 0 [Warning] CA certificate ca.pem is self signed.
2021-09-23T08:45:11.611950Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
#生成配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
# 设置开机自启
[root@localhost ~]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=Mysql server daemon
After=network.targe
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.serve stop
[Install]
WantedBy=multi-user.target
[root@localhost ~]# sed -i 's!^basedir=!basedir=/usr/local/mysql!g;s!^datadir=!datadir=/opt/data!g' /usr/local/mysql/support-files/mysql.server
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
# 修改myql数据库密码
[root@localhost ~]# mysql -uroot -e "set password=password('123')"
安装php
php软件包下载
#安装epel源
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel libzip-devel
#下载依赖软件
[root@localhost ~]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
# 解压软件包
[root@localhost src]# tar xf php-8.0.10.tar.gz
#编译安装
[root@localhost php-8.0.10]# ./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-fpm --disable-debug --disable-rpath --enable-shared --enable-soap --with-openssl --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --enable-exif --enable-ftp --enable-gd --with-jpeg --with-zlib-dir --with-freetype --with-gettext --enable-mbstring --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-readline --enable-shmop --enable-simplexml --enable-sockets --with-zip --enable-mysqlnd-compression-support --with-pear --enable-pcntl --enable-posix
[root@localhost php-8.0.10]# make && make install
# 设置环境变量
[root@localhost ~]# echo "export PATH=/usr/local/php/bin:$PATH" > /etc/profile.d/php.sh
[root@localhost ~]# source /etc/profile.d/php.sh
[root@localhost ~]# which php
/usr/local/php/bin/php
[root@localhost php-8.0.10]# cp /etc/php.ini /opt/
[root@localhost php-8.0.10]# cp php.ini-production /etc/php.ini
cp:是否覆盖'/etc/php.ini'? y
[root@client php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@client php-8.0.10]# chmod +x /etc/rc.d/init.d/php-fpm
[root@client php-8.0.10]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@client php-8.0.10]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@client php-8.0.10]#
#设置开机自启
[root@localhost ~]#vim /usr/lib/systemd/system/php.service
[Unit]
Description=php-fpm server daemon
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP
[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now php
# 修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm; #添加index.php
}
#取消注释
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $Document_Root$fastcgi_script_name; #修改路径
include fastcgi_params;
}
#创建web网页文件
[root@localhost html]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
#重启nginx
[root@localhost ~]# systemctl restart nginx