yum安裝Nginx、nginx虛擬主機、安裝WordPress、安裝discuz

yum安裝Nginx

使用源碼包安裝Nginx須要不少步驟,咱們可使用yum的方式來安裝Nginx。能夠跟簡單明瞭。php

[root@localhost ~]# ps aux |grep nginx
root       7373  0.0  0.1  45936  1124 ?        Ss   21:44   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     7374  0.0  0.1  46388  1904 ?        S    21:44   0:00 nginx: worker process
root       7376  0.0  0.0 112708   980 pts/0    R+   21:44   0:00 grep --color=auto nginx
[root@localhost ~]# pkill nginx
[root@localhost ~]# ps aux |grep nginx
root       7379  0.0  0.0 112708   980 pts/0    R+   21:44   0:00 grep --color=auto nginx
  • 刪掉以前編譯安裝的Nginx
[root@localhost ~]# rm -rf /usr/local/nginx/
  • 搭建yum倉庫
vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
  • 查看yum倉庫是否生效
[root@localhost ~]# yum list |grep nginx
BDB2053 Freeing read locks for locker 0x5: 7383/140362401236800
nginx.x86_64                                1:1.14.2-1.el7_4.ngx       nginx    
nginx-debug.x86_64                          1:1.8.0-1.el7.ngx          nginx    
nginx-debuginfo.x86_64                      1:1.14.2-1.el7_4.ngx       nginx    
nginx-module-geoip.x86_64                   1:1.14.2-1.el7_4.ngx       nginx    
nginx-module-geoip-debuginfo.x86_64         1:1.14.2-1.el7_4.ngx       nginx    
nginx-module-image-filter.x86_64            1:1.14.2-1.el7_4.ngx       nginx    
nginx-module-image-filter-debuginfo.x86_64  1:1.14.2-1.el7_4.ngx       nginx    
nginx-module-njs.x86_64                     1:1.14.2.0.2.7-1.el7_4.ngx nginx    
nginx-module-njs-debuginfo.x86_64           1:1.14.2.0.2.7-1.el7_4.ngx nginx    
nginx-module-perl.x86_64                    1:1.14.2-1.el7_4.ngx       nginx    
nginx-module-perl-debuginfo.x86_64          1:1.14.2-1.el7_4.ngx       nginx    
nginx-module-xslt.x86_64                    1:1.14.2-1.el7_4.ngx       nginx    
nginx-module-xslt-debuginfo.x86_64          1:1.14.2-1.el7_4.ngx       nginx    
nginx-nr-agent.noarch                       2.0.0-12.el7.ngx           nginx    
pcp-pmda-nginx.x86_64                       4.1.0-5.el7_6              updates
  • 使用yum安裝Nginx
[root@localhost ~]# yum install nginx -y
  • 可使用nginx -v 命令來查看Nginx版本。-V查看Nginx的參數。
[root@localhost ~]# nginx -v
nginx version: nginx/1.14.2
[root@localhost ~]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
  • 配置文件路徑是/etc/nginx/nginx.conf
[root@localhost ~]# ls /etc/nginx/nginx.conf 
/etc/nginx/nginx.conf
  • 查看配置文件有沒有語法錯誤,使用nginx -t
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
  • 啓動nginx
[root@localhost ~]# systemctl start nginx
  • 若是提示錯誤,則運行以下命令
Job for nginx.service failed because a configured resource limit was exceeded. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@localhost init.d]# sudo netstat -lnp | grep 0.0.0.0:80

Nginx虛擬主機

在一個web服務上能跑多個站點,每一個站點就是一個虛擬主機。html

[root@localhost /]# vi /etc/nginx/nginx.conf         打開Nginx配置文件
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;      這個就是虛擬主機配置文件的路徑。
  • 能夠看到,默認在這個路徑下有一個default.conf的文件
[root@localhost /]# ls /etc/nginx/conf.d/
default.conf
  • 查看虛擬主機配置文件,定義虛擬主機配置文件,以域名爲命名
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;
  • 這時候能夠經過ip地址來訪問該站點下的內容,是能夠訪問到的,若是訪問不到,能夠看一下是否是防火牆的安全策略的問題。iptables -nvL 查看防火牆規則
  • iptables -nvL 查看防火牆規則,若是確認是防火牆的問題,能夠將80端口寫入白名單。
firewall-cmd --add-port=80/tcp --permanent          將80端口寫入安全策略。
firewall-cmd --reload                   從新加載firewalld
  • Nginx配置文件
