centos7環(huán)境下手動部署LNMP環(huán)境

Nginx是一款小巧而高效的Web服務器軟件,可幫您在Linux系統(tǒng)下快速方便地搭建出LNMP Web服務環(huán)境。本教程介紹如何手動在ECS實例上搭建LNMP環(huán)境,其中LNMP分別代表Linux、Nginx、MySQL和PHP。

配置詳情:

CPU:2 vCPU

內(nèi)存:4GiB

網(wǎng)絡類型:專有網(wǎng)絡VPC

IP地址:公網(wǎng)IP

操作系統(tǒng):公共鏡像CentOS 7.2 64位

Nginx版本:Nginx 1.16.1

MySQL版本:MySQL 5.7.28

PHP版本:PHP 7.0.33

操作流程

一、安裝準備

1關閉防火墻

臨時關閉防火墻,運行命令systemctl stop firewalld

永久關閉防火墻,運行命令systemctl disable firewalld

2關閉SELinux

臨時關閉SELinux,運行命令setenforce 0。

永久關閉SELinux,運行命令vim /etc/selinux/config。編輯SELinux配置文件?;剀嚭螅压鈽艘苿拥絊ELINUX=enforcing這一行,按i鍵進入編輯模式,修改為SELINUX=disabled,按Esc鍵,然后輸入:wq并按Enter鍵以保存并關閉SELinux配置文件。

二、安裝NGINX

1添加nginx安裝源

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2安裝nginx

yum -y install nginx

3啟動nginx并開機自啟

systemctl start nginx

systemctl enable nginx

4查看nginx版本

nginx -v

nginx version: nginx/1.18.0

三、安裝Mysql

1運行以下命令更新YUM源

 rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

 rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

2運行以下命令安裝MySQL。

yum -y install mysql-community-server

3運行以下命令查看MySQL版本號。

mysql -V

4運行以下命令啟動MySQL。

systemctl start mysqld

5運行以下命令設置開機啟動MySQL。

systemctl enable mysqld

systemctl daemon-reload

四、安裝PHP

1運行以下命令添加epel源。

yum install https://repo.ius.io/ius-release-el7.rpm https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

2運行以下命令添加Webtatic源。

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

3運行以下命令安裝PHP。

yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64  php70w-pdo.x86_64  php70w-mysqlnd  php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb

4運行以下命令查看PHP版本。

php -v

五、配置NGINX

1運行以下命令備份Nginx配置文件。

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

2修改Nginx配置文件,添加Nginx對PHP的支持。

vim /etc/nginx/conf.d/default.conf



在server大括號內(nèi),添加下列配置信息。
    listen       80;
    server_name  localhost;
    root         /usr/share/nginx/html;


 #除下面提及的需要添加的配置信息外,其他配置保持默認值即可。

location / {

#在location大括號內(nèi)添加以下信息,配置網(wǎng)站被訪問時的默認首頁

     index index.php index.html index.htm;

  }

#添加下列信息,配置Nginx通過fastcgi方式處理您的PHP請求

  location ~ .php$ {

    root /usr/share/nginx/html;   #將/usr/share/nginx/html替換為您的網(wǎng)站根目錄,本教程使用/usr/share/nginx/html作為網(wǎng)站根目錄

    fastcgi_pass 127.0.0.1:9000;  #Nginx通過本機的9000端口將PHP請求轉發(fā)給PHP-FPM進行處理

    fastcgi_index index.php;

    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

    include fastcgi_params;  #Nginx調(diào)用fastcgi接口處理PHP請求

 }         
image.png

3運行以下命令啟動Nginx服務。

systemctl start nginx

4運行以下命令設置Nginx服務開機自啟動。

systemctl enable nginx

配置MySQL

1運行以下命令查看/var/log/mysqld.log文件,獲取并記錄root用戶的初始密碼。

grep 'temporary password' /var/log/mysqld.log

返回結果如下:

2016-12-13T14:57:47.535748Z 1 [Note] A temporary password is generated for root@localhost: p0/G28g>lsHD

2運行以下命令配置MySQL的安全性。

mysql_secure_installation

安全性的配置包含以下五個方面:

重置root賬號密碼。

Enter password for user root: #輸入上一步獲取的root用戶初始密碼(p0/G28g>lsHD

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) : Y #是否更改root用戶密碼,輸入YNew password: #輸入新密碼,長度為8至30個字符,必須同時包含大小寫英文字母、數(shù)字和特殊符號。特殊符號可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/

Re-enter new password: #再次輸入新密碼

Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y

輸入Y刪除匿名用戶賬號。

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.

輸入Y禁止root賬號遠程登錄。

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root遠程登錄,輸入Y

Success.

輸入Y刪除test庫以及對test庫的訪問權限。

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否刪除test庫和對它的訪問權限,輸入Y

- Dropping test database...

Success.

輸入Y重新加載授權表。

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加載授權表,輸入Y

Success.All done!
、配置PHP

1新建phpinfo.php文件,用于展示PHP信息。

運行以下命令新建文件。

vim <網(wǎng)站根目錄>/phpinfo.php #將<網(wǎng)站根目錄>替換為您配置的網(wǎng)站根目錄。

網(wǎng)站根目錄是您在nginx.conf文件中l(wèi)ocation ~ .php$大括號內(nèi)配置的root值,如下圖所示。



本教程配置的網(wǎng)站根目錄為/usr/share/nginx/html,因此命令為:

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

按i進入編輯模式。

輸入下列內(nèi)容,函數(shù)phpinfo()會展示PHP的所有配置信息。

<?php echo phpinfo(); ?>

按Esc鍵后,輸入:wq并回車以保存并關閉配置文件。

2運行以下命令啟動PHP-FPM。

systemctl start php-fpm

3運行以下命令設置PHP-FPM開機自啟動。

systemctl enable php-fpm

、測試訪問LNMP平臺

打開瀏覽器。

在地址欄輸入http://<ECS實例公網(wǎng)IP地址>/phpinfo.php。

返回結果如下圖所示,表示LNMP環(huán)境部署成功。



后續(xù)步驟

測試訪問LNMP平臺成功后,建議您運行以下命令將phpinfo.php文件刪除,消除安全隱患。

rm -rf <網(wǎng)站根目錄>/phpinfo.php #將<網(wǎng)站根目錄>替換為您在nginx.conf中配置的網(wǎng)站根目錄

本教程配置的網(wǎng)站根目錄為/usr/share/nginx/html,因此命令為:

rm -rf /usr/share/nginx/html/phpinfo.php

搭建dz論壇

創(chuàng)建數(shù)據(jù)庫,數(shù)據(jù)庫名,數(shù)據(jù)庫密碼 授權

進入數(shù)據(jù)庫

mysql -uroot -p

創(chuàng)建數(shù)據(jù)庫

create database selinux_com_cn;

創(chuàng)建用戶

create user 'selinux_com_cn'@'%' identified by '52Chijuzi';

給新建用戶授權

 grant all privileges on selinux_com_cn.* to 'selinux_com_cn'@'%'identified by '52Chijuzi!@';
flush privileges;

修改文件夾權限

root@localhost html]# chown -R nginx:nginx /usr/share/nginx/html/*

chmod -R o=rwx /usr/share/nginx/html/

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

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