lnmp簡介
LNMP是指一組通常一起使用來運行動態(tài)網(wǎng)站或者服務(wù)器的自由軟件名稱首字母縮寫扣蜻。L指Linux走孽,N指Nginx钝的,M一般指MySQL翁垂,也可以指MariaDB,P一般指PHP扁藕,也可以指Perl或Python沮峡。
LNMP代表的就是:Linux系統(tǒng)下Nginx+MySQL+PHP這種網(wǎng)站服務(wù)器架構(gòu)
linux是Unix計算機操作系統(tǒng)的統(tǒng)稱,是目前最流行的免費的操作系統(tǒng)亿柑,代表有Debian、centos棍弄、ubuntu望薄、fedora、gentoo等呼畸。
Nginx是一個高性能的HTTP和反向代理服務(wù)器痕支,也是一個IMAP/POP3/SMTP代理服務(wù)器。
Mysql是一個小型關(guān)系型數(shù)據(jù)庫管理系統(tǒng)
PHP是一種在服務(wù)器端執(zhí)行的嵌入HTML文檔的腳本語言
這四種軟件均為免費開源軟件蛮原,組合到一起卧须,成為一個免費、高效、擴展性強的網(wǎng)站服務(wù)系統(tǒng)花嘶。
環(huán)境說明
系統(tǒng) | IP |
---|---|
Redhat8.2 | 192.168.182.143 |
安裝nginx
下載nginx的源碼包
[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
解決nginx的依賴
yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel
解壓nginx的rpm包
[root@localhost ~]# tar -xzf nginx-1.20.1.tar.gz -C /usr/local
創(chuàng)建系統(tǒng)用戶nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
創(chuàng)建日志存放目錄
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
編譯安裝
[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
安裝mysql
- 安裝依賴包
[root@localhost opt]# yum -y install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel openssl openssl-devel ncurses-compat-libs perl ncurses-devel cmake
- 下載MySQL的tar包
[root@localhost opt]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C packages
[root@localhost opt]# tar xf packages/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
- 創(chuàng)建MySQL用戶和組
[root@localhost opt]# useradd -r -M -s /sbin/nologin mysql
- 為了后面操作方便將mysql-5.7.32-linux-glibc2.12-x86_64目錄改名或做成軟連接都可以
[root@localhost local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
- MySQL的屬主和屬組
[root@localhost local]# pwd
/usr/local
[root@localhost local]# chown -R mysql.mysql mysql
- 添加環(huán)境變量
[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
- 配置MySQL數(shù)據(jù)庫的lib庫文件
[root@localhost local]# vim /etc/ld.so.conf.d/mysql.conf
[root@localhost local]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost local]# ldconfig
- 創(chuàng)建MySQL的數(shù)據(jù)目錄笋籽,并修改其屬主和屬組
[root@localhost local]# mkdir -p /opt/data
[root@localhost local]# chown -R mysql.mysql /opt/data/
- 初始化數(shù)據(jù)庫
[root@localhost ~]# mysqld --initialize-insecure --user mysql --datadir /opt/data/
- 生成MySQL配置文件
[root@localhost 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
- 配置服務(wù)啟動的腳本
[root@localhost ~]# vim /usr/local/mysql/support-files/mysql.server
找到basedir和datadir然后添加下面的內(nèi)容
basedir=/usr/local/mysql
datadir=/opt/data
- 配置系統(tǒng)服務(wù)使用systemctl來管理MySQL
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service
[root@localhost ~]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysql server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now mysqld.service
設(shè)置數(shù)據(jù)庫密碼
[root@localhost local]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password = password("1");
Query OK, 0 rows affected, 1 warning (0.00 sec)
安裝php
需要網(wǎng)絡(luò)倉庫:curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@localhost local]# 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 http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
下載php的tar包并解壓
可以去官網(wǎng)下載:https://www.php.net/downloads
[root@localhost opt]# https://www.php.net/distributions/php-8.0.11.tar.gz
[root@localhost opt]# tar xf packages/php-8.0.11.tar.xz -C /usr/local/
編譯安裝php
[root@localhost php-8.0.11]# ./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
[root@localhost php-8.0.11]# make && make install
配置環(huán)境變量
[root@localhost ~]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@localhost ~]# source /etc/profile.d/php.sh
配置php-fpm文件
[root@localhost php-8.0.11]# cp php.ini-production /etc/php.ini
cp:是否覆蓋'/etc/php.ini'? y
[root@localhost fpm]# pwd
/usr/local/php-8.0.11/sapi/fpm
[root@localhost fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-8.0.11]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-8.0.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost php-8.0.11]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
[root@localhost php-8.0.11]# service php-fpm start
Starting php-fpm done
配置系統(tǒng)服務(wù)使用systemctl來管理PHP
[root@localhost ~]# cp /usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/php-fpm.service
[root@localhost ~]# vim /usr/lib/systemd/system/php-fpm.service
[root@localhost ~]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php 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 $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost ~]# pkill php-fpm
[root@localhost ~]# systemctl enable --now php-fpm.service
Synchronizing state of php-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable php-fpm
創(chuàng)建php訪問界面
[root@localhost html]# cat index.php
<?php
phpinfo();
?>
[root@localhost html]# pwd
/usr/local/nginx/html
修改nginx配置文件
[root@localhost conf]# vim nginx.conf
43 location / {
44 root html;
45 index index.php index.html index.htm;
46 }
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# pwd
/usr/local/nginx/conf
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; //這個地方寫你你PHP頁面的路徑椭员,可寫絕對路徑车海,但是最好寫變量
70 include fastcgi_params;
71 }