LNMP(Linux+PHP+Nginx+Mysql)環(huán)境搭建

系統(tǒng)環(huán)境

[root@web01 ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
[root@web01 ~]# uname -r
3.10.0-1062.18.1.el7.x86_64

LNMP服務(wù)用

一、Nginx的安裝

1.1.1 下載編譯安裝Nginx

1)下載Nginx到到本我服務(wù)器

[root@web01 ~]# mkdir -p /server/tools/
[root@web01 ~]# cd /server/tools/
[root@web01 /server/tools]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
--2020-05-09 21:06:13--  http://nginx.org/download/nginx-1.16.0.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1032345 (1008K) [application/octet-stream]
Saving to: ‘nginx-1.16.0.tar.gz’

100%[===========================================================================================>] 1,032,345   7.08KB/s   in 1m 54s 

2020-05-09 21:08:08 (8.85 KB/s) - ‘nginx-1.16.0.tar.gz’ saved [1032345/1032345]

[root@web01 /server/tools]# ls
nginx-1.16.0.tar.gz

2)yum下載依賴環(huán)境

[root@web01 /server/tools]# yum install pcre  pcre-devel openssl openssl-devel make gcc gcc-c++ -y

3)創(chuàng)建www用戶

[root@web01 /server/tools]# useradd -M -s /sbin/nologin www
[root@web01 /server/tools]# id www
uid=888(www) gid=888(www) groups=888(www)

4)解壓Nginx并編譯安裝

[root@web01 /server/tools]# tar zxf nginx-1.16.0.tar.gz 
[root@web01 /server/tools]# cd nginx-1.16.0/
[root@web01 /server/tools/nginx-1.16.0]# ./configure --help #編譯幫助命令
[root@web01 /server/tools/nginx-1.16.0]# ./configure --prefix=/application/nginx-1.16.0 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre
[root@web01 /server/tools/nginx-1.16.0]# make && make install

5)做個軟連接去掉版本號

[root@web01 /server/tools/nginx-1.16.0]# ln -s /application/nginx-1.16.0/ /application/nginx
[root@web01 /server/tools/nginx-1.16.0]# ls /application/nginx
conf  html  logs  sbin
[root@web01 /server/tools/nginx-1.16.0]# ls -l /application/nginx
lrwxrwxrwx 1 root root 26 May  9 21:29 /application/nginx -> /application/nginx-1.16.0/
  1. 查看你版本和編譯的參數(shù)
[root@web01 /server/tools/nginx-1.16.0]# cd /application/nginx
[root@web01 /application/nginx]# sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.16.0 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre
  1. 啟動Nginx服務(wù)
[root@web01 /application/nginx]# sbin/nginx 
[root@web01 /application/nginx]# ps -ef|grep nginx
root      27669      1  0 21:33 ?        00:00:00 nginx: master process sbin/nginx
www       27670  27669  0 21:33 ?        00:00:00 nginx: worker process
root      27672  24853  0 21:33 pts/0    00:00:00 grep --color=auto nginx
[root@web01 /application/nginx]# ss -luntp|grep 80
tcp    LISTEN     0      511       *:80                    *:*                   users:(("nginx",pid=27670,fd=6),("nginx",pid=27669,fd=6))
[root@web01 /application/nginx]# lsof -i :80
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   27669 root    6u  IPv4 210139      0t0  TCP *:http (LISTEN)
nginx   27670  www    6u  IPv4 210139      0t0  TCP *:http (LISTEN)
  1. 訪問Nginx網(wǎng)頁
[root@web01 /application/nginx]# curl 172.16.1.7
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
#當(dāng)然你也可以在瀏覽器上訪問:http:ip地址

9)Nginx的關(guān)閉和重啟

#關(guān)閉
[root@web01 /application/nginx]# sbin/nginx -s stop
#重啟
[root@web01 /application/nginx]# sbin/nginx -s reload

10) nginx的配置文件

