centos7搭建lnmp

1、準備

1. 修改網絡yum源

先將系統自帶的yum配置文件重命名或刪除,而後下載下面兩個文件php

阿里雲:http://mirrors.aliyun.com/repo/Centos-7.repohtml

eprl擴展:http://mirrors.aliyun.com/repo/epel-7mysql

下載完以後,須要使用命令清除原來的yum緩存,使用新的配置文件創建新的緩存nginx

yum clean all                #清除原來的緩存
yum makecache                #創建新的緩存列表
yum update                   #將全部可更新的軟件更新

2. 安裝編譯工具和依賴軟件

yum -y install gcc gcc-c++ pcre-devel openssl openssl-devel zlib-devel ncurses-devel cmake bison libxml2-devel libpng-devel

3. nginx,mysql,php軟件源碼包下載地址

  nginx:http://nginx.org/en/download.htmlc++

  mysql:https://dev.mysql.com/downloads/mysql/web

  php:http://www.php.net/sql

  版本選用:vim

  nginx:  1.16.*  #選用軟件的穩定版本便可centos

  mysql:  5.5.*  #5.5以上版本須要1G以上的內存,不然沒法安裝緩存

  php:  7.3.*  #我使用的是php7

注:每次安裝LNMP時,軟件的小版本都不同,官方會對其大版本下的小版本進行覆蓋式更新,請按照下載版本進行安裝。

 

2、源碼軟件包安裝

1. nginx

nginx是一款輕量級的web服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,在BSD-like協議下發行。特色是佔用內存少,併發能力強。

1.1 下載nginx源碼包

[root@centos2 /lnmp]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

1.2 建立用於運行nginx的用戶

[root@centos2 /lnmp]# useradd -r -s /sbin/nologin nginx

1.3 解壓縮nginx並安裝

[root@centos2 /lnmp]# tar -zxf nginx-1.16.1.tar.gz
[root@centos2 /lnmp/nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
[root@centos2 /lnmp/nginx-1.16.1]# make && make install

1.4 上傳編寫好的nginx啓動腳本

注:先檢查文件中是否有字符集問題

#################################Nginx啓動管理腳本##################################
#!/bin/bash
#Author:nanshan
#chkconfig: 2345 99 33
#description: nginx server control tools

ngxc="/usr/local/nginx/sbin/nginx"
pidf="usr/local/nginx/logs/nginx.pid"
ngxc_fpm="/usr/local/php/sbin/php-fpm"
pidf_fpm="/usr/local/php/var/run/php-fpm.pid"
case "$1" in
    start)
        $ngxc -t  &> /dev/null
        if [ $? -eq 0 ];then
                    $ngxc
                    $ngxc_fpm
                    echo "nginx service start success!"
        else
                    $ngxc -t
        fi
        ;;
    stop)
        kill -s QUIT $(cat $pidf)
        kill -s QUIT $(cat $pidf_fpm)
                echo "nginx service stop success!"
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        $ngxc -t &> /dev/null
        if [ $? -eq 0 ];then
                    kill -s HUP $(cat $pidf)
                    kill -s HUP $(cat $pidf_fpm)
                    echo "reload nginx config success!"
        else
                    $ngxc -t
        fi
        ;;
    *)
        echo "please input stop|start|restart|reload."
        exit 1
esac

 

2. MySQL

下載:https://dev.mysql.com/downloads/mysql/

選擇:Looking for previous GA versions?

選擇:Select Version:按照本身要求選擇

   Select Operating System: Source Code

   Select OS Version: Generic Linux

格式:mysql-N.N.NN.tar.gz

[root@centos2 /lnmp/nginx-1.16.1]# wget https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.62.tar.gz

2.1 建立運行mysql的用戶

[root@centos2 /lnmp]# useradd -r -s /sbin/nologin mysql

2.2 解壓縮mysql並安裝

