全手工搭建LNMP環境(CentOS7.2 PHP7.0)

對于不想使用各種面板的老鳥玩家,本文主要介紹了在VPS云服務器上如何手工搭建LNMP平臺的web環境。具體操作以CentOS 7.2 64位操作系統為例。

Linux實例手工部署LNMP環境具體操作步驟如下:

  1. 安裝nginx。
  2. 安裝MySQL。
  3. 安裝PHP。
  4. 瀏覽器訪問測試。

前提條件

  1. VPS云服務器已綁定彈性公網IP。
  2. VPS云服務器所在安全組添加了如下表所示的安全組規則,具體步驟參見為安全組添加安全組規則。注意不同服務商有不同操作。很多國外商家也沒有限制,默認全開的。

安全組規則

方向

協議/應用

端口/范圍

源地址

入方向

HTTP(80)

80

0.0.0.0/0

操作步驟

  1. 安裝nginx。
    1. 登錄VPS云服務器。
    2. 執行以下命令,下載對應當前系統版本的nginx包。
      wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    3. 執行以下命令,建立nginx的yum倉庫。
      rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
    4. 執行以下命令,安裝nginx。
      yum -y install nginx
    5. 執行以下命令,啟動nginx并設置開機啟動。
      systemctl start nginx
      systemctl enable nginx
    6. 使用瀏覽器訪問 “http://服務器IP地址”,顯示如下頁面,說明nginx安裝成功。
  2. 安裝MySQL。
    1. 依次執行以下命令,安裝MySQL。
      rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
      yum -y install mysql-community-server
    2. 依次執行以下命令,啟動MySQL服務并設置開機自啟動。
      systemctl start mysqld
      systemctl enable mysqld
    3. 執行以下命令,獲取安裝MySQL時自動設置的root用戶密碼。
      grep 'temporary password' /var/log/mysqld.log
    4. 回顯如下類似信息。
      2018-08-29T07:27:37.541944Z 1 [Note] A temporary password is generated for root@localhost: 2YY?3uHUA?Ys
    5. 執行以下命令,并按照回顯提示信息進行操作,加固MySQL。
      mysql_secure_installation
      Securing the MySQL server deployment.
      
      Enter password for user root:    #輸入上一步驟中獲取的安裝MySQL時自動設置的root用戶密碼
      The existing password for the user account root has expired. Please set a new password.
      
      New password:  #設置新的root用戶密碼
      
      Re-enter new password:   #再次輸入密碼
      The 'validate_password' plugin is installed on the server.
      The subsequent steps will run with the existing configuration of the plugin.
      Using existing password for root.
      
      Estimated strength of the password: 100
      Change the password for root ? ((Press y|Y for Yes, any other key for No) : N   #是否更改root用戶密碼,輸入N
      
       ... skipping.
      By default, a MySQL installation has an anonymous user,
      allowing anyone to log into MySQL without having to have
      a user account created for them. This is intended only for
      testing, and to make the installation go a bit smoother.
      You should remove them before moving into a production
      environment.
      
      Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y   #是否刪除匿名用戶,輸入Y
      Success.
      
      Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
      
      Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y   #禁止root遠程登錄,輸入Y
      Success.
      
      By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
      
      Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y   #是否刪除test庫和對它的訪問權限,輸入Y
       - Dropping test database...
      Success.
      
       - Removing privileges on test database...
      Success.
      
      Reloading the privilege tables will ensure that all changes
      made so far will take effect immediately.
      
      Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y   #是否重新加載授權表,輸入Y
      Success.
      
      All done!
      
  3. 安裝PHP。
    1. 依次執行以下命令,安裝PHP 7和一些所需的PHP擴展。
      rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
      
      rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
      
      yum -y install php70w-tidy php70w-common php70w-devel php70w-pdo php70w-mysql php70w-gd php70w-ldap php70w-mbstring php70w-mcrypt php70w-fpm
    2. 執行以下命令,驗證PHP的安裝版本。
      php -v
    3. 回顯如下類似信息:
      PHP 7.0.31 (cli) (built: Jul 20 2018 08:55:22) ( NTS )
      Copyright (c) 1997-2017 The PHP Group
      Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
      
    4. 執行以下命令,啟動PHP服務并設置開機自啟動。
      systemctl start php-fpm
      
      systemctl enable php-fpm
    5. 修改nginx配置文件以支持PHP。
      1. 執行以下命令打開配置文件“default.conf”。
        vim /etc/nginx/conf.d/default.conf
      2. 按
        i

        鍵進入編輯模式。

      3. 修改打開的“default.conf”文件。
        • 在所支持的主頁面格式中添加php格式的主頁,如下所示:
              location / {
                  root   /usr/share/nginx/html;
                  index index.php index.html index.htm;
              }
          
        • 取消如下內容的注釋,并設置字體加粗部分為nginx的默認路徑,如下圖所示:
              location ~ \.php$ {
                  root           html;
                  fastcgi_pass   127.0.0.1:9000;
                  fastcgi_index  index.php;
                  fastcgi_param  SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
                  include        fastcgi_params;
              }
          
      4. 按Esc鍵退出編輯模式,并輸入:wq保存后退出。
    6. 執行以下命令,重新載入nginx的配置文件。
      service nginx reload
  4. 瀏覽器訪問測試。
    1. 在/usr/share/nginx/html/目錄下創建“info.php”的測試頁面。
      1. 執行以下命令創建并打開“info.php”的測試文件。

        vim /usr/share/nginx/html/info.php

      2. 按i鍵進入編輯模式。
      3. 修改打開的“info.php”文件,將如下內容寫入文件。
        <?php
         phpinfo();
        ?>

         

      4. 按Esc鍵退出編輯模式,并輸入:wq保存后退出。
    2. 使用瀏覽器訪問“http://服務器IP地址/info.php”,顯示如下頁面,說明環境搭建成功。

贊(0)
版權聲明:本文采用知識共享 署名4.0國際許可協議 [BY-NC-SA] 進行授權, 轉載請注明出處。
文章名稱:《全手工搭建LNMP環境(CentOS7.2 PHP7.0)》
文章鏈接:http://www.lrracing.com/11895.html
【聲明】:國外主機測評僅分享信息,不參與任何交易,也非中介,所有內容僅代表個人觀點,均不作直接、間接、法定、約定的保證,讀者購買風險自擔。一旦您訪問國外主機測評,即表示您已經知曉并接受了此聲明通告。
【關于安全】:任何 IDC商家都有倒閉和跑路的可能,備份永遠是最佳選擇,服務器也是機器,不勤備份是對自己極不負責的表現,請保持良好的備份習慣。