CentOS7下搭建基本LNMP環境,部署WordPress

系統環境:CentOS Linux release 7.4.1708 (Core) 3.10.0-693.el7.x86_64php

軟件版本:nginx-1.12.2.tar.gzhtml

     php 7.1.11mysql

     mysql-community-server-5.7.20-1.el7.x86_64nginx

網絡環境中配置了DNS服務器,本次nginx服務器的IP是192.168.1.20,域名是wp1.st.local。過程當中所有使用域名配置或訪問。web

1、安裝nginx

一、安裝依賴及相關支持庫sql

# yum install pcre-devel pcre openssl openssl-devel

二、編譯安裝nginx數據庫

# cd nginx-1.12.2
# ./configure \
  --prefix=/opt/nginx \
  --sbin-path=/usr/sbin/nginx \
  --user=nginx \
  --group=nginx \
 --pid-path=/var/run/nginx.pid \
 --lock-path=/var/run/nginx.lock \
  --with-http_ssl_module \
  --with-http_stub_status_module \
  --with-pcre \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --with-http_gzip_static_module \
  --with-file-aio \
  --with-http_realip_module

三、編譯成功vim

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/opt/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/opt/nginx/modules"
  nginx configuration prefix: "/opt/nginx/conf"
  nginx configuration file: "/opt/nginx/conf/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

四、安裝安全

# make && make install

五、檢驗安裝結果服務器

# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --sbin-path=/usr/sbin/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --with-file-aio --with-http_realip_module --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock

六、建立nginx用戶

# useradd -M -s /sbin/nologin nginx

七、建立nginx的unit

注意文件路徑

# vi /usr/lib/systemd/system/nginx.service

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

八、防火牆

# iptables -I INPUT 5 -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# iptables-save >/etc/sysconfig/iptables

 九、配置nginx.conf語法高亮

# wget http://www.vim.org/scripts/download_script.php?src_id=14376 -O /usr/share/vim/vim74/syntax/nginx.vim
# vi /usr/share/vim/vim74/filetype.vim
#追加一行,注意nginx的配置文件路徑
au BufRead,BufNewFile /opt/nginx/conf/* set ft=nginx

 十、修改配置文件

# vi /opt/nginx/conf/nginx.conf

user  nginx;
worker_processes  auto;

    server {
        listen       80;
        server_name  wp1.st.local;        #配置域名
        location / {
            root   html;
            index  index.php index.html index.htm;        #增長index.php
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

十一、啓動服務

# systemctl start nginx.service
# systemctl enable nginx.service

十二、訪問測試

 2、安裝PHP

一、添加擴展源

# yum -y install https://mirror.webtatic.com/yum/el7/epel-release.rpm https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

二、安裝支持庫

# yum install libxml2 libxml2-devel libpng libpng-devel libcurl-devel libcurl libzip-devel libzip gd bzip2-devel bzip2

三、安裝PHP7.1及相關支持

# yum -y install mod_php71w php71w-mbstring php71w-pear php71w-fpm php71w-gd php71w-pdo 

php71w-mysql放在安裝MySQL時一塊兒安裝,由於在未添加mysql57源的時候,可能會安裝mariadb-libs做爲mysql-community-libs的替代。

四、修改配置文件

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

#指定用戶爲前面建立的nginx
user = nginx
group = nginx

修改nginx.conf,root最好填寫絕對路徑,document_root就是root選項的指定目錄

#添加
location ~ \.php$ {
            root           /opt/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

修改所屬用戶爲nginx

# chown -R nginx. /var/lib/php
# chown -R nginx. /opt/nginx/html

五、啓動php-fpm,重載nginx

# systemctl start php-fpm.service 
# systemctl enable php-fpm.service
# systemctl reload nginx.service

六、訪問測試

# /opt/nginx/html/index.php

<?php
    phpinfo();
?>

注意權限

# chown nginx. index.php

3、安裝MySQL5.7

一、添加擴展源

# vi /etc/yum.repos.d/mysql-community.repo

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://repo.mysql.com/RPM-GPG-KEY-mysql

二、安裝MySQL

# yum -y install mysql-community-server php71w-mysql

三、啓動並配置

# vi /etc/my.cnf
#添加一行
character-set-server=utf8
# systemctl start mysqld

經過mysql_secure_installation命令按提示設置root密碼以及基礎的MySQL配置

四、建立數據庫和新用戶

mysql> create database wordpress;    
mysql> create user 'wp_user'@'%' identified by 'xxxxxxxx';    #建立用戶
mysql> grant all privileges on wordpress.* to 'wp_user'@'%' identified by 'xxxxxxxx';    #受權
mysql> flush privileges;

五、重置MySQL的用戶密碼

若是忘記MySQL密碼,就使用以下辦法重置。須要root權限

#vi /etc/my.cnf
[mysqld]
skip-grant-tables
#mysql -uroot
mysql> use mysql;
mysql> update user set authentication_string=password('xxxxxxxx') where user='root';    #5.7版本的密碼字段是authentication_string,而不是password
mysql> flush privileges;

修改完後註釋掉skip-grant-tables,重啓mysqld.service

 4、安裝WordPress

一、下載並解壓

# wget https://cn.wordpress.org/wordpress-4.9.1-zh_CN.zip
# unzip wordpress-4.9.1-zh_CN.zip
# cp -rva wordpress/* /opt/nginx/html/

二、修改用戶和組

# chown nginx. /opt/nginx/html

三、SELINUX

# cd /opt/nginx
# chcon -R -t httpd_sys_content_t html/
# cd html/
# chcon -R -t httpd_sys_rw_content_t wp-content/

經過編譯安裝的nginx與yum安裝的的安全上下文有所不一樣,yum安裝會自動配置正確的上下文,編譯安裝默承認能是admin_home_t,訪問時會提示File not found,必須改成httpd_sys_content_t

四、按照提示部署WordPress

5、配置nginx虛擬機

一、添加配置

虛擬server均可以配置爲80端口,配置不一樣的域名

# mkdir /opt/nginx/conf.d
# vi /opt/nginx/conf.d/virtual1.conf
server {
    listen       80;
    server_name  bbs.st.local;

    location / {
        root   /opt/nginx/virtual;
        index  index.html index.htm;
    }
}
#在nginx.conf全局區塊中包含配置文件
# vi /opt/nginx/conf/nginx.conf
include /opt/nginx/conf.d/*.conf;

二、建立虛擬主機目錄並修改權限

# mkdir /opt/nginx/virtual
# vi /opt/nginx/virtual/index.html
Nginx Virtual Host Test Page
# chown -R nginx. /opt/nginx/virtual/
# chcon -R -t httpd_sys_content_t /opt/nginx/virtual/

三、重載服務

# systemctl reload nginx.service

四、結果檢測

相關文章
相關標籤/搜索