1.安装
Nginx
安装依赖
yum -y install gcc gcc-c++ wget aut
omake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Em
bed pcre-devel openssl openssl-devel
创建
一个不能
登录的
Nginx运行
用户
groupadd
www-data useradd -s /sbin/nologin -g
www-data www-data
创建源码保存目录和运行时的临时
文件夹,下载
Nginx源码,当前稳定版为
Nginx-1.14.2
mkdir -p /var/cache/
Nginx mkdir -p /usr/local/src/
Nginx cd /usr/local/src/
Nginx wget -c http://
Nginx.org/download/
Nginx-1.14.2.tar.gz
解压
tar -zxvf
Nginx-1.14.2.tar.gz cd /usr/local/src/
Nginx/
Nginx-1.14.2
编译前配置检查
./con
figure \ --prefix=/usr/local/
Nginx \ --sbin-path=/usr/local/
Nginx/sbin/
Nginx \ --conf-path=/usr/local/
Nginx/conf/
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-uw
sgi-temp-path=/var/cache/
Nginx/uw
sgi_temp \ --http-scgi-temp-path=/var/cache/
Nginx/scgi_temp \ --user=
www-data \ --group=
www-data \ --with-pcre \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-mail \ --with-mail_ssl_module \ --with-file-aio \ --with-http_v2_module \ --with-threads \ --with-stream \ --with-stream_ssl_module
配置检查完毕,已经创建了Makefile
creating objs/Makefile Con
figuration summary + using threads + using sy
stem PCRE library + using sy
stem OpenSSL library + using sy
stem zlib library
Nginx path prefix: "/usr/local/
Nginx"
Nginx binary file: "/usr/local/
Nginx/sbin/
Nginx"
Nginx modules path: "/usr/local/
Nginx/modules"
Nginx con
figuration prefix: "/usr/local/
Nginx/conf"
Nginx con
figuration file: "/usr/local/
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: "/var/cache/
Nginx/client_temp"
Nginx http proxy temporary files: "/var/cache/
Nginx/proxy_temp"
Nginx http fastcgi temporary files: "/var/cache/
Nginx/fastcgi_temp"
Nginx http uw
sgi temporary files: "/var/cache/
Nginx/uw
sgi_temp"
Nginx http scgi temporary files: "/var/cache/
Nginx/scgi_temp"
编译,安装
make make install
修改配置
Nginx详细配置请移步(
nginx的configure参数,配置文件,虚拟主机配置,信号控制)
vim /usr/local/
Nginx/conf/
Nginx.conf user
www-data; pid /var/run/
Nginx.pid;
启动
Nginx
/usr/local/
Nginx/sbin/
Nginx
查看进程
ps aux|grep
Nginx
杀掉进程
pkill -9
Nginx
配置服务
vim /usr/lib/sy
stemd/sy
stem/
Nginx.service
输入如下配置
[Unit] Description=
Nginx - high performance web server Documentation=http://
Nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/var/run/
Nginx.pid ExecStart=/usr/local/
Nginx/sbin/
Nginx -c /usr/local/
Nginx/conf/
Nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID ExecStartPost=/bin/sleep 0.1 [Install] WantedBy=
multi-user.target
#注意:如果你是单核服务器,需要加ExecStartPost=/bin/sleep 0.1
#否则在执行sy
stemctl start
Nginx之后,在执行sy
stemctl status
Nginx时
#会出现sy
stemd[1]:
Failed to read PID from file /var/run/
Nginx.pid: Invalid argument
#这是因为
Nginx还未启动完成,sy
stemctl就去寻找pid进程
文件了,这是
一个已知的bug
#地址
https://bugs.launchpad.net/ubuntu/+source/nginx/+bug/1581864
重新载入 sy
stemd
sy
stemctl daemon-reload
开启开机启动
sy
stemctl enable
Nginx.service Created symlink from /etc/sy
stemd/sy
stem/
multi-user.target.wants/
Nginx.service to /usr/lib/sy
stemd/sy
stem/
Nginx.service.
启动和查看
Nginx状态
sy
stemctl start
Nginx sy
stemctl status
Nginx ●
Nginx.service -
Nginx - high performance web server Loaded: loaded (/usr/lib/sy
stemd/sy
stem/
Nginx.service; enabled;
vendor preset:
disabled) Active: active (running) since 五 2019-01-04 18:24:18 CST; 9min ago Docs: http://
Nginx.org/en/docs/ Process: 75573 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS) Process: 75576 ExecStart=/usr/local/
Nginx/sbin/
Nginx -c /usr/local/
Nginx/conf/
Nginx.conf (code=exited, status=0/SUCCESS) Main PID: 75577 (
Nginx) CGroup: /sy
stem.slice/
Nginx.service ├─75577
Nginx: master process /usr/local/
Nginx/sbin/
Nginx -c /usr/local/
Nginx/conf/
Nginx.conf └─75578
Nginx: worker process 1月 04 18:24:18 jmsiteos7 sy
stemd[1]: Stopping
Nginx - high performance web server... 1月 04 18:24:18 jmsiteos7 sy
stemd[1]: Stopped
Nginx - high performance web server. 1月 04 18:24:18 jmsiteos7 sy
stemd[1]: Starting
Nginx - high performance web server... 1月 04 18:24:18 jmsiteos7 sy
stemd[1]: Started
Nginx - high performance web server.
2.安装
mysql
注:
MysqL从5.5版本开始,通过./con
figure进行编译配置方式已经被取消,取而代之的是cmake工具。因此,我们首先要在系统中源码编译安装cmake工具。
安装依赖
yum install -y cmake gcc-c++ bison bison-devel ncurses-devel perl-Data-Dumper boost boost-doc boost-devel
创建
MysqL用户和
用户组
groupadd
MysqL useradd -g
MysqL MysqL
创建源码存放目录,下载,解压
mkdir -p /usr/local/src/
MysqL cd /usr/local/src/
MysqL wget -c https://dev.
MysqL.com/get/Downloads/
MysqL-5.7/
MysqL-boost-5.7.20.tar.gz tar -zxvf
MysqL-boost-5.7.20.tar.gz cd
MysqL-5.7.20
创建相应目录并授权
mkdir -p /usr/local/
MysqL/data chown -R
MysqL:
MysqL /usr/local/
MysqL/ touch /var/log/
MysqL.log chown -R
MysqL:
MysqL /var/log/
MysqL.log mkdir -p /var/lib/
MysqL chown -R
MysqL:
MysqL /var/lib/
MysqL mkdir -p /var/run/
MysqLd chown -R
MysqL:
MysqL /var/run/
MysqLd
配置检查
cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/
MysqL \ -D
MysqL_DATADIR=/usr/local/
MysqL/data \ -DSYS
confdIR=/etc \ -D
MysqL_USER=
MysqL \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -D
MysqL_UNIX_ADDR=/var/lib/
MysqL/
MysqL.sock \ -D
MysqL_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_unicode_ci \ -DWITH_DEBUG=0 \ -D
MysqL_MAINTAINER_MODE=0 \ -DWITH_SY
stemD=1 \ -DWITH_BOOST=boost \ -DWITH_SSL:STRING=bundled \ -DWITH_ZLIB:STRING=bundled
编译,安装
make make install
修改MysqL配置
vim /etc/my.cnf
如下设置
[
MysqL] default-ch
aracter-set=utf8 socket=/var/lib/
MysqL/
MysqL.sock [
MysqLd] init-connect = 'SET NAMES utf8' ch
aracter-set-server = utf8 basedir=/usr/local/
MysqL datadir=/usr/local/
MysqL/data socket=/var/lib/
MysqL/
MysqL.sock user =
MysqL explicit_defaults_for_timestamp=true bind-address = 0.0.0.0 server-id = 1 log_error=/var/log/
MysqL.log #
disabling symbolic-links is recommended to prevent assorted s
ecurity risks symbolic-links=0 # Settings user and group are ig
nored when sy
stemd is used. # If you need to run
MysqLd under a different user or group, # customize your sy
stemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Sy
stemd [
MysqLd_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid
加入环境变量
vim /etc/profile
尾部加入下面两行
PATH=$PATH:/usr/local/
MysqL/bin/ export PATH
执行source使环境变量立即生效
source /etc/profile
初始化
数据库,安装密钥
MysqLd --defaults-file=/etc/my.cnf --initialize-insecure --basedir=/usr/local/
MysqL --datadir=/usr/local/
MysqL/data --user=
MysqL MysqL_ssl_rsa_setup
#–initialize-insecure
不生成随机密码
#–initialize
生成随机密码
设置开机启动
cp /usr/local/
MysqL/usr/lib/sy
stemd/sy
stem/
MysqLd.service /usr/lib/sy
stemd/sy
stem
重新载入 sy
stemd
sy
stemctl daemon-reload
开机启动
sy
stemctl enable
MysqLd.service sy
stemctl start
MysqLd.service [root@jmsiteos7
MysqL-5.7.20]# sy
stemctl status
MysqLd.service ●
MysqLd.service -
MysqL Server Loaded: loaded (/usr/lib/sy
stemd/sy
stem/
MysqLd.service; enabled;
vendor preset:
disabled) Active: active (running) since 五 2019-01-04 15:36:52 CST; 3s ago Docs: man:
MysqLd(8) http://dev.
MysqL.com/doc/refman/en/using-sy
stemd.html Process: 30737 ExecStart=/usr/local/
MysqL/bin/
MysqLd --daemonize --pid-file=/var/run/
MysqLd/
MysqLd.pid $
MysqLD_OPTS (code=exited, status=0/SUCCESS) Process: 30719 ExecStartPre=/usr/local/
MysqL/bin/
MysqLd_pre_sy
stemd (code=exited, status=0/SUCCESS) Main PID: 30740 (
MysqLd) CGroup: /sy
stem.slice/
MysqLd.service └─30740 /usr/local/
MysqL/bin/
MysqLd --daemonize --pid-file=/var/run/
MysqLd/
MysqLd.pid 1月 04 15:36:51 jmsiteos7 sy
stemd[1]: Starting
MysqL Server... 1月 04 15:36:52 jmsiteos7 sy
stemd[1]: Started
MysqL Server.
设置密码(如果初始化参数是–initialize,这步跳过)
MysqL_secure_installation
登录MysqL
[root@jmsiteos7 ~]#
MysqL -uroot -p
MysqL: [Warning] Using a password on the command line interface can be insecure. Welcome to the
MysqL monitor. Commands end with ; or \g. Your
MysqL connection id is 4 Server version: 5.7.20 Source
distribution
copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered
Trademark of Oracle Corporation and/or its affiliates. Other names may be
Trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MysqL> show databases; +--------------------+ | Database | +--------------------+ | @R_394_
4045@ion_schema | |
MysqL | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
MysqL> set password for root@localhost = password('yourpassword');
MysqL> flush privileges;
MysqL> quit; Bye
3.安装
php
安装依赖
yum install libxml2 libxml2-devel curl-devel openjpeg openjpeg-devel openjpeg-libs libjpeg libjpeg-devel libpng freetype libpng-devel freetype-devel openssl openssl-devel
创建源码保存目录,下载,解压
mkdir -p /usr/local/src/
PHP72 cd /usr/local/src/
PHP72 wget -c http://cn2.
PHP.net/get/
PHP-7.2.13.tar.gz tar -xzvf
PHP-7.2.13.tar.gz cd
PHP-7.2.13
配置检查
./con
figure --prefix=/usr/local/
PHP72 \ --with-con
fig-file-path=/usr/local/
PHP72/etc \ --with-con
fig-file-scan-dir=/usr/local/
PHP72/etc/
PHP.d \ --with-mhash \ --
disable-debug \ --
disable-rpath \ --enable-
MysqLnd \ --with-
MysqLi \ --with-pdo-
MysqL \ --enable-fpm \ --with-fpm-user=
www-data \ --with-fpm-group=
www-data \ --with-gd \ --with-iconv \ --with-zlib \ --enable-bcmath \ --enable-xml \ --enable-shmop \ --enable-sysvsem \ --enable-inline-optimization \ --enable-mbregex \ --enable-mbstring \ --enable-ftp \ --with-openssl \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-zip \ --enable-soap \ --without-pear \ --with-gettext \ --enable-session \ --with-curl \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir
编译,安装
make -j4 make install
设置环境变量
vim /etc/profile
文件末尾加入如下两行
代码
PATH=$PATH:/usr/local/
PHP72/bin/:/usr/local/
PHP72/sbin/ export PATH
使之立即生效
source /etc/profile
测试一下
[root@jmsiteos7
PHP-7.2.13]#
PHP -v
PHP 7.2.13 (cli) (built: Jan 4 2019 17:35:17) ( NTS )
copyright (c) 1997-2018 The
PHP Group Zend Engine v3.2.0,
copyright (c) 1998-2018 Zend Technologies
设置
PHP.ini和
PHP-fpm.conf,www.conf
进入源码目录
cd /usr/local/src/
PHP72/
PHP-7.2.13
复制示例配置
cp
PHP.ini-development /usr/local/
PHP72/etc/
PHP.ini #或 cp
PHP.ini-production /usr/local/
PHP72/etc/
PHP.ini
进入
PHP.ini目录
cd /usr/local/
PHP72/etc
打开
配置文件
vim /usr/local/
PHP72/etc/
PHP.ini
更改pdo_
MysqL.default_socket为上面安装
MysqL时
.sock设定的位置
pdo_
MysqL.default_socket = /var/lib/
MysqL/
MysqL.sock
如果不设置,
PHP通过pdo连接
MysqL时会报
sqlSTATE[HY000] [2002] No such file or directory
复制fpm示例配置
cp
PHP-fpm.conf.default
PHP-fpm.conf
进入
PHP-fpm.d目录
cd /usr/local/
PHP72/etc/
PHP-fpm.d
复制www.conf
cp www.conf.default www.conf
设置
PHP-fpm开机启动
cp /usr/local/src/
PHP72/
PHP-7.2.13/sapi/fpm/
PHP-fpm.service /usr/lib/sy
stemd/sy
stem/
重新载入 sy
stemd
sy
stemctl daemon-reload
启用开机启动
sy
stemctl enable
PHP-fpm Created symlink from /etc/sy
stemd/sy
stem/
multi-user.target.wants/
PHP-fpm.service to /usr/lib/sy
stemd/sy
stem/
PHP-fpm.service.
启动
PHP-fpm
sy
stemctl start
PHP-fpm
查看状态
[root@jmsiteos7
PHP-fpm.d]# sy
stemctl status
PHP-fpm ●
PHP-fpm.service - The
PHP FastCGI Process Manager Loaded: loaded (/usr/lib/sy
stemd/sy
stem/
PHP-fpm.service; enabled;
vendor preset:
disabled) Active: active (running) since 五 2019-01-04 17:55:16 CST; 5s ago Main PID: 70669 (
PHP-fpm) CGroup: /sy
stem.slice/
PHP-fpm.service ├─70669
PHP-fpm: master process (/usr/local/
PHP72/etc/
PHP-fpm.conf) ├─70670
PHP-fpm: pool www └─70671
PHP-fpm: pool www 1月 04 17:55:16 jmsiteos7 sy
stemd[1]: Started The
PHP FastCGI Process Manager.
4.验证安装的
Nginx,
PHP,
MysqL
编辑
Nginx配置文件
vim /usr/local/
Nginx/conf/
Nginx.conf
#更改运行
用户 user
www-data; #编辑server段,
默认
文件添加index.
PHP location / { root html; index index.
PHP index.html index.htm; } #匹配
PHP的配置块取消注释并更改/scripts为$document_root location ~ \.
PHP$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.
PHP; fastcgi_p
aram SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_p
arams; }
保存并
退出,重启
Nginx
sy
stemctl restart
Nginx
Nginx默认的web目录下新建index.
PHP
vim /usr/local/
Nginx/html/index.
PHP
输入如下
PHP代码
<?
PHP $dbms='
MysqL'; //
数据库类型 $host='localhost'; //
数据库主机名 $
dbname='
MysqL'; //使用的
数据库 $user='root'; //
数据库连接
用户名 $pass=''; //对应的
密码 $dsn="$dbms:host=$host;
dbname=$
dbname"; try { $dbh = new PDO($dsn, $user, $pass); //初始化
一个PDO对象 echo "连接成功<br/>"; foreach ($dbh->query('SELECT db from db') as $row) { print_r($row); } $dbh = null; } catch (PDOException $e) { die ("Error!: " . $e->getMessage() . "<br/>"); } ?>
浏览器访问你的
站点
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。