[root@web01 /application/nginx]# tree
.
├── client_body_temp
├── conf
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── fastcgi_temp
├── html
│   ├── 50x.html
│   └── index.html
├── logs
│   ├── access.log
│   ├── error.log
│   └── nginx.pid
├── nginx-1.16.0 -> /application/nginx-1.16.0/
├── proxy_temp
├── sbin
│   ├── nginx
│   └── nginx.old
├── scgi_temp
└── uwsgi_temp

二、Mysql數(shù)據(jù)庫的安裝

2.1.1 安裝Mysql數(shù)據(jù)庫

1.創(chuàng)建用戶

[root@web01 ~]# useradd -M -s /sbin/nologin mysql
[root@web01 ~]# id mysql
uid=1001(mysql) gid=1001(mysql) groups=1001(mysql)

2.上傳mysql安裝包

下載地址:https://downloads.mysql.com/archives/community/

圖片.png
[root@web01 /server/tools]# rz -E
rz waiting to receive.
[root@web01 /server/tools]# ll
total 630768
-rw-r--r-- 1 root  root  644869837 Oct 28  2019 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x 9 mysql mysql       186 May  9 21:25 nginx-1.16.0
-rw-r--r-- 1 root  root    1032345 Apr 23  2019 nginx-1.16.0.tar.gz

3.解壓安裝

[root@web01 /server/tools]# tar zxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz 
[root@web01 /server/tools]# ll
total 630768
drwxr-xr-x 9 root  root        129 May 11 22:22 mysql-5.7.26-linux-glibc2.12-x86_64
-rw-r--r-- 1 root  root  644869837 Oct 28  2019 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x 9 mysql mysql       186 May  9 21:25 nginx-1.16.0
-rw-r--r-- 1 root  root    1032345 Apr 23  2019 nginx-1.16.0.tar.gz
#因為mysql是二進制安裝,所以解壓就可以用

4.移動到第三方軟件目錄下

