CentOS 下安裝 LEMP 服務(Nginx、MariaDB/MySQL 和PHP)

本文環境基於 CentOS 7 php

1)安裝 Nginxhtml

# yum install nginx
# systemctl enable nginx.service
# systemctl start nginx.servicemysql

2)安裝 mariadbnginx

# yum install mariadb-server
# systemctl enable mariadb
# systemctl start mariadbsql

 

安裝完成後,首次啓動應先運行腳本:shell

# mysql_secure_installation

這是爲數據庫服務器進行一些安全強化措施,包括設置(非空)的 root 密碼、刪除匿名用戶、鎖定遠程訪問。數據庫

命令以下:json

# mysql_secure_installation 

3)安裝 PHPvim

LEMP的安裝,至少須要 PHP-FPM 和 PHP-MySQL 兩個模塊。瀏覽器

PHP-FPM(FastCGI 進程管理器)實現的是 nginx 服務器和生成動態內容的 PHP 應用程序的訪問接口。

PHP-MySQL 模塊使 PHP 程序能訪問 MariaDB/MySQL 數據庫。

其它模塊根據實際狀況選用

# yum install php php-fpm php-mysql php-gd php-mbstring php-mcrypt 

 

4) 配置和啓動 PHP-FPM

# vim /etc/php-fpm.d/www.conf

將其中的 user 和 group 部分改成nginx

user = nginx
group = nginx

而後啓動 PHP-FPM

# sudo systemctl start php-fpm
# sudo systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

繼續,調整 PHP 的安全配置選項,在 /etc/php.ini 文件中增長如下兩行:

cgi.fix_pathinfo=0
date.timezone ="Asia/Shanghai"

第二行定義的是 PHP 中日期/時間相關函數使用相關的默認時區。使用本文最後《PHP的時區配置》中提到的方法,設置您所在的時區,並設置相應 date.timezone 的值。

調整完成後:

# systemctl restart nginx
# systemctl restart php-fpm

 

5) 永久關閉 HTTPD 服務

由於安裝php的時候,系統默認把HTTPD 也給安裝上了,爲確保安全永久關閉該服務。

# systemctl disable httpd


6)最後重啓相關服務,更新配置

# systemctl restart nginx
# systemctl restart php-fpm

 

7) 配置 nginx 中的網站

配置文件以下:

server {
    listen 80; server_name www.example.com; root /var/www/html/example; index index.php index.html index.htm; location / { if (-f $request_filename/index.html) { rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php) { rewrite (.*) $1/index.php; } if (!-f $request_filename) { rewrite (.*) /index.php; } try_files $uri $uri/ = 404; } # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { } # nginx passes PHP scripts to FastCGI server via a TCP/9000 socket # this setting much be consistent with /etc/php-fpm.d/www.conf # try_files prevents nginx from passing bad scripts to FastCGI server location ~ \.php$ { try_files $uri = 404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } server { server_name example.com; return 301 $scheme://www.example.com$request_uri; }

 注意,須要把缺省配置中的衝突段落註釋掉。

 

8) 測試服務器配置

在 /var/www/html/ 目錄下,添加文件index.php ,內容以下:

<?php phpinfo();?>

保存後,在瀏覽器中輸入 http://www.example.com/ 查看顯示。

 

9)PHP 的時區配置(timezone)

PHP的缺省時區在php.ini 文件中配置,首先定位配置文件的所在:

# php --ini
Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed:      /etc/php.d/curl.ini,
/etc/php.d/fileinfo.ini,
/etc/php.d/gd.ini,
/etc/php.d/json.ini,
/etc/php.d/mbstring.ini,
/etc/php.d/mcrypt.ini,
/etc/php.d/mysql.ini,
/etc/php.d/mysqli.ini,
/etc/php.d/pdo.ini,
/etc/php.d/pdo_mysql.ini,
/etc/php.d/pdo_sqlite.ini,
/etc/php.d/phar.ini,
/etc/php.d/sqlite3.ini,
/etc/php.d/zip.ini
# 

命令回顯的第二行顯示配置文件位於:/etc/php.ini ,接下來肯定咱們的時區標記(timezone,TZ),使用 tzselect 命令:

# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
 1) Africa
 2) Americas
 3) Antarctica
 4) Arctic Ocean
 5) Asia 6) Atlantic Ocean
 7) Australia
 8) Europe
 9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5
Please select a country.
 1) Afghanistan          18) Israel            35) Palestine
 2) Armenia          19) Japan            36) Philippines
 3) Azerbaijan          20) Jordan            37) Qatar
 4) Bahrain          21) Kazakhstan        38) Russia
 5) Bangladesh          22) Korea (North)        39) Saudi Arabia
 6) Bhutan          23) Korea (South)        40) Singapore
 7) Brunei          24) Kuwait            41) Sri Lanka
 8) Cambodia          25) Kyrgyzstan        42) Syria
 9) China          26) Laos            43) Taiwan
10) Cyprus          27) Lebanon            44) Tajikistan
11) East Timor          28) Macau            45) Thailand
12) Georgia          29) Malaysia            46) Turkmenistan
13) Hong Kong          30) Mongolia            47) United Arab Emirates
14) India          31) Myanmar (Burma)        48) Uzbekistan
15) Indonesia          32) Nepal            49) Vietnam
16) Iran          33) Oman            50) Yemen
17) Iraq          34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time 2) Xinjiang Time
#? 1

The following information has been given:

    China
    Beijing Time

Therefore TZ='Asia/Shanghai' will be used.
Local time is now:    Thu Mar 29 10:25:09 CST 2018.
Universal Time is now:    Thu Mar 29 02:25:09 UTC 2018.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
    TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
# 

咱們的時區即爲: Asia/Shanghai

如今打開PHP的配置初始化文件 php.ini 找到並修改以下內容:

date.timezone = "Asia/Shanghai"

肯定後保存並重啓相關服務。

相關文章
相關標籤/搜索