CentOS上安装Nginx、PHP、MySQL

 

一、编译安装Nginx

# cd /usr/local/src

# wget http://nginx.org/download/nginx-1.16.0.tar.gz

# tar -zxvf nginx-1.16.0.tar.gz

# cd nginx-1.16.0

# yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/run/nginx.pid --lock-path=/usr/local/nginx/run/nginx.lock --http-client-body-temp-path=/usr/local/nginx/run/client_body_temp --http-proxy-temp-path=/usr/local/nginx/run/proxy_temp --http-fastcgi-temp-path=/usr/local/nginx/run/fastcgi_temp --http-uwsgi-temp-path=/usr/local/nginx/run/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/run/scgi_temp --http-log-path=/usr/local/nginx/log/access.log --error-log-path=/usr/local/nginx/log/error.log --with-debug --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_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-pcre --with-pcre-jit --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
# make
# make install

安装完毕,启动/关闭/重启Nginx命令如下:

# /usr/local/nginx/sbin/nginx //启动
# /usr/local/nginx/sbin/nginx -s stop //关闭
# /usr/local/nginx/sbin/nginx -s reload //重启
附带:查看启动状态
# ps -ef | grep nginx

 

设置Nginx开机启动(参考自:https://www.jianshu.com/p/ca5ee5f7075c

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

# 在文件中写入启动脚本
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存上述脚本文件,并执行如下命令,完成Nginx开机启动设置:

# systemctl enable nginx.service

重启系统

# reboot

查看Nginx是否开机启动成功,在浏览器输入:http://localhost

为了让nginx命令有效,将nginx添加到系统环境变量中:

# vim /etc/profile

在profile文件中添加如下两行代码:

 

PATH=$PATH:/usr/local/nginx/sbin
export PATH

 

保存退出/etc/profile文件,执行如下命令让profile立即生效:

# source /etc/profile

 

二、编译安装PHP 7.2

# cd /usr/local/src

# wget https://www.php.net/distributions/php-7.2.20.tar.gz

# tar -zxvf php-7.2.20.tar.gz

# cd php-7.2.20

# yum install -y gcc gcc-c++ make automake autoconf gd file bison patch mlocate flex diffutils zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel libcurl libcurl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers openldap-devellibxslt-devel kernel-devel libtool-libs readline-devel gettext-devel libcap-devel php-mcrypt libmcrypt libmcrypt-devel recode-devel gmp-devel icu libxslt libxslt-devel php-devel

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql-sock --with-mysqli --with-libxml-dir --with-openssl --with-mhash --with-pcre-regex --with-zlib --with-iconv --with-bz2 --with-curl --with-cdb --with-pcre-dir --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --with-gettext --with-gmp --with-mhash --with-libmbfl --with-onig --with-pdo-mysql --with-zlib-dir --with-readline --with-libxml-dir --with-xsl --with-pear --enable-fpm --enable-soap --enable-bcmath --enable-calendar --enable-dom --enable-exif --enable-fileinfo --enable-filter --enable-ftp --enable-gd-jis-conv --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --enable-pdo --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --enable-zip --enable-mysqlnd-compression-support

# make

# make install

 

创建php-fpm.conf、www.conf配置文件:

# cd /usr/local/php/etc
# cp php-fpm.conf.default php-fpm.conf
# cd /usr/local/php/etc/php-fpm.d
# cp www.conf.default www.conf

创建php.ini配置文件:

# find /usr/local/src/php-7.2.20 -name php.ini*

# cp /usr/local/src/php-7.2.20/php.ini-production /usr/local/php/etc/php.ini

手动启动和关闭php:

# /usr/local/php/sbin/php-fpm

# /usr/bin/pkill -9 php-fpm
# pstree -p | grep php

 

设置php-fpm开机启动:

首先,关闭php-fpm进程:

# /usr/bin/pkill -9 php-fpm

然后,修改php-fpm.conf中的 [pid = /run/php-fpm.pid] 配置项,该配置在后续的php-fpm.service文件中需要用到

# vi /usr/local/php/etc/php-fpm.conf

创建php-fpm.service文件:

# vi /usr/lib/systemd/system/php-fpm.service

# 在文件中写入启动脚本

[Unit]
Description=php-fpm
After=network.target

[Service]
Type=forking
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=/usr/bin/pkill -9 php-fpm
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存上述脚本文件,并执行如下命令,完成php-fpm开机启动设置:

# systemctl enable php-fpm.service

这样就可以使用systemctl命令管理php-fpm:
# systemctl start php-fpm.service
# systemctl stop php-fpm.service

也可以使用如下命令管理php-fpm:

# service php-fpm start
# service php-fpm stop
# service php-fpm restart
# service php-fpm reload

整合Nginx + PHP:

# vi /usr/local/nginx/conf/nginx.conf

# 修改默认的server{}配置为如下内容:

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        root    "/usr/local/nginx/html";
        location / {
            index  index.html index.htm index.php l.php;
           autoindex  off;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php(.*)$  {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
}

保存文件,重启Nginx服务器:

# nginx -s reload

在server{}的root目录 /usr/local/nginx/html 中创建一个phpinfo.php文件,测试整合是否成功:

http://localhost/phpinfo.php

 

三、yum安装MySQL 5.7

参考:https://blog.csdn.net/u013517229/article/details/79412170

# 下载mysql源安装包
shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

# 安装mysql源
shell> yum localinstall mysql57-community-release-el7-8.noarch.rpm

# 检查mysql源是否安装成功
shell> yum repolist enabled | grep "mysql.*-community.*"

说明:可以修改vim /etc/yum.repos.d/mysql-community.repo源,改变默认安装的mysql版本;
比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。

# 安装MySQL
shell> yum install mysql-community-server

# 启动MySQL服务
shell> systemctl start mysqld

# 查看MySQL的启动状态
shell> systemctl status mysqld

开机启动

shell> systemctl enable mysqld
shell> systemctl daemon-reload

 

# 修改root本地登录密码
mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。
通过下面的方式找到root默认密码,然后登录mysql进行修改:
shell> grep 'temporary password' /var/log/mysqld.log
shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
或者
mysql> set password for 'root'@'localhost'=password('123456');

posted @ 2019-07-11 23:38  巅峰键盘侠  阅读(1892)  评论(0编辑  收藏  举报