[root@web01 /server/tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql-5.7.26
[root@web01 /server/tools]# ll /application/mysql-5.7.26
total 36
drwxr-xr-x  2 root root   4096 May 11 22:22 bin
-rw-r--r--  1 7161 31415 17987 Apr 13  2019 COPYING
drwxr-xr-x  2 root root     55 May 11 22:22 docs
drwxr-xr-x  3 root root   4096 May 11 22:21 include
drwxr-xr-x  5 root root    230 May 11 22:22 lib
drwxr-xr-x  4 root root     30 May 11 22:21 man
-rw-r--r--  1 7161 31415  2478 Apr 13  2019 README
drwxr-xr-x 28 root root   4096 May 11 22:22 share
drwxr-xr-x  2 root root     90 May 11 22:22 support-files
  1. 做個軟鏈接
[root@web01 /server/tools]# ln -s /application/mysql-5.7.26 /application/mysql
[root@web01 /server/tools]# ls -ls /application/mysql
0 lrwxrwxrwx 1 root root 25 May 11 22:33 /application/mysql -> /application/mysql-5.7.26

6.創(chuàng)建件配置文件

[root@web01 /server/tools]# vim /etc/my.cnf
[root@web01 /server/tools]# cat /etc/my.cnf
[mysqld]
basedir = /application/mysql/
datadir = /application/mysql/data
socket = /tmp/mysql.sock
server_id = 1
port = 3306
log_error = /application/mysql/data/mysql.err
pid-file = /application/mysql/mysql.pid

[mysql]
socket = /tmp/mysql.sock

7.創(chuàng)建數(shù)據(jù)存放目錄data

[root@web01 /server/tools]# mkdir /application/mysql/data
[root@web01 /server/tools]# chown mysql.mysql /application/mysql

8.初始化數(shù)據(jù)庫

[root@web01 /application/mysql]# /application/mysql/bin/mysqld --initialize-insecure --user=mysql   --basedir=/application/mysql/ --datadir=/application/mysql/data/
#注意不要執(zhí)行第二遍。否則會報錯
  1. 報錯
[root@web01 /application/mysql]# /application/mysql/bin/mysqld --initialize-insecure --user=mysql --group=mysql  --basedir=/application/mysql/ --datadir=/application/mysql/data/
2020-05-11T15:16:51.309166Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-05-11T15:16:51.311402Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2020-05-11T15:16:51.311793Z 0 [ERROR] Aborting
#解決方法
[root@web01 /application/mysql]# rm -rf data/*
再初始化一遍

10 用system來管理mysql

[root@web01 /application/mysql]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service
[root@web01 /application/mysql/data]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MYSQL server 
Documentation=https://dev.mysql.com/doc/refman/
After=network.target 
After=syslog.target 


[Service]
User=mysql
Group=mysql
EnvironmentFile=/etc/my.cnf
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
ExecReload=/bin/kill -HUP `cat /application/mysql/mysql.pid`
limitNOFILE=5000

[Install]
WantedBy=multi-user.target

11.啟動成功

[root@web01 /application/mysql]# systemctl start mysqld
[root@web01 /application/mysql]# systemctl status mysqld
● mysqld.service - MYSQL server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-05-12 00:36:14 CST; 12s ago
     Docs: https://dev.mysql.com/doc/refman/
 Main PID: 2103 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─2103 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf

May 12 00:36:14 web01 systemd[1]: [/usr/lib/systemd/system/mysqld.service:14] Unknown lvalue 'limitNOFILE' in section 'Service'
May 12 00:36:14 web01 systemd[1]: Started MYSQL server.
[root@web01 /application/mysql]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
  1. 檢查mysql是否啟動成功
[root@web01 /application/mysql]# netstat -luntp|grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      2608/mysqld 
  1. 配置環(huán)境變量
[root@web01 /application/mysql]# echo "export PATH=/application/mysql/bin:$PATH" >>/etc/profile
[root@web01 /application/mysql]# tail -1 /etc/profile
export PATH=/application/mysql/bin:/application/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
[root@web01 /application/mysql]# . /etc/profile
  1. 進入mysql并利用mysqladmin設(shè)置用戶和密碼
[root@web01 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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>exit 
Bye

#退出設(shè)置用戶和密碼
[root@web01 ~]# mysqladmin -u root password '密碼'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
  1. 再次登錄就需要指定用戶和密碼了
[root@web01 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> 

三、PHP下載安裝

3.1.1 PHP的裝

1.環(huán)境依賴安裝

[root@web01 ~]# yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel   freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel mcrypt mhash  libmcrypt-devel -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * epel: mirror2.totbb.net
Package zlib-devel-1.2.7-18.el7.x86_64 already installed and latest version
No package libiconv-devel available.
  1. 下載字符編碼的庫文件
    下載地址:http://www.gnu.org/software/libiconv/
    舊版本:https://ftp.gnu.org/gnu/libiconv/
    圖片.png

3.PHP下載
官網(wǎng):https:www.php.net

圖片.png

4.編譯安裝字符編碼庫文件

[root@web01 ~]# cd /server/tools/
[root@web01 /server/tools]# ls 
libiconv-1.16.tar.gz  mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz  nginx-1.16.0  nginx-1.16.0.tar.gz  php-7.3.5.tar.gz
[root@web01 /server/tools]# tar zxf libiconv-1.16.tar.gz
[root@web01 /server/tools]# cd libiconv-1.16/
[root@web01 /server/tools/libiconv-1.16]# ./configure  --prefix=/application/libiconv
[root@web01 /server/tools/libiconv-1.16]# make && make install

5.編譯安裝PHP

[root@web01 /server/tools]# tar zxf php-7.3.5.tar.gz 
[root@web01 /server/tools]# cd php-7.3.5/
[root@web01 /server/tools/php-7.3.5]# ./configure \
--prefix=/application/php-7.3.5 \
--enable-mysqlnd  \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir=/application/libiconv \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-ftp \
--enable-opcache=no> --with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-ftp \
--enable-opcache=no
[root@web01 /server/tools/php-7.3.5]# make && make install

6.創(chuàng)建軟連接

[root@web01 /server/tools/php-7.3.5]# ln -s /application/php-7.3.5/  /application/php
[root@web01 /server/tools/php-7.3.5]# ls -ld /application/php
lrwxrwxrwx 1 root root 23 May 14 00:03 /application/php -> /application/php-7.3.5/

7.配置php.ini(PHP解析器配置文件)

#這個配置文件在PHP的解壓源碼包里面
[root@web01 /application/php/etc]# ll /server/tools/php-7.3.5/php.ini*
-rw-r--r-- 1 root root 71648 Apr 30  2019 /server/tools/php-7.3.5/php.ini-development   #開發(fā)環(huán)境
-rw-r--r-- 1 root root 71920 Apr 30  2019 /server/tools/php-7.3.5/php.ini-production    #生產(chǎn)環(huán)境
[root@web01 /application/php/etc]# cp  /server/tools/php-7.3.5/php.ini-production /application/php/lib/php.ini
[root@web01 /application/php/etc]# ll /application/php/lib/php.ini
-rw-r--r-- 1 root root 71920 May 14 00:43 /application/php/lib/php.ini
  1. 配置PHP FPM
[root@web01 /application/php/etc]# ll
total 12
-rw-r--r-- 1 root root 1321 May 13 23:58 pear.conf
-rw-r--r-- 1 root root 5417 May 13 23:58 php-fpm.conf.default
drwxr-xr-x 2 root root   30 May 13 23:58 php-fpm.d
[root@web01 /application/php/etc]# cp php-fpm.conf.default php-fpm.conf
[root@web01 /application/php/etc]# ll
total 20
-rw-r--r-- 1 root root 1321 May 13 23:58 pear.conf
-rw-r--r-- 1 root root 5417 May 14 00:49 php-fpm.conf
-rw-r--r-- 1 root root 5417 May 13 23:58 php-fpm.conf.default
drwxr-xr-x 2 root root   30 May 13 23:58 php-fpm.d
[root@web01 /application/php/etc]# cd php-fpm.d/
[root@web01 /application/php/etc/php-fpm.d]# ll
total 20
-rw-r--r-- 1 root root 19240 May 13 23:58 www.conf.default
[root@web01 /application/php/etc/php-fpm.d]# cp www.conf.default www.conf
[root@web01 /application/php/etc/php-fpm.d]# ll
total 40
-rw-r--r-- 1 root root 19240 May 14 00:50 www.conf
-rw-r--r-- 1 root root 19240 May 13 23:58 www.conf.default

9.啟動PHP

[root@web01 /application/php/etc/php-fpm.d]# /application/php/sbin/php-fpm 
[root@web01 /application/php/etc/php-fpm.d]# netstat -luntp|grep php
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      8943/php-fpm: maste   #9000端口啟動成功

10.配置開機自啟動

[root@web01 /application/php/etc/php-fpm.d]# echo "/application/php/sbin/php-fpm" >>/etc/rc.local 
[root@web01 /application/php/etc/php-fpm.d]# chmod +x /etc/rc.d/rc.local

11.配置nginx轉(zhuǎn)發(fā)PHP請求

[root@web01 /application/php/etc/php-fpm.d]# cat /application/nginx/conf/extra/03_blog.conf 
      server {

     listen       8;
     server_name  blog.etiantian.org;

     location / {
     root      html/blog;
     index    index.html index.htm;

       }
        location ~ \.php$ {
            root           html/blog;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #這里要修改一下將/scripts修改為$document_root
            include        fastcgi_params;
        }

      error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
     access_log  logs/access_blog.log  main;
    }
#因為我的環(huán)境的是每個虛擬主機是一個獨立的配置文件,所以配置需要看個人環(huán)境,不要被誤導(dǎo)。

12.檢查語法并啟動nginx

[root@web01 /application/php/etc/php-fpm.d]# nginx -t
nginx: the configuration file /application/nginx-1.16.0/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16.0/conf/nginx.conf test is successful
[root@web01 /application/php/etc/php-fpm.d]# nginx
[root@web01 /application/php/etc/php-fpm.d]# netstat -luntp|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9120/nginx: master

13.編寫測試文件檢查是否成功

[root@web01 /application/php/etc/php-fpm.d]# vim /application/nginx/html/blog/index.php
[root@web01 /application/php/etc/php-fpm.d]# cat /application/nginx/html/blog/index.php
<?php
phpinfo();
?>

14.出現(xiàn)如下圖片代表部署成功


圖片.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容