一、LNMP简介
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Mysql是一个小型关系型数据库管理系统。PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
二、LNMP的特性
Nginx是一个小巧而高效的Linux下的Web服务器软件,是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler 站点开发的,已经在一些俄罗斯的大型网站上运行多年,相当的稳定。Nginx性能稳定、功能丰富、运维简单、处理静态文件速度快且消耗系统资源极少。
三、安装nginx
// 关闭防火墙和selinux
[root@lnmp ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@lnmp ~]# systemctl stop --now firewalld
[root@lnmp ~]# vim /etc/selinux/config
SELINUX=disabled
[root@lnmp ~]# reboot
[root@lnmp ~]# setenforce 0
setenforce: SELinux is disabled
// 创建用户
[root@lnmp ~]# useradd -r -M -s /sbin/nologin nginx
[root@lnmp ~]# id nginx
uid=994(nginx) gid=991(nginx) groups=991(nginx)
// 安装epel源、依赖包、工具包(安装lnmp所有需要的依赖包都在里面)
[root@lnmp ~]# yum -y install epel-release
[root@lnmp ~]# 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@lnmp ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make ncurses-devel cmake mariadb-devel ncurses-compat-libs libxml2 libxml2-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 oniguruma libzip-devel
// 将之前下载好的包传上去,并解压
[root@lnmp ~]# cd /usr/src/
[root@lnmp src]# ls
debug
kernels
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
nginx-1.20.1.tar.gz
php-8.0.10.tar.gz
[root@lnmp src]# tar xf nginx-1.20.1.tar.gz -C /usr/local/
// 创建日志存放目录
[root@lnmp src]# mkdir -p /var/log/nginx
[root@lnmp src]# chown -R nginx.nginx /var/log/nginx/
// 编译安装
[root@lnmp 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 \
&& make && make install
// 设置环境变量
[root@lnmp ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lnmp ~]# . /etc/profile.d/nginx.sh
[root@lnmp ~]# nginx
[root@lnmp ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
四、mysql安装
// 创建用户
[root@lnmp ~]# useradd -r -M -s /sbin/nologin mysql
// 把下载好的包传到当前目录然后解压mysql包
[root@lnmp ~]# tar xf /usr/src/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
// 做软链接
[root@lnmp local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
[root@lnmp local]# chown -R mysql.mysql mysql/
// 头文字连接
[root@lnmp local]# ln -s /usr/local/mysql/include/ /usr/include/mysql/
// 库文件
[root@lnmp local]# vim /etc/ld.so.conf.d/mysql.conf
[root@lnmp local]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
// 创建数据存放目录
[root@lnmp local]# mkdir /opt/data
[root@lnmp local]# chown -R mysql.mysql /opt/data/
// 设置环境变量
[root@lnmp local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lnmp local]# . /etc/profile.d/mysql.sh
// 初始化
[root@lnmp local]# mysqld --initialize-insecure --user mysql --datadir /opt/data
// 编写配置文件
[root@lnmp local]# cat > /etc/my.cnf << EOF
> [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
> EOF
// 编写服务控制脚本
[root@lnmp local]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=Mysql server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/usr/local/mysql/support-files/mysql.server stop
[Install]
WantedBy=multi-user.target
basedir=/usr/local/mysql
datadir=/opt/data
// 启动mysql服务
[root@lnmp local]# systemctl daemon-reload
[root@lnmp local]# systemctl start mysqld
[root@lnmp local]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@lnmp local]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
五、php安装
// 把下载好的包传到当前目录然后解压
[root@lnmp ~]# tar xf /usr/src/php-8.0.10.tar.gz -C /usr/local/
[root@lnmp ~]# cd /usr/local/php-8.0.10/
// 编译安装
[root@lnmp php-8.0.10]# ./configure --prefix=/usr/local/php8 \
--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 \
&& make && make install
// 设置环境变量
[root@lnmp php-8.0.10]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@lnmp php-8.0.10]# . /etc/profile.d/php.sh
// 配置php
[root@lnmp php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@lnmp php-8.0.10]# chmod +x /etc/init.d/php-fpm
[root@lnmp php-8.0.10]# cd /usr/local/php8
[root@lnmp php8]# cd etc/
[root@lnmp etc]# cp php-fpm.conf.default php-fpm.conf
[root@lnmp etc]# cd php-fpm.d/
[root@lnmp php-fpm.d]# cp www.conf.default www.conf
[root@lnmp php-fpm.d]# ls
www.conf www.conf.default
// 启动服务
[root@lnmp php-fpm.d]# service php-fpm start
Starting php-fpm done
[root@lnmp php-fpm.d]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
六、配置nginx
// 配置网页文件
[root@lnmp ~]# cd /usr/local/nginx/html/
[root@lnmp html]# vim index.php
[root@lnmp html]# cat index.php
<?php
phpinfo();
?>
[root@lnmp html]# chown -R nginx.nginx index.php
//修改配置文件
[root@lnmp nginx]# vim conf/nginx.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
// 重新加载nginx
[root@lnmp nginx]# nginx -s reload
七、web页面访问
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/5602.html