LNMP
LAMP和LNMP的区别
lamp 的全称是linux + apache + mysql +php
lnmp 的全称是linux + nginx + mysql + php (国外喜欢简称为LEMP,搜英文资料需要搜LEMP)
注意:四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统
LNMP的特点
Nginx是一个小巧而高效的Linux下的Web服务器软件,是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler 站点开发的,已经在一些俄罗斯的大型网站上运行多年,相当的稳定。Nginx性能稳定、功能丰富、运维简单、处理静态文件速度快且消耗系统资源极少。
LNMP的优点
1.作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。
2.作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器对外进行服务。Nginx 用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好的多。
3.作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。
4.Nginx 安装非常的简单:配置文件非常简洁(还能够支持perl语法)。Nginx支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。
综上所述,小网站估计lamp就够了,访问量大的网站lnmp
lnmp部署
ip | 设备 |
---|---|
192.168.17.147 | NGINX |
192.168.17.134 | PHP |
192.168.17.133 | MYSQL |
安装mysql
[root@mysql src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
#创建用户和组
[root@mysql src]# useradd -r -M -s /sbin/nologin mysql
#安装依赖包
[root@mysql src]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
#解压软件至/usr/local/
#改个名字
[root@mysql local]# ln -s /usr/local/mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
#改目录/usr/local/mysql的属主属组
[root@mysql mysql]# chown -R mysql.mysql /usr/local/mysql
#配置mysql,设置这三个文件
[root@mysql mysql]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@mysql mysql]# echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf
[root@mysql mysql]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
#添加环境变量
[root@mysql mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql mysql]# source /etc/profile.d/mysql.sh
[root@mysql mysql]# which mysql
/usr/local/mysql/bin/mysql
#建立数据存放目录
[root@mysql mysql]# mkdir -p /opt/data
[root@mysql mysql]# chown -R mysql.mysql /opt/data
#初始化数据库
[root@mysql mysql]# mysqld --initialize --user mysql --datadir /opt/data
#生成配置文件
[root@mysql ~]# 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@mysql ~]# cd /usr/local/mysql/
[root@localhost mysql]# ls
LICENSE README bin docs include lib man share support-files
[root@localhost mysql]# cd support-files/
[root@localhost support-files]# ls
magic mysql-log-rotate mysql.server mysqld_multi.server
[root@localhost support-files]# file mysql.server
mysql.server: POSIX shell script, ASCII text executable
[root@localhost support-files]# cp mysql.server /etc/init.d/mysqld
[root@localhost support-files]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/opt/data
给它一个可执行的权限
[root@mysql support-files]# chmod +x /etc/init.d/mysqld
启动mysql,查看端口号3306
[root@mysql ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
设置开机自启
[root@mysql ~]# chkconfig --add mysqld
#修改一下密码,登录数据库
[root@mysql ~]# mysql -uroot -pFzfr7Qf5zs%7
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
mysql> set password = password('yqz1230');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> exit
Bye
[root@mysql ~]# mysql -uroot -pyqz1230
安装php
#创建nginx用户
[root@php src]# useradd -r -M -s /sbin/nologin nginx
#安装php依赖包
[root@php ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel --allowerasing --nobest
#下载并解压安装php
[root@php src]# ls
debug kernels
[root@php src]# wget wget https://www.php.net/distributions/php-7.4.30.tar.xz
[root@php src]# tar xf php-8.1.11.tar.gz
[root@php src]# ls
debug kernels php-8.1.11 php-8.1.11.tar.gz
[root@php php-8.1.11]# ./configure --prefix=/usr/local/php8 --with-config-file-path=/etc --enable-fpm --enable-inline-optimization --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-json --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@php php-8.1.11]# make && make install
#配置环境变量
[root@php php-8.1.11]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@php php-8.1.11]# source /etc/profile.d/php8.sh
[root@php php-8.1.11]# which php
/usr/local/php8/bin/php
#配置php-fpm
[root@php php-8.1.11]# cp php.ini-production /etc/php.ini
[root@php php-8.1.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php php-8.1.11]# chmod +x /etc/rc.d/init.d/php-fpm
[root@php php-8.1.11]# cd /usr/local/php8/etc/
[root@php etc]# cp php-fpm.conf.default php-fpm.conf
[root@php etc]# cd php-fpm.d/
[root@php php-fpm.d]# cp www.conf.default www.conf
[root@php ~]# vim /usr/local/php8/etc/php-fpm.d/www.conf
listen = 192.168.17.134:9000 //监听php服务器的ip端口号为9000
;listen.allowed_clients = 192.168.17.147 //允许nginx服务器进行访问
//创建index.php文件
[root@php ~]# mkdir /var/www
[root@php ~]# cat > /var/www/index.php <<EOF
> <?php
> phpinfo();
> ?>
> EOF
//重启服务
[root@php php-8.1.11]# service php-fpm start
Starting php-fpm done
[root@php ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 192.168.183.137:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
安装nginx
#创建系统用户nginx
[root@nginx src]# useradd -r -M -s /sbin/nologin nginx
#安装依赖环境
[root@nginx src]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@nginx src]# yum -y groups mark install 'Development Tools'
#创建日志存放目录
[root@nginx src]# mkdir -p /var/log/nginx
[root@nginx src]# chown -R nginx.nginx /var/log/nginx
#下载nginx
[root@nginx yum.repos.d]# cd /usr/src/
[root@nginx src]# ls
debug kernels
[root@nginx src]# wget https://nginx.org/download/ nginx-1.23.1.tar.gz
[root@nginx src]# ls
debug kernels nginx-1.23.1.tar.gz
#编译安装
[root@nginx src]# tar xf nginx-1.23.1.tar.gz
[root@nginx src]# ls
debug kernels nginx-1.23.1 nginx-1.23.1.tar.gz
[root@nginx ~]# cd nginx-1.23.1/
[root@nginx nginx-1.20.2]# ./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
#nginx配置,服务控制方式,使用nginx命令
[root@nginx nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
#配置环境变量
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# source /etc/profile.d/nginx.sh
[root@nginx ~]# which nginx
/usr/local/nginx/sbin/nginx
#使用 nginx -t 检ng查配置文件语法
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#用nginx -v 输出nginx的版本
[root@nginx ~]# nginx -v
nginx version: nginx/1.20.2
做一下开机自启,修改配置文件
[root@nginx ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginx.service
[root@slave ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@nginx ~]# systemctl daemon-reload
[root@nginx ~]# nginx -s stop
[root@nginx ~]# systemctl status nginx.service
● nginx.service - nginx server daemon
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disa>
Active: inactive (dead)
[root@nginx ~]# systemctl start nginx.service
[root@nginx ~]# systemctl status nginx.service
● nginx.service - nginx server daemon
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disa>
Active: active (running) since Mon 2022-10-10 20:32:58 CST; 3s ago
Process: 80407 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 80408 (nginx)
Tasks: 2 (limit: 23460)
[root@nginx ~]# systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@nginx ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@nginx ~]# 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 192.168.17.134:9000; //改为php端ip
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name; //将$/scripts修改为根目录
include fastcgi_params;
}
//检查配置文件是否有错误
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
//重启服务
[root@nginx ~]# systemctl restart nginx
//创建index.php文件
[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# vim index.php
[root@nginx html]# cat index.php
<?php
phpinfo();
?>
访问