server {
    listen       80;
    server_name www.gongzi.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/gongzi;
        index  index.html index.htm;
    }
  • 檢查配置有無錯誤。
nginx -t //查看配置文件是否有錯誤
nginx -s reload //從新加載配置文件
systemctl restart nginx  //重啓nginx
  • 經過修改hosts文件使域名生效,使用域名訪問

默認虛擬主機

排在第一位的虛擬主機配置文件就是默認虛擬主機,或者是在配置文件中listen 80 後面添加default_server 。這樣能夠將該配置文件的站點定義成默認虛擬主機。mysql

  • 默認虛擬主機,舉個例子,abc.com能解析到該服務器,可是配置文件中並無配置abc.com的域名,照樣能夠訪問到默認虛擬主機的內容。添加一行deny all能夠取消默認虛擬主機帶來的泛解析

補充

Ctrl+z能夠暫停一個進程,按fg能夠再次回到進程。linux

新建一個站點blog

  • 新建一個blog.com.conf的虛擬主機配置文件
server {
    listen       80;
    server_name  blog.abc.com;

    #access_log  /var/log/nginx/host.access.log  main;
    #charset koi8-r;

    location / {
        root   /data/wwwroot/blog.abc.com;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/wwwroot/blog.abc.com;
    }
    location ~ \.php$ {
        root           /data/wwwroot/blog.abc.com;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /data/wwwroot/blog.abc.com$fastcgi_script_name;
        include        fastcgi_params;
    }


}
  • 建立相應的目錄
[root@localhost conf.d]# mkdir -p /data/wwwroot/blog.abc.com
  • 驗證配置文件是否有錯並從新加載
[root@localhost conf.d]# nginx -t
[root@localhost conf.d]# nginx -s reload

安裝WordPress

[root@localhost blog.abc.com]# wget https://cn.wordpress.org/wordpress-5.0.2-zh_CN.tar.gz
  • 將下載的壓縮包解壓
[root@localhost blog.abc.com]# tar zxvf wordpress-5.0.2-zh_CN.tar.gz
  • 將解壓的目錄下的全部東西複製到當前目錄,而後將壓縮包和原目錄刪掉。
[root@localhost blog.abc.com]# cp wordpress/* ./
cp: omitting directory ‘wordpress/wp-admin’
cp: omitting directory ‘wordpress/wp-content’
cp: omitting directory ‘wordpress/wp-includes’
[root@localhost blog.abc.com]# ls
index.php    wordpress                     wp-blog-header.php    wp-cron.php        wp-login.php     wp-signup.php
license.txt  wordpress-5.0.2-zh_CN.tar.gz  wp-comments-post.php  wp-links-opml.php  wp-mail.php      wp-trackback.php
readme.html  wp-activate.php               wp-config-sample.php  wp-load.php        wp-settings.php  xmlrpc.php
[root@localhost blog.abc.com]# rm -rf wordpress wordpress-5.0.2-zh_CN.tar.gz

  • 建立數據庫設置數據庫(先鏈接到MariaDB)
[root@localhost blog.abc.com]# mysql -uroot -ppassword
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 66
Server version: 10.3.12-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database blog;
  • 在MariaDB中爲blog數據庫建立用戶
grant all on blog.* to 'blog'@'127.0.0.1' identified by 'pbxfuej3LR4r';
  • 切換到blog庫,查詢blog庫裏有什麼標
use blog;
show tables;

安裝discuz

yum install git -y
  • 克隆discuz
git clone https://gitee.com/ComsenzDiscuz/DiscuzX.git
  • 複製upload裏面的內容到咱們自定義的路徑下
[root@localhost DiscuzX]# cp -r upload /data/wwwroot/bbs.abc.site
  • 定義主機配置文件
server {
    listen       80;
    server_name bbs.abc.site;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /data/wwwroot/bbs.abc.site;
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
        root           /data/wwwroot/bbs.abc.site;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /data/wwwroot/bbs.abc.site$fastcgi_script_name;
        include        fastcgi_params;
    }

}
  • 從新加載配置文件並檢查有無錯誤
[root@localhost conf.d]# nginx -s reload
[root@localhost conf.d]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost nginx]# setenforce 0

  • 修改目錄屬主權限。
[root@localhost bbs.abc.site]# chown -R php-fpm  config data uc_server/data  uc_client/data
[root@localhost bbs.abc.site]# chown -R php-fpm  config data uc_server/data  uc_client/data
相關文章
相關標籤/搜索