[root@centos2 /lnmp]# tar -zxvf mysql-5.5.62.tar.gz
[root@centos2 ~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
[root@centos2 ~]# make && make install
[root@centos2 ~]# ln -s /usr/local/mysql/bin/* /usr/local/bin/

2.3 修改安裝後的目錄權限

[root@centos2 ~]# cd /usr/local/mysql
[root@centos2 ~]# chown -R root .
[root@centos2 ~]# chown -R mysql data

2.4 生成mysql配置文件

[root@centos2 ~]# cp -a /lnmp/mysql-5-5.62/support-files/my-medium.cnf /etc/my.cnf

2.5 初始化,生成受權表

[root@centos2 ~]# cd /usr/local/mysql
[root@centos2 ~]# ./scripts/mysql_install_db --user=mysql
#初始化成功標誌:兩個OK

2.6 生成mysql的啓動和自啓動腳本

[root@centos2 ~]# cd /lnmp/mysql-5.5.62/support-files/

[root@centos2 /lnmp/mysql-5.5.62/support-files]# cp -a mysql.server /etc/init.d/mysqld

[root@centos2 /etc/init.d]# chmod +x mysqld 

[root@centos2 /etc/init.d]# chkconfig --add mysqld 
[root@centos2 /etc/init.d]# chkconfig mysqld on

[root@centos2 /etc/init.d]# systemctl start mysqld
[root@centos2 /etc/init.d]# systemctl status mysqld

2.7 給mysql的root用戶設置密碼

[root@centos2 /usr/local/mysql]# mysqladmin -uroot password 123456
[root@centos2 /usr/local/mysql]# mysql -uroot -p
Enter password: 

 

3. PHP

下載:https://www.php.net/

           http://mirrors.sohu.com/php/

[root@centos2 /lnmp]# wget https://www.php.net/distributions/php-7.3.12.tar.gz

3.1 解壓縮並安裝

[root@centos2 /lnmp/php-7.3.12]# tar -zxf php-7.3.12.tar.gz
[root@centos2 /lnmp/php-7.3.12]# ./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc/ --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-soap --enable-mbstring=all --enable-sockets --with-pdo-mysql=/usr/local/mysql --with-gd --without-pear --enable-fpm
[root@centos2 /lnmp/php-7.3.12]# make
[root@centos2 /lnmp/php-7.3.12]# make install

3.2 生成php配置文件

[root@centos2 ~]# cp -a /lnmp/php-7.3.12/php.ini-production  /usr/local/php/etc/php.ini
#複製源碼包內的配置文件到安裝目錄下,並更名便可

3.3 建立軟鏈接,使用php相關命令更方便

[root@centos2 ~]# ln -s /usr/local/php/bin/* /usr/local/bin/
[root@centos2 ~]# ln -s /usr/local/php/sbin/* /usr/local/sbin/

 

4. 配置nginx鏈接php

4.1 nginx鏈接php需啓動php-fpm服務

[root@centos2 ~]# cd /usr/local/php/etc/
[root@centos2 /usr/local/php/etc]# cp php-fpm.conf.default php-fpm.conf
[root@centos2 /usr/local/php/etc]# vim php-fpm.conf
#修改指定參數
pid = run/php-fpm.pid
[root@centos2 /usr/local/php/etc]# cd /usr/local/php/etc/php-fpm.d/
[root@centos2 /usr/local/php/etc/php-fpm.d]# cp -a www.conf.default www.conf
[root@centos2 /usr/local/php/etc/php-fpm.d]# vim www.conf
#修改用戶和組的指定用戶
user = nginx
group = nginx

修改nginx啓動腳本;將php-fpm的註釋取消便可

4.2 修改nginx的配置文件,使其能識別.php後綴的文件

[root@centos2 /usr/local/php/etc/php-fpm.d]# cd ../../../nginx/conf/
[root@centos2 /usr/local/nginx/conf]# vim nginx.conf
#取消下列行的註釋,並修改include 選項後的後綴爲fastcgi.conf,並注意每一行結尾的分號和大括號

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;    #修改成fastcgi.conf
        }

測試:

重啓nginx,建立php測試文件,訪問並查看是否解析

4.3 修改nginx配置文件,使其默認自動加載php文件

location / {
    root   html;                #nginx默認的網頁路徑:prefix/html
    index  index.html index.php index.htm; #設置默認加載的頁面,以及優先級
}
相關文章
相關標籤/搜索