- To use mongo shell:
> mongo --shell # Open Mongodb shell
> show dbs # Show all databases' names
> use amniat # Open the db with the name of amniat
> db.users.find() # Show all users
> db.users.updateOne( # Update a user
{ "name" : "ashkan" },
{ $set: { "type" : "admin" } }
);
- to backup:
mongodump --out ./acs-27-Azar-1397 --db acs
- to restore:
mongorestore ./acs-27-Azar-1397
- to move the backup:
rsync -aih --progress --log-file=[destination]/ingest.log [source] [destination]
rsync -aih --progress --log-file=./ingest.log username@ip:/root/backups/acs-27-Azar-1397 ./
- compress a file
tar -czvf backups/amniatelec/amniatelec.tar.gz ./amniatelec/ --exclude=/amniatelec/node_modules
mysql -h host -u user -p # lacking host means localhost
> show databases; # show all databases (; is mandatory)
> use dbname; # Use a database with the name of dbname
> select database(); # Show the database that has been selected (is in use)
> show tables; # Show all the tables
> describe tablename; # Show structure of a table
> drop database `databasename`; # Deleting a database
> create database `databaseName` charset utf8; # creating a new database
> SELECT User FROM mysql.user; # see all defined users
- sending mysql backup from local to the vps:
rsync -aih --progress --log-file=./ingest.log ./electr95_amniatElec-14Dey1397.sql username@ip:/root/backups/wordpress/
- Creating backup for a mysql database:
mysqldump -u USERNAME -pPASSWORD OLD_WORDPRESS_DB_NAME > new_WORDPRESS.sql
-
Recover the database from backup (If the database has already created)
- I used this which worked well.
mysql -u USERNAME -pPASSWORD DATABASE_NAME < new_database.sql
-
Migrating MySQL database file into new database environment (The database has not been created)\
- I tested this.
mysql -u USER -pPASSWORD
> use <your database name>;
> source /path/to/your_sql_file.sql;
> exit;
- In the VPS:
cd /var
mkdir repo && cd repo
mkdir eSecMag.git && cd eSecMag.git
git init --bare
cd hooks
cat > post-receive
#!/bin/sh
git --work-tree=/var/repo/eSecMag --git-dir=/var/repo/eSecMag.git checkout -f
# now enter ctrl + d to save the file
# and make the post-receive file executable:
# chmod +x post-receive
- In local repository:
git remote add vps ssh://[email protected]/var/repo/site.git
- If the hooks/post-receive doesn't work and you have set the file as executable correctly and you see this message:
Everything up-to-date
# It can be because that there is no new changes in your push. So change a file and commit it and push again.
- If you saw this message:
remote: fatal: You are on a branch yet to be born
# You can solve it by first push your master branch before pushing your desired branch:
# git push serverRemote master
- After this, the files in the word-tree directory will be the master branch files. If you want to update the files after each push with your desired branch files, you must checkout on your desired branch from the bare repository in server (you must enter your work-tree directory in the command):
git --work-tree=/path/to/work/tree checkout yourDesiredBranch
- توجه: در صورتی که از nginx استفاده کنیم نیازی به آپاچی نیست.
sudo apt update
sudo apt install apache2
# چک کردن مجوزها در فایروال
sudo ufw app list
#Output
#Available applications:
# Apache
# Apache Full
# Apache Secure
# OpenSSH
# to verify that everything went as planned by visiting your server's public IP address:
# http://your_server_ip
# sudo systemctl status apache2
# Do this if there is any problem:
sudo ufw app info "Apache Full"
sudo apt install mysql-server
# This will remove some dangerous defaults and lock down access to your database system
sudo mysql_secure_installation
# This will ask you "VALIDATE PASSWORD PLUGIN"
# If yes: You should select strong passwords contain numbers, upper and lowercase letters, and special characters, and which is not based on common dictionary words.
- If you don't need nginx and using Apache, Do this:
- Linux, Apache, MySQL, PHP (LAMP stack)
sudo apt install php libapache2-mod-php php-mysql
# To see if everything is right, create this file:
sudo vi /var/www/html/info.php
# write this and save
<?php
phpinfo();
?>
# Now go to this address:
http://your_server_ip/info.php
# Remove the file after checking the php info
sudo rm /var/www/html/info.php
-
If you want to install nginx and not using Apache, Do this:
- Linux, Nginx, MySQL, PHP (LEMP stack)
sudo apt install php-fpm php-mysql
# To see if everything is right, create this file:
sudo vi /var/www/html/info.php
# add you nginx config ffor the php based website
sudo vi /etc/nginx/sites-available/default
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
mysql -u root pw
> CREATE DATABASE dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
> GRANT ALL ON dbname.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'wordpressuserpassword';
> FLUSH PRIVILEGES;
> exit;
-
after installing the wordpress with correct information (and correct prefix!!), I removed the database and create a new mysql database and restore the backed-up database into it.
-
I installed the wordpress again (with correct database prefix, which needs to create a new empty database); Then I restored the database file (.sql) in the new database. Note: The theme is important, If the theme's file are'nt there, It will cause a blank page in wordpress website (note thant wp address must have http:// too).
-
I needded to change options table too (temporarily):
update amniatoptions set option_value = "http://localhost:8080" where option_name = "home";
update amniatoptions set option_value = "http://localhost:8080" where option_name = "siteurl";
- For being able to upload new images or files:
chown -R www-data: wp-content
- Wordpress images in articles were not displaying, I added this to the functions.php:
// This will disable responsive images (srcset images)
function disable_srcset( $sources ) {
return false;
}
add_filter( 'wp_calculate_image_srcset', 'disable_srcset' );
- To get a custom field rom wp rest api:
function get_post_meta_cb($object, $field_name, $request){
return get_post_meta($object['id'], $field_name, true);
}
function update_post_meta_cb($value, $object, $field_name){
return update_post_meta($object['id'], $field_name, $value);
}
add_action('rest_api_init', function(){
register_rest_field('post', 'access',
array(
'get_callback' => 'get_post_meta_cb',
'update_callback' => 'update_post_meta_cb',
'schema' => null
)
);
});
وجود سمیکولون در پایان تمام سطرها اجباری است.
- sudo service nginx status وضعیت nginx
- sudo nginx -t تست گرامری کانفیگها
- sudo systemctl restart nginx رستارت انجینایکس. پس از هر تغییر کانفیگها میبایست رستارت شود
- server_name نمیتواند آیپی باشد
- listen 80 default_server; برای وقتی که اگر برای پورتهای مشابه، نام سرور با هیچکدام مطابقت نداشت، این را به عنوان دیفالت قبول کند
server {
listen 80;
server_name cms.ashkanph.ir;
root /home/ubuntu/projects/electronic-security-mag/wordpress;
index index.php index.html index.htm index.nginx-debian.html;
# server_name example.com;
location ~ /wp-json/ {
try_files $uri $uri/ /index.php?$args;
}
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
server {
server_name ashkanph.ir www.ashkanph.ir;
location / {
proxy_pass http://127.0.0.1:3100;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/ashkanph.ir/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/ashkanph.ir/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.ashkanph.ir) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = ashkanph.ir) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name ashkanph.ir www.ashkanph.ir;
return 404; # managed by Certbot
}
ساخت سرویسها در اینجا:
/etc/systemd/system/e-sec-mag-app
مثال:
[Unit]
Description=our Application <[email protected]>
after=network.target mongod.service
[Service]
Type=simple
#WorkingDirectory= /home/ubuntu/projects/electronic-security-mag/application/dist/bin/
WorkingDirectory= /home/ubuntu/projects/electronic-security-mag/application/
ExecStart=/snap/bin/node dist/bin/www
#ExecStart=/snap/bin/node /home/ubuntu/projects/test/test.js
ExecStop=/bin/kill -HUP $MAINPID
; reload is diabled ExecReload=
; do not kill, only sending signal is enough
KillMode=none
KillSignal=SIGHUP
TimeoutStopSec=10
; Restart=always
Restart=on-failure
RestartSec=60
RestartPreventExitStatus=255
User=ubuntu
[Install]
WantedBy=multi-user.target
Alias=e-sec-mag.service
- then: sudo systemctl daemon-reload # Do this any time you change any service file
- then: sudo systemctl start e-sec-mag
- sudo systemctl enable e-sec-mag # Make the application start up when the machine boots