網(wǎng)站開發(fā)公司網(wǎng)絡推廣的作用
文章目錄
- 一、環(huán)境信息
- 二、LNMP環(huán)境搭建
- 2.1 準備編譯環(huán)境
- 2.2 nginx安裝
- 2.3 mysql安裝
- 2.4 php安裝
- 2.5 nginx配置
- 2.6 mysql配置
- 2.7 配置php
- 三、常見問題
- 3.1 安裝其它版本的nginx服務
- 3.2 php版本過低
一、環(huán)境信息
操作系統(tǒng):公共鏡像CentOS 7.8 64位
本文的部署配置中,服務版本如下,如果需要其它版本,需要另行安裝配置。
Nginx版本:Nginx 1.20.1
MySQL版本:MySQL 5.7.36
PHP版本:PHP 7.0.33
二、LNMP環(huán)境搭建
2.1 準備編譯環(huán)境
- 關閉防火墻
運行 systemctl status firewalld 命令,查看當前防火墻的狀態(tài)
如果防火墻的狀態(tài)參數(shù)是inactive,則防火墻為關閉狀態(tài);如果防火墻的狀態(tài)參數(shù)是active,則防火墻為開啟狀態(tài)。
# 臨時關閉
systemctl stop firewalld# 禁止開機自啟
systemctl disable firewalld
- 關閉SELinux
運行 getenforce 命令查看SELinux的當前狀態(tài)。
如果SELinux狀態(tài)參數(shù)是Disabled,則SELinux為關閉狀態(tài);如果SELinux狀態(tài)參數(shù)是Enforcing,則SELinux為開啟狀態(tài)。
# 臨時關閉
setenforce 0# 永久關閉
vi /etc/selinux/config
找到SELINUX=enforcing,按i進入編輯模式,將參數(shù)修改為SELINUX=disabled。
2.2 nginx安裝
- 安裝Nginx
yum -y install nginx
- 查看Nginx版本。
# nginx -v
nginx version: nginx/1.20.1
2.3 mysql安裝
- 更新Yyum源
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
- 安裝MySQL。
注:使用的操作系統(tǒng)內核版本為el8,可能會提示報錯信息No match for argument。您需要先運行命令yum module disable mysql禁用默認的MySQL模塊,再安裝MySQL。
yum -y install mysql-community-server --nogpgcheck
- 查看mysql版本號
返回結果如下所示,表示MySQL安裝成功。
# mysql -V
mysql Ver 14.14 Distrib 5.7.41, for Linux (x86_64) using EditLine wrapper
- 啟動mysql
systemctl start mysqld
- 設置開機啟動mysql
systemctl enable mysqld
systemctl daemon-reload
2.4 php安裝
- 更新yum源
yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
- 添加Webtatic源
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
- 安裝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
- 查看php版本。
# php -v
PHP 7.0.33 (cli) (built: Dec 6 2018 22:30:44) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologieswith Zend OPcache v7.0.33, Copyright (c) 1999-2017, by Zend Technologies
2.5 nginx配置
- nginx 主配置文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;
}http {log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 4096;include /etc/nginx/mime.types;default_type application/octet-stream;include /etc/nginx/conf.d/*.conf;
}
- conf 文件配置
server {listen 80;server_name 域名;charset utf-8;location / {root /usr/share/nginx/xxx;try_files $uri $uri/ /index.html;index index.html index.htm index.php;}location = /50x.html {root /usr/share/nginx/html/xxx;}location ~ .php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}access_log /var/log/nginx/xxx.access.log main;error_log /var/log/nginx/xxx.error.log warn;#如果需要http強制跳轉至https,則開啟
# rewrite ^(.*)$ https://$host$1 permanent;}
server {listen 443 ssl;server_name 域名;charset utf-8;ssl_certificate /etc/nginx/cert/xxx.pem;ssl_certificate_key /etc/nginx/cert/xxx.key;ssl_session_timeout 5m;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;ssl_protocols TLSv1.2;ssl_prefer_server_ciphers on;location / {root /usr/share/nginx/xxx;try_files $uri $uri/ /index.html;index index.html index.htm index.php;}location = /50x.html {root /usr/share/nginx/html/xxx;}location ~ .php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}access_log /var/log/nginx/xxx.access.log main;error_log /var/log/nginx/xxx.error.log warn;
}
- 檢查配置文件是否正確
nginx -t
- 生效nginx配置文件
nginx -s reload
2.6 mysql配置
查看/var/log/mysqld.log文件,獲取并記錄root用戶的初始密碼。
grep 'temporary password' /var/log/mysqld.log
- 配置mysql的安全性。
mysql_secure_installation
輸入MySQL的初始密碼。
說明 在輸入密碼時,系統(tǒng)為了最大限度的保證數(shù)據(jù)安全,命令行將不做任何回顯。只需要輸入正確的密碼信息,然后按Enter鍵即可。
Securing the MySQL server deployment.Enter password for user root: #輸入上一步獲取的root用戶初始密碼
為mysql設置新密碼。
The existing password for the user account root has expired. Please set a new password.New password: #輸入新密碼。長度為8至30個字符,必須同時包含大小寫英文字母、數(shù)字和特殊符號。特殊符號包含()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/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) :Y #您需要輸入Y以確認使用新密碼。#新密碼設置完成后,需要再次驗證新密碼。
New password:#再次輸入新密碼。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,再次確認使用新密碼。
- 輸入Y刪除匿名用戶
Remove anonymous users? (Press y|Y for Yes, any other key for No) :Y
Success.
- 輸入Y禁止使用root用戶遠程登錄mysql
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :Y
Success.
- 輸入Y刪除test庫以及用戶對test庫的訪問權限
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :Y- Dropping test database...
Success.- Removing privileges on test database...
Success.
- 輸入Y重新加載授權表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :Y
Success.All done!
2.7 配置php
- 創(chuàng)建phpinfo.php文件
<網(wǎng)站根目錄>是您在nginx.conf配置文件中l(wèi)ocation ~ .php$大括號內,配置的root參數(shù)值,如下圖所示。網(wǎng)站根目錄本文配置的網(wǎng)站根目錄為/usr/share/nginx/html,因此需要運行以下命令新建phpinfo.php文件:
vim /usr/share/nginx/html/phpinfo.php輸入下列內容,函數(shù)phpinfo() 會展示PHP的所有配置信息。
<?php echo phpinfo(); ?>
- 啟動php-fpm
systemctl start php-fpm
- 設置php-fpm開機自啟動
systemctl enable php-fpm
- 測試訪問LNMP配置信息頁面
在本地Windows主機或其他具有公網(wǎng)訪問能力的Windows主機中,打開瀏覽器。
在瀏覽器的地址欄輸入http://域名/phpinfo.php進行訪問。
訪問結果為php測試頁即為成功
注:測試訪問LNMP配置信息頁面后,建議將phpinfo.php文件刪除,消除數(shù)據(jù)泄露風險。
rm -rf /usr/share/nginx/html/phpinfo.php
三、常見問題
3.1 安裝其它版本的nginx服務
- 下載nginx 1.21.3
wget http://nginx.org/download/nginx-1.21.3.tar.gz
- 安裝Nginx相關依賴。
yum install -y gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
- 解壓nginx 安裝包
tar zxvf nginx-1.21.3.tar.gz
cd nginx-1.21.3
- 編譯源碼
./configure \--user=nobody \--group=nobody \--prefix=/usr/local/nginx \--with-http_stub_status_module \--with-http_gzip_static_module \--with-http_realip_module \--with-http_sub_module \--with-http_ssl_module
make && make install
- 啟動nginx
/usr/local/nginx/sbin/nginx
3.2 php版本過低
以上安裝中,php默認安裝的版本是 PHP 7.0.33 ,在某些時候,php的版本過低,將導致LNMP環(huán)境出現(xiàn)很多問題,那么我們就需要升級php的版本
- 卸載php
# 查看當前php已安裝的包
rpm -qa|grep php 會出現(xiàn)很多php相關包,基本只需要卸載幾個名為common的包即可,其他同版本依賴會被全部刪除,
刪除php70w-common,70w版本的依賴包全部會被刪除。yum remove php70w-common
yum remove php74w-common
安裝 PHP7.2
- 安裝 EPEL 軟件包
yum install epel-release
- 安裝 remi 源
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
- yum 擴展包:
yum install yum-utils
啟用 remi 倉庫:
yum-config-manager --enable remi-php72
yum update
- 安裝 PHP7.2
yum install php72
安裝 php-fpm 和一些其他模塊
yum install php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache
- php72 -v 查看安裝結果
# php72 -v
PHP 7.2.34 (cli) (built: Dec 19 2022 16:12:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologieswith Zend OPcache v7.2.34, Copyright (c) 1999-2018, by Zend Technologies
- 設置開機自啟
systemctl enable php72-php-fpm.service
- 常用 php-fpm 命令
# 開啟服務
systemctl start php72-php-fpm.service
# 停止服務
systemctl stop php72-php-fpm.service
# 查看狀態(tài)
systemctl status php72-php-fpm.service
- 設置php與nginx為同一個用戶名及用戶組
egrep '^(user|group)' /etc/nginx/nginx.conf# 結果示例:
user nginx;# 編輯 /etc/opt/remi/php72/php-fpm.d/www.conf,修改執(zhí)行 php-fpm 的權限:vim /etc/opt/remi/php72/php-fpm.d/www.conf# 設置用戶和用戶組為 nginx:user = nginx
group = nginx# 保存并關閉文件,重啟 php-fpm 服務:systemctl restart php72-php-fpm.service
- 路徑參考
# php 安裝路徑
/etc/opt/remi/php72# nginx 配置文件
/etc/nginx/nginx.conf# nginx 默認項目路徑
/usr/share/nginx/html