操作系統:Centos7.4
數據庫:Mariadb5.5
php版本:php-fpm
搭建Nginx服務
準備工作
1、關閉防火墻及selinux
[root@Nginx ~]# systemctl disable firewall
[root@Nginx ~]# systemctl stop firewall
[root@Nginx ~]#sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
2、安裝開發組件(yum源安裝)
[root@Mariadb ~]# yum grouplist
Loaded plugins: fastestmirror
Available Environment Groups:
Minimal Install
Compute Node
Infrastructure Server
File and Print Server
Basic Web Server
Virtualization Host
Server with GUI
GNOME Desktop
KDE Plasma Workspaces
Development and Creative Workstation
Available Groups:
Compatibility Libraries
Console Internet Tools
Development Tools
Graphical Administration Tools
Legacy UNIX Compatibility
Scientific Support
Security Tools
Smart Card Support
System Administration Tools
System Management
Done
[root@Nginx ~]#yum groupinstall -y 'Development tools'
3、下載Nginx安裝包
這里是Nginx官方下載頁面download_links,本次安裝Nginx1.8穩定版
[root@Nginx ~]#tar -zxvf nginx-1.8.1.tar.gz
4、編譯安裝 編譯之前需要安裝一些依賴包,否則編譯過程會報錯:
[root@Nginx nginx-1.8.1]# yum install -y gd pcre-devel openssl-devel zlib-devel
[root@Nginx nginx-1.8.1]# ./configure \
--prefix=/usr/local/nginx1.8 \
--conf-path=/etc/nginx.conf \
--error-log-path=/var/log/nginx/nginx.log \
--pid-path=/var/run/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_image_filter_module \
--with-http_gzip_static_module \
--with-http_auth_request_module
配置環境變量,并加載配置文件啟動服務
[root@Nginx nginx-1.8.1]#export PATH=/usr/local/nginx1.8/sbin:$PATH
[root@Nginx ~]# nginx -c /etc/nginx.conf /*指定加載配置文件,否則會報錯
[root@Nginx ~]# nginx -t
nginx: the configuration file /etc/nginx.conf syntax is ok
nginx: configuration file /etc/nginx.conf test is successful
[root@Nginx ~]#nginx -s reopen
5、訪問測試頁
http://SERVERIP/index.html
安裝PHP-FPM服務
因為Nginx本身直接僅支持FastCGI,所以,相應的php調用需要php-fpm
1、安裝php-fpm(配置本地yum源安裝)
[root@Nginx nginx-1.8.1]#yum install php-fpm
[root@Nginx nginx-1.8.1]#systemctl start php-fpm.service
本次實驗將php-fpm與Nginx安裝在同一臺服務器上,故需要修改配置;
2、下面我們進行測試配置
配置nginx支持FastCGI(參照以下內容),將.php模塊的#號去掉
[root@Nginx ~]#vi /etc/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm; /*添加index.php
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx1.8/html/$fastcgi_script_name;
include fastcgi_params;
}
location /favicon.ico {
root html;
}
}
其中fastcgi_param為php文件所在的絕對路徑
在html路徑下新建一個index.php測試頁內容如下:
[root@Nginx ~]#cd /usr/local/nginx1.8/html
[root@localhost html]#vi index.php
<?php
phpinfo();
?>
[root@Nginx ~]#systemctl restart php-fpm
[root@Nginx ~]#nginx -s reload
3、打開php測試頁,便可看到phpinfo()測試頁
http://$SERVERIP/index.php
安裝Mariadb數據庫
[本次安裝僅適用yum源安裝]Mariadb官方的安裝已經給的很全了,已經有針對systemd的編譯的完整包,可直接下載使用
[root@Nginx ~]#yum install mariadb-server
[root@Nginx ~]#systemctl start mariadb.service
[root@Nginx ~]#mysql -uroot
MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> alter user set password=password('xxxxx') where User='root'; /*設置root密碼
MariaDB [mysql]> flush privileges;
MariaDB [mysql]> grant all privileges on mysql.* to 'root'@'%'; /*root用戶賦權,可以從任意主機遠程訪問數據庫,當數據庫與web服務器不在同一臺主機時有效
MariaDB [mysql]> flush privileges;
更改root密碼也可以使用命令mysqladmin實現
[root@Nginx ~]#mysqladmin -u root -p password xxxxxxx
測試php是否可以訪問數據庫
[root@Nginx ~]#vim /usr/local/nginx1.8/html/index.php
<?php
$link=mysql_connect('localhost','root','xxxxxx');
if ($link)
echo 'mysql is running!';
else
echo 'mysql isn't connected!';
?>
[root@Nginx ~]#nginx -s reload
瀏覽器打開 http://$SERVERIP/index.php
部署wordpress
1、Wordpress官方下載最新版本linux安裝包WordPress4.8
[root@Nginx ~]#tar -zxvf wordpress4.8.2.tar.gz
[root@Nginx ~]#mv wordpress4.8.2/* /usr/local/nginx1.8/html/
[root@Nginx ~]#cd /usr/local/nginx1.8/html/
[root@Nginx html]#cp wp-config-sample.php wp-config.php
[root@Nginx html]#vi wp-config.php
// ** MySQL 設置 - 具體信息來自您正在使用的主機 ** //
/** WordPress數據庫的名稱 */
define('DB_NAME', 'mysql');
/** MySQL數據庫用戶名 */
define('DB_USER', 'root');
/** MySQL數據庫密碼 */
define('DB_PASSWORD', '$MYSQLPASSWORD');
/** MySQL主機 */
define('DB_HOST', '$DATABASEIP');
/** 創建數據表時默認的文字編碼 */
define('DB_CHARSET', 'utf8');
/** 數據庫整理類型。如不確定請勿更改 */
define('DB_COLLATE', '');
……
……
……
/* 如果沒有ftp服務器的話,需要在配置文件添加如下代碼,以保證安裝主題時會提示先登陸ftp服務器
define('WP_TEMP_DIR', ABSPATH.'wp-content/tmp');
define("FS_METHOD", "direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);
2、修改/etc/nginx.conf配置文件,以至于能正確解析wordpress的php文件
[root@Nginx ~]#vi /etc/nginx/
location ~ \.php$ {
root /usr/local/nginx1.8/html/wordpress; /*wordpress的目錄,如果是html根目錄,則默認即可
fastcgi_pass 127.0.0.1:9000; /*本地php-fpm監聽地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx1.8/html/wordpress$fastcgi_script_name;
include fastcgi_params;
}
……
……
location \favicon.ico {
root html; /*防止nginx找不到favicon.ico文件而打開頁面失敗
}
[root@Nginx ~]#nginx -t
[root@Nginx ~]#nginx -s reload
現在,找個瀏覽器就可以瀏覽wordpress,簡單的安裝即可開始個性的博客之旅。
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/88183